LinuxCommandLibrary

git-branch

TLDR

List all local branches

$ git branch
copy
List all branches including remote
$ git branch -a
copy
Create a new branch
$ git branch [branch_name]
copy
Create and switch to a new branch
$ git branch [branch_name] && git checkout [branch_name]
copy
Delete a local branch
$ git branch -d [branch_name]
copy
Force delete a branch with unmerged changes
$ git branch -D [branch_name]
copy
Rename the current branch
$ git branch -m [new_name]
copy
Set upstream tracking branch
$ git branch -u [origin/branch_name]
copy
Show branches with last commit
$ git branch -v
copy

SYNOPSIS

git branch [OPTIONS] [BRANCH-NAME] [START-POINT]

DESCRIPTION

git branch lists, creates, renames, and deletes branches. Without arguments, it lists existing local branches, marking the current branch with an asterisk.
Branches are lightweight pointers to commits, allowing parallel development workflows. Creating a branch does not switch to it; use git checkout or git switch to change branches.
Remote-tracking branches (origin/main, etc.) are read-only references to the state of branches on remote repositories. They are updated by git fetch.

PARAMETERS

-a, --all

List both local and remote-tracking branches.
-r, --remotes
List remote-tracking branches only.
-d, --delete
Delete a branch (must be fully merged).
-D
Force delete a branch regardless of merge status.
-m, --move
Rename a branch.
-M
Force rename even if target name exists.
-c, --copy
Copy a branch.
-u, --set-upstream-to=UPSTREAM
Set upstream tracking branch.
--unset-upstream
Remove upstream tracking information.
-v, --verbose
Show SHA1 and commit subject for each branch.
--merged
List branches merged into current branch.
--no-merged
List branches not merged into current branch.
--contains COMMIT
List branches containing the specified commit.

CAVEATS

Deleting a branch with -d fails if the branch has unmerged changes. Use -D to force deletion, but unmerged work will be lost (though commits remain recoverable via reflog temporarily). Cannot delete the currently checked-out branch.

HISTORY

Branching has been a core Git feature since its creation by Linus Torvalds in 2005. Git's lightweight branching model, where branches are simply pointers to commits rather than full directory copies, was revolutionary and enabled workflows like GitFlow and GitHub Flow.

SEE ALSO

Copied to clipboard