top of page

Git Cheatsheet - Branches

  • Writer: Anuja Dalal
    Anuja Dalal
  • Dec 28, 2023
  • 3 min read

Updated: Apr 21, 2025



Branching is not a new concept in Version Control Systems. Git has taken this concept to a whole new level of sophistication. Git's branching mechanism makes the Git one of the best and most powerful Version Control Systems ever created.


Let's begin from the basics.

Listing of branches

The following various ways to list the branches will have a * sign used in output to denote the current checked-out branch.
# If you run it with no arguments, you get a simple listing of your branches
$ git branch
  develop
  feature/bug-1
* feature/bug-2
  feature/services
  master
$
# List all branches (local and tracking)
$ git branch -a
# or
$ git branch --all
  develop
  feature/bug-1
* feature/bug-2
  feature/services
  master
  remotes/origin/develop
  remotes/origin/feature/bug-1
  remotes/origin/feature/header-improvement
  remotes/origin/feature/home-page-improvements
  remotes/origin/feature/services
  remotes/origin/feature/user-profile-improvements
  remotes/origin/master
# To see the last commit on each branch, you can run git branch -v
$ git branch -v
  develop          ada6a43 Add Camera feature
  feature/bug-1    f2a5e6f Fix typo
* feature/bug-2    ac467f3 Use correct library
  feature/services 6386f84 Add Service feature
  master           c42a2f6 [behind 2] Initial commit
$

To see the last commit on each branch with tracking/upstream branches, you can run git branch -vv.

It’s important to note that the output of this option will include commits only since the last time you fetched from the server. Any new remote branch pushed after your last fetch will not appear in this command's output. Output also includes ahead and behind numbers.

$ git branch -vv
  develop          ada6a43 [origin/develop] Add Camera feature  
  feature/bug-1    f2a5e6f [origin/feature/bug-1] Fix typo
* feature/bug-2    ac467f3 Use correct library
  feature/services 6386f84 [origin/feature/services] Add Service feature
  master           c42a2f6 [origin/master: behind 2] Initial commit
$ 
# If you want up-to-date branch status for the -vv option with correct ahead and behind numbers, you’ll need to fetch all your remotes right before running this -
$ git fetch --all; git branch -vv
# To see which branches are already merged into the branch you’re on, you can run git branch --merged
$ git branch --merged
  develop
  feature/bug-1
  feature/services
* master
$
# To see all the branches that contain work you haven’t yet merged in, you can run git branch --no-merged
# This command will not show the current checked-out branch
$ git branch --no-merged
  feature/bug-2
$
Important to note, that the branches listed with the --no-merged option, cannot be deleted unless used -D option for force delete

# To use --merged and --no-merged options with a branch or commit that is not checked out, specify that branch name or commit hash
$ git checkout develop
$ git branch --no-merged master
  feature/bug-2
$
$ git branch --merged master
* develop
  feature/bug-1
  feature/services
  master
$
$ git branch --no-merged <branch-or-commit-hash>

Create and Switch Branches

# To create a branch but do not switch
$ git branch hotfix

# To switch to the existing branch
$ git checkout hotfix

# To create a branch and checkout in a single command
$ git checkout -b hotfix

# To create and checkout remote branch with a different name for the local tracking branch (ex: local-hotfix)
$ git checkout -b local-hotfix origin/hotfix

# If you already have a local branch and want to set it to a remote branch you just pulled down, or want to change the upstream branch you’re tracking
$ git branch -u origin/serverfix

From Git version 2.23 onwards you can use git switch instead of git checkout to:

# To create a branch and switch
$ git switch -c newbranch

# To switch to the existing branch
$ git switch somebranch

# Return to your previously checked-out branch
$ git switch -

Rename Branches

Renaming branches is not recommended. To avoid renaming, identify some branch naming rules that can be agreed upon by the team.

# Rename brach while keeping all history at local work
$ git branch --move old-branch-name corrected-new-branch-name

# To push a new branch at the remote repository use the following command, please note this will not delete the old branch from the remote repository
$ git push --set-upstream origin corrected-new-branch-name

# You can consequently delete the old branch from the remote repository
$ git push origin --delete old-branch-name

Delete Branches

# Switch to the master branch before removing the child branch
$ git branch -d hotfix

# To delete a branch from the remote repository
$ git push origin --delete old-branch-name

In Git, creating branches is an inexpensive and powerful feature. There are various Git workflows suggested (Ex: GitFlow workflows, Feature branch workflows, Trunk-based workflows etc) which explain different branching models to minimize the conflicts in bigger teams. I will encourage readers to go through it for better understanding.


Meanwhile, keep this cheat sheet handy for managing branches.


Here are some additional topics to read:




Comments


Drop Me a Line, Let Me Know What You Think

Thanks for submitting!

© 2020 by Monologue, Proudly created with Wix.com

Website Disclaimer: The information provided on our site https://anujadalal2020.wixsite.com/monologue is for general informational purposes only. All information provided on this site is in good faith. However, we make no representation or warranty of any kind, express or implied, regarding the validity, accuracy, adequacy, reliability or completeness of any information. Under any circumstances, we have no liability to you for any loss or damage. You assume sole responsibility for the use or reliance on any information contained on this site. Last Updated: Sept 15 2023

bottom of page