Usha Guduri

Git Branches - Local

Once you clone a Git repository, you are highly unlikely to work on the master unless you are a Maverick! The immediate thing you will look out for is branching - whether it is to add new features or even fix your bugs before pushing them live (Do you do ‘Test Driven Development’, by the way? :-D ). And Git is a great tool in this realm because branches are really really cheap - both in terms of memory and time (when switching between branches)

You start off with knowing the current branch you are working on with

$ git branch

You can get a list of all the branches using:

$ git branch -a --> all local and remote branches
$ git branch -r --> remote branches only

Creating a local branch is a simple

$ git branch myLocalBranch

Beware that the above only creates a branch, it does not check it out and set it up ready for use. To do both at once, you can use:

$ git checkout -b myLocalBranch

While working in branches is good, it is also a good practice to keep your branch as close as possible to master (if you want to avoid day long conflict resolutions) by regularly updating from master by one of the 2 following ways:

  1. merging master into your branch. This will create a separate commit that master was merged or a custom message that you may choose in case of conflicts.

     $ git merge master
    
  2. rebasing against master, generally a cleaner (preferred by many) way to get the changes into your branch. This does not create a separate commit in case of conflicts.

     $ git rebase master
     $ git rebase --continue (once you resolve any conflicts)
    

Note that these are just local branches on your machine. All changes/commits reside on your machine, not on the remote server. We’ll look at remote branches soon.

Comments