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.
Listing the different types 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
Working with remote branches
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.
Advanced branch listings
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.
Filtering and formatting branch listings
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
orgit 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/'
Troubleshooting
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.
Utility Commands
- 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:
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.