git branch [--color[=<when>] | --no-color] [--show-current] [-v [--abbrev=<n> | --no-abbrev]] [--column[=<options>] | --no-column] [--sort=<key>] [--merged [<commit>]] [--no-merged [<commit>]] [--contains [<commit>]] [--no-contains [<commit>]] [--points-at <object>] [--format=<format>] [(-r | --remotes) | (-a | --all)] [--list] [<pattern>...:] git branch [--track | --no-track] [-f] <branchname> [<start-point>] git branch (--set-upstream-to=<upstream> | -u <upstream>) [<branchname>] git branch --unset-upstream [<branchname>] git branch (-m | -M) [<oldbranch>] <newbranch> git branch (-c | -C) [<oldbranch>] <newbranch> git branch (-d | -D) [-r] <branchname>...: git branch --edit-description [<branchname>]
If --list is given, or if there are no non-option arguments, existing branches are listed; the current branch will be highlighted in green and marked with an asterisk. Any branches checked out in linked worktrees will be highlighted in cyan and marked with a plus sign. Option -r causes the remote-tracking branches to be listed, and option -a shows both local and remote branches.
If a <pattern> is given, it is used as a shell wildcard to restrict the output to matching branches. If multiple patterns are given, a branch is shown if it matches any of the patterns.
Note that when providing a <pattern>, you must use --list; otherwise the command may be interpreted as branch creation.
With --contains, shows only the branches that contain the named commit (in other words, the branches whose tip commits are descendants of the named commit), --no-contains inverts it. With --merged, only branches merged into the named commit (i.e. the branches whose tip commits are reachable from the named commit) will be listed. With --no-merged only branches not merged into the named commit will be listed. If the <commit> argument is missing it defaults to HEAD (i.e. the tip of the current branch).
The command's second form creates a new branch head named <branchname> which points to the current HEAD, or <start-point> if given. As a special case, for <start-point>, you may use "A...B" as a shortcut for the merge base of A and B if there is exactly one merge base. You can leave out at most one of A and B, in which case it defaults to HEAD.
Note that this will create the new branch, but it will not switch the working tree to it; use "git switch <newbranch>" to switch to the new branch.
When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the branch.<name>.remote and branch.<name>.merge configuration entries) so that git pull will appropriately merge from the remote-tracking branch. This behavior may be changed via the global branch.autoSetupMerge configuration flag. That setting can be overridden by using the --track and --no-track options, and changed later using git branch --set-upstream-to.
With a -m or -M option, <oldbranch> will be renamed to <newbranch>. If <oldbranch> had a corresponding reflog, it is renamed to match <newbranch>, and a reflog entry is created to remember the branch renaming. If <newbranch> exists, -M must be used to force the rename to happen.
The -c and -C options have the exact same semantics as -m and -M, except instead of the branch being renamed, it will be copied to a new name, along with its config and reflog.
With a -d or -D option, <branchname> will be deleted. You may specify more than one branch for deletion. If the branch currently has a reflog then the reflog will also be deleted.
Use -r together with -d to delete remote-tracking branches. Note, that it only makes sense to delete remote-tracking branches if they no longer exist in the remote repository or if git fetch was configured not to fetch them again. See also the prune subcommand of git-remote(1) for a way to clean up all obsolete remote-tracking branches.
-d, --delete
-D
--create-reflog
-f, --force
-m, --move
-M
-c, --copy
-C
--color[=<when>]
--no-color
-i, --ignore-case
--column[=<options>], --no-column
This option is only applicable in non-verbose mode.
-r, --remotes
-a, --all
-l, --list
--show-current
-v, -vv, --verbose
-q, --quiet
--abbrev=<n>
--no-abbrev
-t, --track
This behavior is the default when the start point is a remote-tracking branch. Set the branch.autoSetupMerge configuration variable to false if you want git switch, git checkout and git branch to always behave as if --no-track were given. Set it to always if you want this behavior when the start-point is either a local or remote-tracking branch.
--no-track
--set-upstream
-u <upstream>, --set-upstream-to=<upstream>
--unset-upstream
--edit-description
--contains [<commit>]
--no-contains [<commit>]
--merged [<commit>]
--no-merged [<commit>]
<branchname>
<start-point>
<oldbranch>
<newbranch>
--sort=<key>
--points-at <object>
--format <format>
pager.branch is only respected when listing branches, i.e., when --list is used or implied. The default is to use a pager. See git-config(1).
Start development from a known tag
$ git clone git://git.kernel.org/pub/scm/.../linux-2.6 my2.6 $ cd my2.6 $ git branch my2.6.14 v2.6.14 (1) $ git switch my2.6.14
1. |
This step and the next one could be combined into a single step with "checkout -b my2.6.14 v2.6.14".
|
Delete an unneeded branch
$ git clone git://git.kernel.org/.../git.git my.git $ cd my.git $ git branch -d -r origin/todo origin/html origin/man (1) $ git branch -D test (2)
1. |
Delete the remote-tracking branches "todo", "html" and "man". The next
fetch
or
pull
will create them again unless you configure them not to. See
git-fetch(1).
|
2. |
Delete the "test" branch even if the "master" branch (or whichever branch is currently checked out) does not have all commits from the test branch.
|
Listing branches from a specific remote
$ git branch -r -l '<remote>/<pattern>' (1) $ git for-each-ref 'refs/remotes/<remote>/<pattern>' (2)
1. |
Using
-a
would conflate <remote> with any local branches you happen to have been prefixed with the same <remote> pattern.
|
2. |
for-each-ref
can take a wide range of options. See
git-for-each-ref(1)
|
Patterns will normally need quoting.
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.
The options --contains, --no-contains, --merged and --no-merged serve four related but different purposes:
When combining multiple --contains and --no-contains filters, only references that contain at least one of the --contains commits and contain none of the --no-contains commits are shown.
When combining multiple --merged and --no-merged filters, only references that are reachable from at least one of the --merged commits and from none of the --no-merged commits are shown.
git-check-ref-format(1), git-fetch(1), git-remote(1), m[blue]"Understanding history: What is a branch?"m[][1] in the Git User's Manual.
Part of the git(1) suite