Branching & Merging Cheatsheet
Branches
| Command | Action |
|---|---|
git branch | List local branches |
git branch -a | List all branches (+ remote) |
git branch -v | List with last commit info |
git branch <name> | Create a branch |
git switch -c <name> | Create and switch to branch |
git switch <name> | Switch to existing branch |
git switch - | Switch to previous branch |
git branch -d <name> | Delete merged branch |
git branch -D <name> | Force-delete branch |
git branch -m <new> | Rename current branch |
git branch --merged | List merged branches |
git branch --no-merged | List unmerged branches |
Merging
| Command | Action |
|---|---|
git merge <branch> | Merge branch into current |
git merge --no-ff <branch> | Force merge commit |
git merge --squash <branch> | Squash all commits into one |
git merge --abort | Cancel an in-progress merge |
Rebasing
| Command | Action |
|---|---|
git rebase <branch> | Rebase current onto branch |
git rebase -i HEAD~N | Interactive rebase last N commits |
git rebase -i --autosquash | Auto-squash fixup commits |
git rebase --continue | Continue after resolving conflict |
git rebase --abort | Cancel the rebase |
git pull --rebase | Pull with rebase (no merge commit) |
Conflict Resolution
| Command | Action |
|---|---|
git status | Show conflicted files |
git checkout --ours <file> | Keep your version |
git checkout --theirs <file> | Keep their version |
git add <file> | Mark conflict as resolved |
git merge --abort | Cancel merge |
git rebase --abort | Cancel rebase |
Interactive Rebase Commands
| Key | Action |
|---|---|
pick | Keep commit as-is |
reword | Change commit message |
edit | Pause to modify commit |
squash | Merge into previous (keep message) |
fixup | Merge into previous (discard message) |
drop | Delete the commit |