Git Branch Cheatsheet


August 21, 2020

Reading time
4 min read


Working on the command line with git can be a bit overwhelming, so I'm starting a series of git cheatsheet posts for various areas. This post focuses on git branch.

List Branches

There are many different ways to list git branches. The commands start with git branch, but then you can provide addition flags to adjust or filter the data that gets displayed.

# List local branches
git branch

# List local & remote branches
git branch -a

# List branches sorted by most recent commit date
git branch --sort=-committerdate

# List by branches that have been merged into the main branch
git branch --merged main

# List by branches that have not been merged
git branch --no-merged

# List branches with their upstream and last commit message
git branch -vv

Create Branch

There are a couple of different ways you can create branches in git

  1. Create a branch and check it out
git branch new-branch
git checkout new-branch
  1. Create a branch and immediately check it out
git checkout -b new-branch
  1. Use the new switch -c command with is simular to git checkout -b
git switch -c new-branch

Rename Branch

Sometimes you need to rename a branch for one reason or another. You can provide the -m flag giving the old name and the new name.

git branch -m old-branch new-branch

Switch Branch

You can switch branches with git checkout or with the new git switch commands. Either of the following do the exact same thing.

git checkout existing-branch
git swtich existing-branch

In addition to the above, I have two favorite ways of switching branches

  1. Swtich to Previous Branch

By passing - to git checkout it'll automatically switch you to the branch that you were previously in!

git checkout -
  1. Use an alias to list recent branches and interactively choose one

I also have a ~/.zshrc alias to list out my branches sorted by commit date and pipe those to fzf so that I can choose from a list the branch I'd like to checkout.

alias cb='git branch --sort=-committerdate | fzf --header Checkout | xargs git checkout'

NOTE: You'll need to install fzf for this alias to work. You can find installation instructions from their

Delete Branch

Depending on the type of branch you want to delete there are several ways to do it.

  1. Delete local branch
git branch -d existing-branch
  1. Delete remote branch
git branch -dr origin/existing-branch
  1. Delete merged branches

The following command will list out branches that have been already merged into the main branch (excluding the main branch itself) and then delete those locally

git branch --merged main | grep -v "main" | xargs -n 1 git branch -d
  1. Delete stale tracking branches

Sometimes you have branches locally that are no longer tracked in GitHub or wherever. The prune command will help you identify those and remove them. Thankfully there is a --dry-run mode so you can preview what would happen if you were to run the command for real.

git remote prune origin --dry-run
git remote prune origin
  1. GitHub Hub CLI

One of my favorite commands to clean-up my branches does not come with git itself, but is bundled with the GitHub Hub CLI. The sync command will fetch from upstream and update local branches AND if it determines a branch is merged and its upstream branch was deleted, then it will be deleted automatically. I run this all the time.

hub sync

NOTE: You can also alias hub to be git so that you can run git sync

Web Mentions

Tweet about this post and have it show up here!