Presented by Krishna Prasad

About Us

What is our aim

Git branch ( git branch ) Command

Git Branch is a command which helps in creating, deleting and listing branches of the main code. It is part of regular development process which helps in adding new features in a new branch to reverify your changes such that unstable code does not get merged with the master branch.

git branch helps in simultaneous development of multiple features in parallel branches and retains the stable code in the master branch

git branch acts as a pointer to a series of commits and has independent development line. it gives the project a fork like structure as new commits are saved in the current branch

Another feature of it is that it does not let one switch between the branches.

  • To list all branches you have already locally checkout till now, use the following command: $ git branch

    Above commands will list all the branch in your local system and current branch should be marked with green colour

    krishna@krishna-server1:~/Documents/gitcommands$ git branch
    * gh-pages
      master
    krishna@krishna-server1:~/Documents/gitcommands$ 

  • To list all the branch at remote as well locally checkedout ones, use the following command: $ git branch -a

    -a or (--all) option in git branch will provide the List of both remote-tracking branches and local brnaches. e.g: in the my machine:

    krishna@krishna-server1:~/Documents/gitcommands$ git branch -a
    * gh-pages
      master
      remotes/origin/HEAD -> origin/master
      remotes/origin/gh-pages
      remotes/origin/master
    krishna@krishna-server1:~/Documents/gitcommands$

  • To delete branch created from git checkout command or git branch, use the following command: $ git branch -d <'branchName'>

    -d or --delete option in git branch delete a branch from your local machine. The branch must be fully merged in its upstream branch

    -D option in git branch is just Shortcut for --delete --force i.e force removal from your machine of a branch

How to create branch from git branch command

This is bit tricky questions and not eveybody is aware of it, so below is the examples to create a new branch t, in the example we have two branches gh-pages and master and then we have create branch t as follow

krishna@krishna-server1:~/Documents/gitcommands$ git branch
* gh-pages
  master
krishna@krishna-server1:~/Documents/gitcommands$ git branch t gh-pages 
krishna@krishna-server1:~/Documents/gitcommands$ git switch t
Switched to branch 't'
krishna@krishna-server1:~/Documents/gitcommands$ git branch
  gh-pages
  master
* t
krishna@krishna-server1:~/Documents/gitcommands$ 
The above command git branch <'newBranchName'> <'fromBranchName'> can be combined as git checkout -b <'newBranchName'> for more details please check git checkout comamnd