Git list branches

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


When listing branches in git, it’s important to know the difference between local and remote branches, understand which branch you are currently on, and understand the branch tree. This guide will walk through all the different intricacies of Git branches.

  • List Local Branches: To see a list of your local branches, run:

    git branch

    This command displays all local branches. The current branch will be highlighted and marked with an asterisk.

  • Get current git branch:

    • To find out which branch you are currently on:

      git branch --show-current

    • Alternatively, use:

      git rev-parse --abbrev-ref HEAD

  • List all remote branches: To view branches on your remote repositories:

    git branch -r

    This shows you all branches that you've fetched from your remote.

  • Git branch list all: To see both local and remote branches:

    git branch -a

  • Git branch tree: While Git does not have a built-in tree view for branches, you can use the git log --graph command to get a tree-like representation of the commit history.

  • Git branch list format: Customize the output format of the branch listing using the -format option:

    git branch --format="%(refname:short) %(upstream:short) %(contents:subject)"

    This example shows the branch name, its upstream branch, and the subject of the latest commit.

  • Git list branch names: If you're only interested in the names of the branches without any additional info, sticking with git branch or git branch -a for all branches is your best bet.

  • Git command for branch list with patterns: to list branches that match a certain pattern run:

    git branch --list '*pattern*'

    The pattern provided will act as a shell wildcard. If multiple are provided, the command will return all branches matching any of the patterns.

  • Git list all origin branches: In order to specifically list all branches from the origin remote, run:

    git branch -r | grep 'origin/'

  • Git branch command not showing all branches: If git branch doesn't show expected branches, ensure you've fetched the latest updates from your remote:

    git fetch --all

  • Git branch not showing current branch: Ensure you're in a Git repository and not in a detached HEAD state. Use git branch --show-current to attempt to display the current branch.

  • Git show-current branch in terminal: Customize your terminal prompt to always show the current Git branch by editing your shell’s configuration file (e.g., .bashrc, .zshrc) and using Git's shell scripting capabilities. Open your shell configuration file in your favorite text editor and add this line to the bottom:
Terminal
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \\(.*\\)/ (\\1)/'
}
export PS1="\\u@\\h \\[\\033[32m\\]\\w - \\$(parse_git_branch)\\[\\033[00m\\] $ "
  • Git command get branch name:

    git rev-parse --abbrev-ref HEAD

    This is a straightforward way to get the current branch name for scripting purposes.

For further reading please see the official git documentation on git branches.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2