git branch Command Guide
List, create, or delete branches
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
List
git branchList local branchesgit branch -rList remote branchesgit branch -aList all branchesCreate
git branch <name>Create branchgit branch <name> <commit>Branch from commitgit checkout -b <name>Create and switchDelete
git branch -d <name>Delete merged branchgit branch -D <name>Force deletegit push -d origin <name>Delete remoteRename
git branch -m <new>Rename currentgit branch -m <old> <new>Rename specificDownloadable Image Preview
Basic Usage
The git branch command manages branches in your repository. Branches are lightweight pointers to commits, making Git's branching model fast and efficient.
# List all local branches
git branch
# Create a new branch
git branch feature-login
# Delete a branch
git branch -d feature-loginListing Branches
# List local branches (* indicates current)
git branch
# List remote branches
git branch -r
# List all branches (local and remote)
git branch -a
# List with more details (last commit)
git branch -v
# List branches merged into current branch
git branch --merged
# List branches not yet merged
git branch --no-mergedCreating Branches
# Create a new branch (stay on current branch)
git branch feature-x
# Create branch from specific commit
git branch hotfix abc1234
# Create branch from another branch
git branch feature-y develop
# Create and switch in one command
git checkout -b feature-x
# Or using git switch (Git 2.23+)
git switch -c feature-x
# Create branch tracking a remote
git branch --track feature origin/featureDeleting Branches
# Delete a merged branch
git branch -d feature-done
# Force delete (even if not merged)
git branch -D abandoned-feature
# Delete remote branch
git push origin --delete feature-done
# Clean up stale remote-tracking branches
git remote prune origin
# Or during fetch
git fetch --prune-D to force delete an unmerged branch will permanently lose any commits not merged elsewhere. Use with caution.Renaming Branches
# Rename current branch
git branch -m new-name
# Rename any branch
git branch -m old-name new-name
# Rename and update remote
git branch -m old-name new-name
git push origin --delete old-name
git push -u origin new-namePractical Examples
# Feature branch workflow
git checkout -b feature/user-auth
# ... make changes and commits ...
git checkout main
git merge feature/user-auth
git branch -d feature/user-auth
# Clean up merged branches
git branch --merged | grep -v "\*\|main\|develop" | xargs git branch -d
# See branch differences
git log main..feature-x
# Copy current branch
git branch backup-before-refactor
# Set upstream tracking
git branch -u origin/mainFrequently Asked Questions
What is the difference between git branch and git checkout -b?
"git branch <name>" creates a branch but stays on current branch. "git checkout -b <name>" creates and switches to the new branch in one command.
How do I delete a remote branch?
Use "git push origin --delete <branch-name>" or "git push origin :<branch-name>" to delete a branch from the remote repository.
What is the difference between -d and -D when deleting?
-d only deletes if the branch is fully merged. -D force deletes regardless of merge status. Use -D carefully as you may lose unmerged work.
How do I see which branches are merged?
Use "git branch --merged" to see branches merged into current branch, or "git branch --no-merged" to see unmerged branches.
How do I track a remote branch?
Use "git branch --track <branch> origin/<branch>" or "git checkout --track origin/<branch>" to create a local branch that tracks a remote.
Summary
git branch is essential for managing parallel development. Use branches liberally - they're cheap in Git.
Quick Reference
git branch- List branchesgit branch <name>- Create branchgit branch -d <name>- Delete merged branchgit branch -m <new>- Rename current branch
Official Documentation
For authoritative information, refer to the official documentation: