Mastering the Git Command Line: A Guide for Beginners

Amelia Dahm
The Startup
Published in
5 min readOct 5, 2020

--

Photo by Kevin Horvat on Unsplash

Version control is an important aspect of technology development workflow. Grasping the in’s and out’s of Github has a learning curve, but once you understand its purpose, communicate with teammates, and utilize key command line scripts, project development can be a smooth ride. Referring back to the basics can help you be successful with Github.

Let’s imagine there is a team of four developing a mobile application. How can the team work on the project without overwriting each other’s work? Version Control. Github allows teammates to work on the same code at the same time. A developer can be writing code for a new feature while the designer is changing the color palette. Using the Github flow, this process can happen seamlessly.

Tackling Github head on will lead you to a fluid coding workflow. It may seem confusing, overwhelming, or not necessary at first, but don’t give up just yet. With the instructions and advice I have provided below, YOU will be able to start mastering your Github skills.

Let’s Dive into the Details…

The first thing you are going to need to know is What is a Repository? A repository (or repo for short) contains all of the project files and stores the revision history.

To start a new repo from the command line, change into the directory where you want your repo to be located, use:

git init

If the repository already exists on Github, navigate to the green code button, copy the link, and use:

git clone <insert copied url>

This command will put the remote copy of the files on your local machine to work on. Run this command in the directory path where you want the repository to live. The local repository will always mirror the remote repository.

Let’s Get Down to the Basics…

When working with Github, start by pulling down all the most updated changed from the shared repository using:

git pull

This will make your working directory match the Git repository. After this step, open the files, and make your edits as normal. You can always use this command to see what files have been edited:

git status

Github workflow (image: Amelia Dahm)

Next, add the files to the staging area, make a commit, and push back up to the Git repository. Here are the commands:

git add <file name>

git commit <file name> -m “Add a message indicating what you changed”

git push origin master

If you want to pull a file out of the staging area back to the working directory, you can use the following command:

git reset <file name>

If you have a file you no longer have need for, you can you the following command to remove it:

git rm <file name>

If you want to see a history of your commits, use the command:

git log

Let’s Start Branching and Merging…

A branch is a copy of the repository that is your own. You are working toward the end version, but are not making your changes on the main copy. This is good for exploratory work, bug fixes, and more. Anything you commit on your branch will not show up or affect your teammates work.

Once you have developed a feature on your branch you want your teammates to see, you can merge it back into the master branch.

(image src: https://www.nobledesktop.com/learn/git/git-branches)

Branching Commands…

The default branch is the master. To check on what branch you are working on, use the command:

git branch

To create a branch to work on, use the command:

git branch <branch name>

(Make your branch names descriptive!)

To push branch to the remote repository, use the command:

git push origin <branch name>

To switch to a branch that already exists, use the command:

git checkout <branch name>

Merge Commands…

The basic commands while on the branch are the same. Before merging your branch with the master, make sure all your files have been added and committed. Once that is completed, you are ready to make your merge.

First, checkout the master branch using the command we learned above. Then, merge the branch using the command:

git merge <branch name>

Lastly, push the changes to the remote repository using the same command as above!

To delete a branch locally when you are finished working on it, use the command:

git branch -d <branch name>

Final Thoughts and Advice…

  • Communicate with your teammates and make sure to be on the same page. It can be tough when one person does not understand the inter-workings of Github. Take some time to go over it before starting and check in on each other. Having the basics down will allow the group to put all of their energy into the project itself!
  • Make commits frequently to stay on top of your work! Make your commit messages descriptive so you can refer back to the change, and your teammates can know what was updated.
  • Make sure to pull down the most up-to-date version to work on, and create a branch for new tasks. Don’t work on the master branch! Working on the master will lead to merge conflicts with your teammates, and it will take longer the fix. Take the time to get used to branching and merging.
  • You can always check your work on Github.com . It can give you a list of recent commits, show your merge requests, and give you insights on teammates contributions.
  • If you make mistakes, do not panic! You are using Github so that mistake are okay to be made. Version control always allows you to revert back and restore previous versions.
  • Lastly, don’t be intimidated! It is okay if you don’t feel like you have the hang of it at first. Refer yourself back to the basics, and focus on learning what the overall process of git is to know what next steps to take.

Thank you for reading, and I hope this benefits your Github experience.

--

--