LinuxCommandLibrary

git-branch

Manage, list, and create Git branches

TLDR

List all branches (local and remote; the current branch is highlighted by *)

$ git branch [[-a|--all]]
copy

List which branches include a specific Git commit in their history
$ git branch [[-a|--all]] --contains [commit_hash]
copy

Show the name of the current branch
$ git branch --show-current
copy

Create new branch based on the current commit
$ git branch [branch_name]
copy

Create new branch based on a specific commit
$ git branch [branch_name] [commit_hash]
copy

Rename a branch (you must switch to a different branch before doing this)
$ git branch [[-m|--move]] [old_branch_name] [new_branch_name]
copy

Delete a local branch (you must switch to a different branch before doing this)
$ git branch [[-d|--delete]] [branch_name]
copy

Delete a remote branch
$ git push [remote_name] [[-d|--delete]] [remote_branch_name]
copy

SYNOPSIS

git branch [<options>] [<branchname>] [<start-point>]
git branch (-d | -D) [-r] <branch>…​
git branch (-m | -M) [<old-branch>] <new-branch>
git branch (-c | -C) [<old-branch>] <new-branch>

PARAMETERS

-a, --all
    List both remote-tracking and local branches

-d, --delete
    Delete fully merged branch (safe)

-D, --delete --force
    Force delete unmerged branch

-l, --list
    List branches matching pattern; implies --list if no other subcommand

-m, --move, --rename
    Rename branch; use -m <old> <new>

-M, --move-force
    Force rename even if new branch exists

-c, --copy
    Copy branch and reset new HEAD

-C, --copy-force
    Force copy even if target exists

-r, --remotes
    List or delete remote-tracking branches

-v, --verbose
    Show SHA1 and commit subject; -vv for upstream info

-q, --quiet
    Suppress progress reporting

--contains <commit>
    List branches containing specified commit

--no-contains <commit>
    List branches not containing commit

--merged [<commit>]
    List branches merged into commit (HEAD if omitted)

--no-merged [<commit>]
    List branches not merged into commit

--sort=<key>
    Sort branches by key (e.g., 'committerdate')

--format=<format>
    Format output (e.g., '%(refname:short)')

-t, --track
    Set up tracking for new branch

--no-track
    Do not set up tracking

-f, --force
    Force creation/overwrite

--color[=<when>]
    Color output (always, never, auto)

--no-color
    Turn off colors

-i, --ignore-case
    Sorting and filtering ignore case

--edit-description
    Open editor for branch description

--recurse-submodules
    Handle submodules in operations

-s, --strategy <strategy>
    Merge strategy for branch creation

DESCRIPTION

The git branch command is essential for managing branches in a Git repository. It allows listing local and remote branches, creating new branches from a specified start point, renaming existing branches, and deleting them safely or forcefully.

Without arguments, git branch displays a list of local branches, highlighting the current one with an asterisk (*). Use options like -v for verbose output showing the latest commit SHA and message, or -r for remote-tracking branches.

To create a branch: git branch <new-branch> [<start-point>], where start-point is typically a commit, tag, or another branch (defaults to current HEAD).

Deletion uses -d (safe, only if merged into current branch) or -D (force). Renaming employs -m <old> <new> or -M for force. Copying is via -c or -C.

Advanced filtering includes --merged or --contains to list branches meeting criteria. Sorting with --sort and custom formats via --format enhance output. Note: this command does not switch branches; pair with git checkout or git switch.

Ideal for branch-heavy workflows, it supports tracking remote branches with -t and handles submodules with --recurse-submodules.

CAVEATS

Does not switch branches; use git switch or git checkout. -D deletes unmerged work irreversibly. Remote deletes require -r and push to propagate.

COMMON EXAMPLES

git branch # List local branches
git branch newfeat # Create from HEAD
git branch -d oldfeat # Safe delete
git branch -m old new # Rename

TRACKING REMOTE

git branch --track myremote origin/myremote
Creates local branch tracking remote.

HISTORY

Introduced in Git 1.0.13 (April 2006) as core porcelain command. Evolved significantly: verbose tracking added in 1.6.0 (2008), sort/format in 1.7.10 (2012), copy options in 2.23 (2019). Remains fundamental in Git 2.x series.

SEE ALSO

Copied to clipboard