Start work from a release tag
Create a branch at a tag such as v2.6.14 to fix something in that release without touching your current branch.
Git · Command
git branch manages branches. It can list, create, rename, copy and delete them, and it can set or change the upstream branch each one follows.
Last updated
In Git, a branch is a named ref under refs/heads that points to a commit. With no name argument, git branch lists refs and accepts filters such as --merged, --contains, --sort and --format. Given a name, it creates a new ref at HEAD or at a start point you choose. A start point can be a branch, tag or commit ID, and A...B uses the merge base of A and B. If the start point is a remote-tracking branch, branch.<name>.remote and branch.<name>.merge are written by default, which lets pull and status know the upstream. Other flags rename, copy or delete refs, and renames and copies carry the branch's config and reflog with them.
git branch [--list] [-r|-a] [<pattern>...]
git branch [--track[=(direct|inherit)] | --no-track] [-f] <branch-name> [<start-point>]
git branch (--set-upstream-to=<upstream>|-u <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>...| Name | Type | Description |
|---|---|---|
<branch-name> | string (ref name) | Name of the branch to create or delete. It must pass the checks in git check-ref-format. |
<start-point> | commit-ish | The commit the new branch will point to, given as a branch name, commit ID or tag. If left out, HEAD is used. |
<old-branch> | string | An existing branch to rename or copy. If left out, the current branch is used. |
<new-branch> | string | The new name used by a rename or copy. It has the same naming rules as <branch-name>. |
<pattern> | shell glob | Used with --list to show only matching branches. A branch is shown if it matches any of the patterns. |
<commit> | commit-ish | The commit used by --merged, --no-merged, --contains and --no-contains. Defaults to HEAD. |
<upstream> | string | The branch to set as upstream with -u or --set-upstream-to. |
#!/usr/bin/env bash
set -euo pipefail
# Create a branch from the remote main, tracking it
git fetch origin
git branch --track feature/login origin/main
git switch feature/login
# Inspect: current branch, verbose list with upstreams
git branch --show-current
git branch -vv
# List only matching branches, newest first
git branch --list 'feature/*' --sort=-committerdate
# Rename the branch and point it at a new upstream
git branch -m feature/login feature/auth-login
git branch -u origin/main feature/auth-login
# After merging into main, clean up
git switch main
git branch --merged main
git branch -d feature/auth-loginCreate a branch at a tag such as v2.6.14 to fix something in that release without touching your current branch.
After a release, run git branch --merged main to list local branches that are already merged into main, then remove them with -d.
Before rebasing or amending a commit, run git branch --contains <sha> to find every branch that includes it and will need attention.
Use git branch -u <remote>/<branch> to point an existing local branch at a new upstream so pull and status work again.
When the start point is a remote-tracking branch, git branch records branch.<name>.remote and branch.<name>.merge in the config. This happens because branch.autoSetupMerge defaults to true. You can change this per command with --track or --no-track, or later with --set-upstream-to.
Follow-up: What is the difference between --track=direct and --track=inherit? With direct, the start-point branch itself becomes the upstream. With inherit, the new branch copies the start point's own upstream configuration.
--merged <commit> lists branches whose tips can be reached from that commit, meaning their work is already included in it. --contains <commit> lists branches whose tips descend from that commit, meaning the branch includes it. Both default to HEAD when no commit is given, and both turn on list mode.
Follow-up: How are several --merged and --no-merged filters combined? A ref is shown only if it can be reached from at least one --merged commit and from none of the --no-merged commits.
No. It only deletes your local remote-tracking ref. The manual says this is only worth doing if the branch no longer exists on the remote, or if fetch has been set up not to fetch it.
Follow-up: How do you clean up all stale remote-tracking branches at once? Use the prune subcommand of git remote, as the git-branch manual suggests.
git switchgit checkoutgit mergegit for-each-refgit check-ref-formatgit remotegit fetchgit pull