Skip to main content

Synopsis

git branch [--list] [<pattern>...]
git branch [--track | --no-track] [-f] <branch-name> [<start-point>]
git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branch-name>]
git branch --unset-upstream [<branch-name>]
git branch (-m | -M) [<old-branch>] <new-branch>
git branch (-c | -C) [<old-branch>] <new-branch>
git branch (-d | -D) [-r] <branch-name>...
git branch --edit-description [<branch-name>]

Description

List, create, or delete branches. The current branch will be highlighted and marked with an asterisk. If --list is given, or if there are no non-option arguments, existing branches are listed. When creating a new branch, it will be created based on the current HEAD or the specified <start-point>. Note that this will create the new branch, but it will not switch the working tree to it; use git switch <new-branch> to switch to the new branch.

Common Usage

1

List all branches

View all local branches:
git branch
To see remote branches as well:
git branch -a
2

Create a new branch

Create a new branch from current HEAD:
git branch feature-branch
Create from a specific commit:
git branch feature-branch main
3

Delete a branch

Delete a fully merged branch:
git branch -d old-branch
Force delete an unmerged branch:
git branch -D experimental-branch
4

Rename a branch

Rename the current branch:
git branch -m new-name
Rename a specific branch:
git branch -m old-name new-name

Options

Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set.
git branch -d feature-branch
Shortcut for --delete --force. Delete the branch irrespective of its merged status.
git branch -D experimental-branch
Move/rename a branch, together with its config and reflog.
git branch -m old-name new-name
Shortcut for --move --force. Rename even if the new branch name already exists.
git branch -M old-name new-name
Copy a branch, together with its config and reflog.
git branch -c feature backup-feature
List or delete remote-tracking branches.
git branch -r
List both remote-tracking branches and local branches.
git branch -a
Show SHA1 and commit subject line for each head. If given twice, print the path of the linked worktree and the name of the upstream branch.
git branch -vv
Print the name of the current branch. In detached HEAD state, nothing is printed.
git branch --show-current
List branches. With optional pattern, list only the branches that match.
git branch --list 'feature-*'
Set up branch’s tracking information so upstream is considered the branch’s upstream branch.
git branch -u origin/main
Remove the upstream information for a branch.
git branch --unset-upstream
Only list branches which contain the specified commit.
git branch --contains v2.0
Only list branches whose tips are reachable from the specified commit (default: HEAD).
git branch --merged
Only list branches whose tips are not reachable from the specified commit.
git branch --no-merged

Examples

Start development from a known tag

# Clone the repository
git clone git://git.kernel.org/pub/scm/.../linux-2.6 my2.6
cd my2.6

# Create a new branch from a tag
git branch my2.6.14 v2.6.14

# Switch to the new branch
git switch my2.6.14

Delete branches

# Delete remote-tracking branches
git branch -d -r origin/todo origin/html origin/man

# Force delete a local branch
git branch -D test

List branches from a specific remote

# Using branch with pattern
git branch -r -l 'origin/*'

# Using for-each-ref (more flexible)
git for-each-ref 'refs/remotes/origin/*'

Working with tracking information

# Create a branch with tracking
git branch --track feature origin/feature

# Set upstream for existing branch
git branch --set-upstream-to=origin/main main

# Remove upstream
git branch --unset-upstream feature

Find branches with specific commits

# Find branches containing a specific commit
git branch --contains abc123

# Find branches that are fully merged into main
git branch --merged main

# Find branches not yet merged
git branch --no-merged main
If you are creating a branch that you want to switch to immediately, it is easier to use the git switch command with its -c option to do the same thing with a single command:
git switch -c new-branch

Build docs developers (and LLMs) love