Git – Branching Operations

Branches are new lines of the current development line. Branches creates new work spaces different from current and changes can be done without affecting current and other branches. After these changes generally branch will merge with the main branch.

For example we want to add new feature. To code the new feature we create new branch and develop our code after being sure we can add this to our app we merge the branch with the main branch.

Listing Branches

Listing branches can be done like below. This will show only local branches where branches resides in local. Remote branches exist in remote repositories.

$ git branch  
* master

Listing remote branches can be done like below

$ git branch -a  
* master

Because we have no remote repository there is no remote branch and this command output is not different from previous.

More information about branches can be get with following command by adding -v .

$ git branch -v 
* master 96511bc LICENSE,

As we see branches uid and last added file is show for detailed information.

Create New Branch

We can create new branch easily like below. Created branch is local and will not pushed to the remote repository.

$ git branch test 
$ git branch 
* master 
  test

As we see we create new branch with git branch test and list branches. Although we have created new branch we did not check the the branch test.

Checkout Branch

To start with working test branch we need to checkout it. After the checkout the test branch master branch will not be the HEAD. HEAD is last commit of current work space.

$ git checkout test 
Switched to branch 'test' 
$ git branch        
  master 
* test

We can create new branch and checkout with single command by providing -b argument.

$ git checkout -b new-test                                                                                                            
Switched to a new branch 'new-test' 
$ git branch  
  master 
* new-test 
  test

Rename Branch

Renaming branch is easy as other operations.

$ git branch -m new-test very-new-test 
$ git branch               
  master 
  test 
* very-new-test

Deleting Branch

Branch can be completely deleted like below.

$ git branch -d very-new-test 
Deleted branch very-new-test (was 96511bc). 
$ git branch        
  master 
* test

Push Changes to the Remote Repository

We can push a specific branch to the remote repository by specifying remote branch. If the remote branch is do not exists the branch is created at remote. If we do not specify remote repository by default origin is assumed.

$ git push origin test 
Counting objects: 3, done. 
Delta compression using up to 4 threads. 
Compressing objects: 100% (2/2), done. 
Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done. 
Total 3 (delta 1), reused 0 (delta 0) 
To /home/john/myproject/ 
   96511bc..eaa2efa  test -> test

Diffing Between Branches

Two branches can be diffed by using diff command.

$ git diff master test 
diff --git a/thisistest b/thisistest 
new file mode 100644 
index 0000000..e69de29

Leave a Comment