Git: Branching

In this article, we will look at the commands git branch, git checkout and git merge. If you haven't already done so, check you know the fundamentals of git in Git: Setup and Git: Basics.

If we are working on our own small project without collaborators, we can just about get away without learning how to branch, doing all our development work on the default master branch. However, branching makes things much easier for us, even on small projects. It allows us to keep a working copy (on our master branch), which we can visit any time we want to run our program. It also allows us to keep the development of different features completely separate - this gives us flexibility in deciding what we want to work on and when.

So how does it work?

Lets start off by creating a testingBranches directory and adding a text file master.txt (as we are on the default master branch).

                    $ mkdir testBranching
                        $ cd testBranching/
                        $ touch master.txt
                        $
                    

Initialise this as a git repository and make an initial commit.

                        $ git init
                        Initialized empty Git repository in myComp/testBranching/.git/
                        $ git add .
                        $ git commit -m "initial commit"
                        [master (root-commit) 5710d08] initial commit
                        1 file changed, 2 insertions(+)
                        create mode 100644 text.txt
                        $
                    

Running git branch on its own without arguments will tell us what branch we are currently on.

                        $ git branch
                        * master
                        $
                    

As you can see, there is only one branch (the master branch). The asterix indicates that we are on this branch.

Creating a branch

Let's create a branch to work on our first feature. To do this we use the git branch command with an additional argument for the branch name.

                        $ git branch feature
                        $ git branch
                          feature
                        * master
                        $
                    
Great! We have created our branch. But, as you can see above, we are still on our master branch. In order to move to our new feature branch, we need to use the git checkout command, again giving the branch name 'feature' as an argument. Essentially, this makes git HEAD point at our feature branch.
                        $ git checkout feature
                        Switched to branch 'feature'
                        $ git branch
                        * feature
                          master
                        $ ls  <--  checking we can still see our master.txt file
                        master.txt
                        $
                    

Making changes on our branch

Now we are working on our feature branch. We are going to add a new text file firstFeature.txt and commit this change.
                        $ touch firstFeature.txt
                        $ git add .
                        $ git commit -m "added the first feature"
                        [feature ad77946] added the first feature
                         1 file changed, 0 insertions(+), 0 deletions(-)
                         create mode 100644 firstFeature.txt
                        $
                    
Looking back at our log history we can see all commits from master up to the point we branched off, and our commits on this branch as well.
                        $ git log --pretty=oneline --abbrev-commit
                        ad77946 (HEAD -> feature) added the first feature
                        d759603 (master) initial commit
                        $
                    

Merging from master

Switching back to our master branch we can see that all the changes made in our feature branch are completely hidden.
                        $ git checkout master
                        Switched to branch 'master'
                        $ ls
                        master.txt
                        $ git log --pretty=oneline --abbrev-commit
                        d759603 (HEAD -> master) initial commit
                        $
                    
Once we have finished developing our feature and want to add it to our master branch, we use the git merge command (with the branch name 'feature' as an argument) to merge our changes into our master branch. We need to make sure we are on the master branch to pull these changes in.
                        $ git branch  <--  check we are on the master branch
                          feature
                        * master
                        $ git merge feature
                        Updating d759603..ad77946
                        Fast-forward
                         firstFeature.txt | 0
                         1 file changed, 0 insertions(+), 0 deletions(-)
                         create mode 100644 firstFeature.txt
                        $ ls
                        firstFeature.txt master.txt
                        $
                    
Great! We've created a feature branch, developed a feature there (without affecting our working program) and merged our completed feature back. Now all we have to do is tidy up and delete our feature branch.
                        $ git branch -d feature
                        Deleted branch feature (was ad77946).
                        $