Using Git Properly

To anyone who has tried to figure out how to use git on the command line, it is clear how much there is to know.  Most people provide an exhaustive list of all of the possible commands, which is overwhelming and not very helpful, especially for a beginner.

I am not going to do that.  Instead, I will show you a standard workflow that will work for any of your projects and the 8 commands you need for that.  Trust me, once you know these 8 commands, they will be all you need 98% of the time.

Standard Workflow:

I am going to discuss this from the standpoint of someone working on a project that is on Github and has multiple contributors.  However, even if you are the sole contributor, you can follow the exact same workflow.  I am also going to assume that you already have your project set up, but if you don’t, there are good instructions on Github.

The general git workflow goes as follows:

  1. Make sure that the local version of your project is up to date.
  2. Create a test copy of your project (branch).
  3. Make all your changes and test in this branch.
  4. Check again to make sure your local version of the project is up to date.
  5. If you are happy with your changes, merge them into the main project.  Otherwise you can delete them.
  6. Update the version of your project on Github.
Git branches

Here you can see sort of what this might look like. The grey circles are the main project, and the yellow ones are your branch.

 

Now lets actually look at the commands for this process:

1. Pull

First, we have to update our local version of the project.  This is actually unnecessary if you are the only one working on the project, but its not bad to just do it anyway.  For this we use the pull command:

git pull origin master

This will pull the master branch from your online repository and update the master branch in your local repo.

2. Checkout

Next, we want to create a new branch for us to create our new feature.  First, we want to make sure that we’re on the master branch.  We can switch to that with the command:

git checkout master

The checkout command will switch our current branch to the master branch. Now we want to create a new branch:

git checkout -b update

By adding the -b flag, we are telling git to create a new branch with the name update before switching to it.

Now that you have created a new branch, you can go ahead and make any changes that you want to your code.  If you mess up and want to just get rid of the branch and start over, just do:

git commit -m "Commit message"

git checkout master

git branch -d [branchname]

3. Status

Now that you’ve made changes, it is time to save (commit) them in git.  Status is not actually an essential command, but it is very useful:

git status

This command will show you which files have been changed/added.  It also shows you which files will actually be committed.  If you accidentally changed files you didn’t mean to change or you forgot to stage a file to be committed, this can be helpful.

4. Add

Once you are satisfied with your changes, you have to ‘stage’ them so that git knows which files to commit.  If you want to add all of the new/changed files, it is easy:

git add .

You can also add individual files:

git add [filename]

This will tell git to commit all the files you have added.  You can also use status again to see which files are now staged.

5. Commit

Once you’ve staged all of your files, you have to actually commit them:

git commit -m "Commit message"

Git requires you to add a commit message, and by adding the -m flag, we are able to add the message in the same line, instead of being prompted to add a message after committing.

Now it is time for us to merge our branch back into the main branch.  You want to make sure that you triple check your branch before merging it back because if you merge in a bug, that defeats the purpose of making a branch in the first place.

6. Merge

Merging will require 2 steps.  First, you want to pull again as you did in the first step (and again, this is unnecessary if you are the sole contributor).  This will ensure that you are merging into the most up-to-date version of the project.  Then:

git merge [branchname]

This will merge [branchname] into the current branch, master. Unless multiple people have made changes to the same lines of code, git should be able to do this automatically.

7. Push

Now we need to update the version of the code on Github:

git push origin master

This will push the master branch from our local project to the main version on Github.

8. Branch

Finally, this step is simply a matter of preference, but I like to delete any of my branches when I’m done with them.  This allows me to reuse the same branch names.

git branch

This will allow us to see the names of all of our branches, as well as which branch we are currently on (denoted by a ‘*’). To delete a branch:

git branch -d [branchname]

Just make sure you don’t delete the master branch, and you can’t delete the branch you’re currently on.

 

So there you have it.  This general workflow is what I use 98% of the time when using Git.  Of course there are many other things you can do as you become more of a power user, but hopefully this will be a good start.  In the near future, I hope to post a cheat sheet that will just have descriptions of some of the most useful commands (mostly the ones used in this post) so that you can refer to them as needed.

I hope this post has been helpful.  Please let me know what you think of it!

Advertisements