git branch
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>]Options
Choosing the Right Option
Viewing Branches
Different ways to list and inspect branches
| Command | Shows Local | Shows Remote | Extra Info | Use Case |
|---|---|---|---|---|
git branch | Yes | No | Current branch marked * | Quick check of local branches |
git branch -r | No | Yes | Remote tracking refs | See what's on the server |
git branch -a | Yes | Yes | All refs | Full picture of all branches |
git branch -v | Yes | No | Last commit per branch | See branch activity at a glance |
git branch -vv | Yes | No | Upstream + ahead/behind | Check sync status with remote |
git branch --show-current | Current only | No | Just the name | Scripts, shell prompts |
Deleting Branches
Safe delete vs force delete - know the difference
| Command | Requires Merged | Can Lose Work | When to Use |
|---|---|---|---|
-d (--delete) | Yes | No | After PR merged, routine cleanup |
-D (--delete --force) | No | Yes | Abandoning experimental work |
Renaming & Copying Branches
Options for reorganizing your branch names
| Command | Overwrites Existing | Keeps Original | Typical Use |
|---|---|---|---|
-m (--move) | No (fails) | No | Rename: feature → feature-v2 |
-M (--move --force) | Yes | No | Replace existing branch name |
-c (--copy) | No (fails) | Yes | Duplicate branch for experimentation |
-C (--copy --force) | Yes | Yes | Copy, overwrite if exists |
Filtering Branches by Merge Status
Find branches ready to delete or still in progress
| Command | Shows | Purpose | Common Follow-up |
|---|---|---|---|
--merged | Branches merged into HEAD | Find cleanup candidates | git branch -d <branch> |
--merged main | Branches merged into main | Find branches safe to delete | Bulk delete merged branches |
--no-merged | Branches NOT merged | Find work in progress | Review before release |
--contains <commit> | Branches containing commit | Track where a fix went | Verify hotfix deployment |
Upstream Tracking
Configure the relationship between local and remote branches
| Command | Action | Effect | When to Use |
|---|---|---|---|
-u origin/main | Set upstream | git push/pull work without args | After creating local branch |
--track origin/feature | Create + set upstream | New local tracking remote | Start working on remote branch |
--unset-upstream | Remove upstream | Disconnect from remote | Stop tracking deleted remote branch |
Workflow Examples
git branch feature-loginCreate a new branch for the feature
git switch feature-loginSwitch to the new branch
(make changes and commits)Work on the feature with multiple commits
git switch main && git merge feature-loginMerge completed feature back to main
git branch -d feature-loginDelete the feature branch after merging
git branch --mergedList branches that have been merged
git branch -d branch-nameSafely delete merged branches one by one
git fetch --pruneRemove stale remote-tracking references
git branch -rVerify remote branches are clean
git fetch originGet latest remote branch info
git branch -rList available remote branches
git branch --track feature origin/featureCreate local branch tracking the remote
git switch featureSwitch to the new tracking branch
git branch -m old-name new-nameRename the local branch
git push origin -u new-namePush new branch name to remote
git push origin --delete old-nameDelete old branch name from remote
git log --oneline -1Get the commit hash you're looking for
git branch --contains <commit-hash>Find local branches with this commit
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-nameCannot 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-branchBranch 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-nameRemote 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 originDetached 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-nameBranch 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-branchCannot see all branches
Cause: Only local branches are shown by default
Use -a flag to show both local and remote-tracking branches
git branch -aTips & 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
'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>'.
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.
-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.
Use 'git push origin --delete branch-name' or the shorter syntax 'git push origin :branch-name'. This removes the branch from the remote repository.
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.
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.
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.
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.
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'.
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.