git branch

Branching and Merging

List, create, or delete branches


The 'git branch' command lets you create, list, rename, and delete branches. It does not let you switch between branches - use 'git switch' or 'git checkout' for that. Branches in Git are lightweight pointers to commits, making branch creation and switching extremely fast. When called without arguments, git branch lists existing branches with the current branch highlighted. When a branch name is provided, it creates a new branch pointing to the current HEAD. Branches are essential for parallel development, allowing multiple features or fixes to be developed simultaneously without interfering with each other.

Synopsis

git branch [--list] [-a | -r] [-v [--abbrev=<n> | --no-abbrev]] [--merged | --no-merged] [<pattern>...]
git branch <branch-name> [<start-point>]
git branch (-d | -D) [-r] <branch-name>...
git branch (-m | -M) [<old-branch>] <new-branch>
git branch (-c | -C) [<old-branch>] <new-branch>
git branch [--set-upstream-to=<upstream> | -u <upstream>] [<branch-name>]
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (7)

List branches matching shell wildcard pattern(s). Without pattern, lists all local branches. The --list flag is required when filtering - otherwise Git interprets the pattern as a new branch name to create.

RecommendedNeeded to filter by pattern; without it a pattern creates a branch

Example

git branch --list "feature-*"

List both remote-tracking branches and local branches. Shows all branches that exist locally and all remote branches your repository knows about.

RecommendedSee local and remote-tracking branches together after a fetch

Example

git branch -a

List only the remote-tracking branches. These are local references (refs/remotes/...) that represent the state of branches on remote repositories at the time of the last fetch.

When neededLists remote-tracking branches only; run git fetch first

Example

git branch -r

Delete a branch. The branch must be fully merged in its upstream branch, or in HEAD if no upstream was set. This is a safe delete that protects against losing work.

RecommendedSafe cleanup: refuses if the branch has unmerged work

Example

git branch -d feature-complete

Move/rename a branch together with its config and reflog. If you're on the branch, you can omit the old name.

RecommendedRenames a branch and keeps its reflog; fails safely if the name exists

Example

git branch -m old-name new-name

Show the hash and subject of each branch, plus how far it is ahead of or behind its upstream. Use -vv to also print the upstream branch name (and the path of a linked worktree).

Recommended-vv shows the upstream and ahead/behind counts at a glance

Example

git branch -v

Print the name of the current branch. In detached HEAD state, nothing is printed. Clean output perfect for scripts and automation.

When neededPrints just the branch name; handy in scripts and prompts

Example

git branch --show-current

Intermediate (10)

Force delete a branch even if it has unmerged changes. Shortcut for --delete --force. Use with caution as commits may become unreachable.

Not recommendedDeletes unmerged work — only when you are sure the commits are disposable

Example

git branch -D abandoned-feature

Force rename a branch, overwriting the target if it already exists. Shortcut for --move --force. The overwritten branch is lost.

Not recommendedSilently overwrites an existing branch; use -m unless replacing on purpose

Example

git branch -M main master

Copy a branch together with its config and reflog. Creates a new branch at the same commit as the source. The original branch is preserved (unlike --move).

When neededDuplicates a branch with its config; rarely needed day to day

Example

git branch -c feature feature-backup

List branches whose tips are reachable from the specified commit (HEAD if not specified). These branches have been fully incorporated and are safe to delete with -d.

RecommendedShows fully merged branches that are safe to delete

Example

git branch --merged main

Only list branches whose tips are NOT reachable from the specified commit (HEAD if not specified). These branches contain work that hasn't been integrated yet and need attention before deletion.

When neededShows branches with work not yet integrated

Example

git branch --no-merged

Set up the tracking relationship between a local branch and a remote branch. This allows git pull/push to work without specifying remote and branch names.

RecommendedLinks a branch to its remote so plain pull and push work

Example

git branch -u origin/main

Remove the upstream tracking information for a branch. After this, git pull and git push will require explicit remote/branch arguments.

When neededRemoves the tracking link, e.g. after the remote branch is gone

Example

git branch --unset-upstream

Create a new local branch that tracks a remote branch. The new branch will have upstream configured automatically for git pull/push.

When neededSets upstream at creation; already the default from a remote branch

Example

git branch --track feature origin/feature

Only list branches whose tip commits are descendants of (contain) the specified commit. Useful to find which branches have a specific bugfix or feature. Defaults to HEAD if no commit specified.

When neededFinds which branches already include a given fix or commit

Example

git branch --contains a1b2c3d

Sort branches based on the given key. Use - prefix for descending order. Common keys: committerdate, authordate, refname.

When needed--sort=-committerdate shows recently active branches first

Example

git branch --sort=-committerdate

Choosing the Right Option

Viewing Branches

Different ways to list and inspect branches

CommandShows LocalShows RemoteExtra InfoUse Case
git branchYesNoCurrent branch marked *Quick check of local branches
git branch -rNoYesRemote tracking refsSee what's on the server
git branch -aYesYesAll refsFull picture of all branches
git branch -vYesNoLast commit per branchSee branch activity at a glance
git branch -vvYesNoUpstream + ahead/behindCheck sync status with remote
git branch --show-currentCurrent onlyNoJust the nameScripts, shell prompts

Deleting Branches

Safe delete vs force delete - know the difference

CommandRequires MergedCan Lose WorkWhen to Use
-d (--delete)YesNoAfter PR merged, routine cleanup
-D (--delete --force)NoYesAbandoning experimental work

Renaming & Copying Branches

Options for reorganizing your branch names

CommandOverwrites ExistingKeeps OriginalTypical Use
-m (--move)No (fails)NoRename: feature → feature-v2
-M (--move --force)YesNoReplace existing branch name
-c (--copy)No (fails)YesDuplicate branch for experimentation
-C (--copy --force)YesYesCopy, overwrite if exists

Filtering Branches by Merge Status

Find branches ready to delete or still in progress

CommandShowsPurposeCommon Follow-up
--mergedBranches merged into HEADFind cleanup candidatesgit branch -d <branch>
--merged mainBranches merged into mainFind branches safe to deleteBulk delete merged branches
--no-mergedBranches NOT mergedFind work in progressReview before release
--contains <commit>Branches containing commitTrack where a fix wentVerify hotfix deployment

Upstream Tracking

Configure the relationship between local and remote branches

CommandActionEffectWhen to Use
-u origin/mainSet upstreamgit push/pull work without argsAfter creating local branch
--track origin/featureCreate + set upstreamNew local tracking remoteStart working on remote branch
--unset-upstreamRemove upstreamDisconnect from remoteStop tracking deleted remote branch

Workflow Examples

1
git branch feature-login

Create a new branch for the feature

2
git switch feature-login

Switch to the new branch

3
(make changes and commits)

Work on the feature with multiple commits

4
git switch main && git merge feature-login

Merge completed feature back to main

5
git branch -d feature-login

Delete the feature branch after merging

1
git branch --merged

List branches that have been merged

2
git branch -d branch-name

Safely delete merged branches one by one

3
git fetch --prune

Remove stale remote-tracking references

4
git branch -r

Verify remote branches are clean

1
git fetch origin

Get latest remote branch info

2
git branch -r

List available remote branches

3
git branch --track feature origin/feature

Create local branch tracking the remote

4
git switch feature

Switch to the new tracking branch

1
git branch -m old-name new-name

Rename the local branch

2
git push origin -u new-name

Push new branch name to remote

3
git push origin --delete old-name

Delete old branch name from remote

1
git log --oneline -1

Get the commit hash you're looking for

2
git branch --contains <commit-hash>

Find local branches with this commit

3
git branch -r --contains <commit-hash>

Find remote branches with this commit

Troubleshooting

Cannot delete branch - not fully merged

Cause: The branch has commits not merged into its upstream (or into HEAD if it has no upstream)

Either merge the branch first, or use -D to force delete if you're sure you want to discard the work

git branch -D branch-name

Cannot delete current branch

Cause: You're trying to delete the branch you're currently on

Switch to a different branch first, then delete the target branch

git switch main && git branch -d old-branch

Branch name already exists

Cause: You're trying to create a branch with a name that already exists

Choose a different name, delete the existing branch first, or use -f to reset it to a new start point

git branch -f existing-name <start-point>

No upstream configured for push

Cause: The local branch doesn't have a tracking relationship with a remote branch

Set up the upstream tracking relationship

git push -u origin branch-name

Remote branch not showing in git branch -r

Cause: Local repository doesn't have the latest remote branch info

Fetch from the remote to update your remote-tracking branches

git fetch origin

Detached HEAD after checkout

Cause: You checked out a commit directly instead of a branch

Create a new branch at the current commit, or switch back to an existing branch

git switch -c new-branch-name

Branch tracking wrong remote

Cause: The upstream is configured to track an incorrect remote branch

Reset the upstream tracking to the correct remote branch

git branch -u origin/correct-branch

Cannot see all branches

Cause: Only local branches are shown by default

Use -a flag to show both local and remote-tracking branches

git branch -a

Tips & Pitfalls

Best Practices

  • ✓Use descriptive branch names that indicate the purpose: feature/user-auth, bugfix/login-error, hotfix/security-patch
  • ✓Delete branches after merging to keep the repository clean - use 'git branch --merged' to find safe deletions
  • ✓Always create feature branches from an up-to-date main branch to minimize merge conflicts
  • ✓Use 'git branch -vv' to see tracking relationships and ahead/behind status
  • ✓Set up tracking when creating branches with 'git push -u origin branch-name' for easier push/pull
  • ✓Avoid working directly on main/master - always create a branch for changes
  • ✓Use 'git branch --no-merged' to find branches with work still in progress
  • ✓Regularly sync remote branches with 'git fetch --prune' to remove stale references
  • ✓Consider using 'git switch' instead of 'git checkout' for clearer intent when switching branches
  • ✓Use '--sort=-committerdate' to see most recently active branches first
  • ✓Before deleting a branch with -D, verify you don't need it with 'git log branch-name'
  • ✓Keep branch names lowercase with hyphens for consistency and compatibility

Common Mistakes

  • ✗Expecting 'git branch <name>' to switch to the new branch. It only creates the branch - use 'git switch <name>' or 'git checkout -b <name>' to create and switch.
  • ✗Trying to delete the current branch. You must switch to another branch first before deleting the one you're on.
  • ✗Using -d on unmerged branches and losing work. The -d flag protects you - if you really want to delete unmerged work, use -D.
  • ✗Forgetting to push new branches to remote. Local branches aren't shared until you 'git push -u origin <branch>'.
  • ✗Not cleaning up old branches. Regularly use 'git branch --merged' to find and delete branches that have been merged.

FAQ

A:

'git branch <name>' only creates a new branch but doesn't switch to it. 'git checkout -b <name>' creates a new branch AND switches to it in one command. The modern equivalent is 'git switch -c <name>'.

A:

Use 'git branch -m old-name new-name' to rename a branch. If you're on the branch you want to rename, just use 'git branch -m new-name'. For remote branches, you'll need to push the new name and delete the old one.

A:

-d is a safe delete that only works if the branch is fully merged into its upstream (or HEAD if no upstream is set). -D is a force delete that removes the branch regardless of merge status. Use -D only when you're sure you want to discard unmerged commits.

A:

Use 'git push origin --delete branch-name' or the shorter syntax 'git push origin :branch-name'. This removes the branch from the remote repository.

A:

Remote-tracking branches (like origin/main) are local references that track the state of branches on remote repositories. They're updated when you fetch or pull, showing you where remote branches point.

A:

Use 'git branch --show-current' for just the name, or 'git branch' to see all branches with the current one marked with *. 'git status' also shows the current branch.

A:

Git doesn't allow deleting the branch you're currently on. Switch to another branch first with 'git switch other-branch', then delete the target branch.

A:

Use 'git branch branch-name commit-hash' to create a branch starting from any commit. You can use a commit hash, tag, or another branch name as the starting point.

A:

The upstream (or tracking) branch is the remote branch that your local branch is configured to push to and pull from by default. Set it with 'git branch -u origin/branch-name' or 'git push -u origin branch-name'.

A:

Use 'git branch --sort=-committerdate' to list branches with the most recently committed branches first. Add -v to also see the commit hash and message.