Git Branch Cheatsheet
August 21, 2020
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
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
There are a couple of different ways you can create branches in
- Create a branch and check it out
git branch new-branch git checkout new-branch
- Create a branch and immediately check it out
git checkout -b new-branch
- Use the new
switch -ccommand with is simular to
git checkout -b
git switch -c new-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
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
- Swtich to Previous Branch
git checkout it'll automatically switch you to the branch that you were previously in!
git checkout -
- 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
fzffor this alias to work. You can find installation instructions from their README.md.
Depending on the type of branch you want to delete there are several ways to do it.
- Delete local branch
git branch -d existing-branch
- Delete remote branch
git branch -dr origin/existing-branch
- 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
- 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
- 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.
NOTE: You can also alias
gitso that you can run
Tweet about this post and have it show up here!