Git Branch Cheatsheet — Create, Merge & Manage Branches
Branching is the most powerful feature of Git. This guide covers everything from creating your first branch to handling complex merges and rebases — the foundation of any collaborative workflow.
Quick Reference
| Scenario | Command |
| List local branches | git branch |
| List all branches (including remote) | git branch -a |
| Create and switch to a new branch | git checkout -b feature/login |
| Create (no switch) | git branch feature/login |
| Switch to existing branch | git checkout main or git switch main |
| Rename a branch | git branch -m old-name new-name |
| Delete a merged branch | git branch -d feature/done |
| Force delete unmerged branch | git branch -D feature/abandoned |
| Merge a branch into current | git merge feature/login |
| Rebase current branch onto another | git rebase main |
Core Commands
### Creating & Switching Branches
``bash
# Create a new branch (stays on current branch)
git branch feature/new-widget
# Create and switch immediately git checkout -b feature/new-widget
# Same with the newer git switch command git switch -c feature/new-widget
# Switch to an existing branch
git checkout main
git switch main # newer alternative
`
### Listing Branches
`bash
# Local branches only
git branch
# All branches (including remote tracking) git branch -a
# Show last commit on each branch git branch -v
# Show merged branches (safe to delete)
git branch --merged
`
### Merging
`bash
# Standard merge (creates merge commit)
git checkout main
git merge feature/login
# Fast-forward only (abort if not possible) git merge --ff-only feature/login
# Squash merge (flatten all commits into one)
git merge --squash feature/login
`
### Rebasing
`bash
# Rebase current branch onto main
git checkout feature/login
git rebase main
# Interactive rebase (squash, reorder, edit commits) git rebase -i HEAD~3
# Abort a rebase that went wrong
git rebase --abort
`
### Deleting Branches
`bash
# Delete a fully merged branch
git branch -d feature/complete
# Force delete (even if not merged) git branch -D feature/abandoned
# Delete remote branch git push origin --delete feature/complete ``
Branch(17)
| Command | Level | ||
|---|---|---|---|
git branchList, create, or delete branches | Basic | git branch feature-login | |
git branch -dDelete a merged branch | Basic | git branch -d feature-login | |
git branch -DForce delete a branch regardless of merge status | Basic | git branch -D feature-login | |
git branch -aList all local and remote branches | Basic | git branch -a | |
git branch -rList remote branches | Basic | git branch -r | |
git branch --mergedList branches already merged into the current branch | Basic | git branch --merged | |
git branch --no-mergedList branches not yet merged into the current branch | Basic | git branch --no-merged | |
git branch --containsList branches that contain a specific commit | Intermediate | git branch --contains abc123 | |
git branch --show-currentShow the current branch name | Basic | git branch --show-current | |
git branch -mRename a branch | Basic | git branch -m old-name new-name | |
git branch --moveRename a branch (long option form) | Basic | git branch --move old-name new-name | |
git branch -vvShow branch tracking relationships | Intermediate | git branch -vv | |
git branch --edit-descriptionEdit the description of a branch | Intermediate | git branch --edit-description | |
git switchSwitch branches | Basic | git switch feature-login | |
git checkout -bCreate and switch to a new branch | Basic | git checkout -b feature-login | |
git mergeJoin two or more development histories together | Intermediate | git merge feature-login | |
git rebaseReapply commits on top of another base tip | Expert | git rebase main |