git-branch
Manage, list, and create Git branches
TLDR
List all branches (local and remote; the current branch is highlighted by *)
List which branches include a specific Git commit in their history
Show the name of the current branch
Create new branch based on the current commit
Create new branch based on a specific commit
Rename a branch (you must switch to a different branch before doing this)
Delete a local branch (you must switch to a different branch before doing this)
Delete a remote branch
SYNOPSIS
git branch [-v | --verbose] [--abbrev=<n>] [-a | --all] [--merged [<commit>]] [--no-merged [<commit>]] [--contains <commit>] [--no-contains <commit>] [-d | -D | --delete | --force] [--move | --force --move] [--copy | --force --copy] [--list] [-r | --remotes] [--show-current] [--create-reflog] [--edit-description] [-i | --ignore-case] [-q | --quiet] [--sort=<key>] [--points-at <object>] [--format=<format>] [--column [<options>]] [--no-column] [--color[=<when>]] [--no-color] [--show-upstream] [--no-show-upstream] [--track] [--no-track] [--set-upstream-to=<upstream>] [--unset-upstream] <branchname> [<start-point>]
PARAMETERS
<branchname>
The name of the branch to create, rename, or delete. If no branch name is given, git branch
will list existing branches.<start-point>
The commit, branch, or tag to base the new branch on. If omitted, defaults to HEAD
.-a, --all
List all branches, both local and remote.-r, --remotes
List only remote branches.-d, --delete
Delete a branch. The branch must be fully merged into the current branch.-D
Force delete a branch, even if it is not fully merged.-m, --move
Rename a branch.-c, --copy
Copy a branch.-v, --verbose
Show the last commit on each branch.--merged [<commit>]
List branches that are merged into the current branch or the specified commit.--no-merged [<commit>]
List branches that are not merged into the current branch or the specified commit.--show-current
Show the name of the current branch.--set-upstream-to=<upstream>
Sets up tracking information for the current branch. This option is useful for associating a local branch with a remote branch, allowing you to use commands like git pull
and git push
without specifying the remote.--unset-upstream
Unsets the upstream branch for the current branch.
DESCRIPTION
The git branch
command is a powerful tool for managing branches in Git repositories. It allows you to list existing branches, create new branches, rename branches, and delete branches. Branches are essential for isolating different lines of development, enabling parallel work on features, bug fixes, and experiments without disrupting the main codebase. When used without any arguments, git branch
lists all local branches, highlighting the currently checked-out branch with an asterisk (*). With options, you can view remote branches, delete branches, and perform more advanced branch management tasks.
Branching in Git is lightweight and efficient, encouraging developers to create branches frequently for different purposes. This contributes to a collaborative and organized development workflow, making it easier to manage complex projects and track changes.
CAVEATS
Deleting a branch that contains unmerged changes can lead to data loss. Always double-check before using the -D
option. Renaming the current branch can sometimes lead to unexpected behavior in scripting.
BRANCHING STRATEGIES
Git supports various branching strategies, such as Gitflow, GitHub Flow, and GitLab Flow. These strategies define how branches are used to manage different aspects of the development process, such as feature development, bug fixes, and releases. Understanding these strategies can help you optimize your team's workflow.
git branch plays a key role in enabling the development and execution of most branching strategies.
REMOTE BRANCH TRACKING
Tracking remote branches allows you to easily synchronize your local branches with their counterparts on a remote repository. The --track
option or --set-upstream-to
are very helpful. You can use git branch -vv
to display the tracking information for all local branches.
HISTORY
The git branch
command has been a core part of Git since its inception. It was initially designed to provide a simple and efficient way to manage parallel development efforts. Over time, the command has been extended with various options to support more complex branching workflows, such as tracking remote branches, filtering branches based on merge status, and showing the current branch name. The command has evolved to become an indispensable tool for developers working with Git.
SEE ALSO
git checkout(1), git merge(1), git remote(1), git push(1), git pull(1)