Git Branching Cheat Sheet: Branches, Merge & Rebase
Branch, merge, rebase, and clean up - the Git branching workflow in one reference.
The commands and mental model for working with branches.
Create & switch
| Command | Does |
|---|
| git switch -c name | Create + switch |
| git switch name | Switch existing |
| git switch - | Back to previous branch |
| git branch -a | List all (incl. remote) |
| git branch -m old new | Rename |
Merge & rebase
| Command | Does |
|---|
| git merge feature | Merge into current branch |
| git merge --no-ff feature | Force a merge commit |
| git rebase main | Replay commits onto main |
| git rebase -i HEAD~3 | Squash / edit last 3 |
| git rebase --abort | Bail out of a rebase |
Delete & sync
| Command | Does |
|---|
| git branch -d name | Delete merged branch |
| git branch -D name | Force-delete |
| git push origin --delete name | Delete remote branch |
| git pull --rebase | Update, replaying local commits |
Merge vs rebase
| Approach | Result |
|---|
| merge | Preserves history, adds merge commit |
| rebase | Linear history, rewrites commit SHAs |
| fast-forward | Moves pointer, no new commit |
Key takeaways
- Rebase rewrites SHAs - never rebase a branch others have pulled.
- merge --no-ff keeps an explicit record of the feature merge.
- pull --rebase avoids noisy merge commits on shared branches.
Related guides
Git Cheat Sheet: Everyday Commands and RecoveryA Git cheat sheet - branching, committing, rebasing, stashing, undoing mistakes, and the recovery commands th…
Git CLI Cheat Sheet: Config, Remotes, Stash & TagsA Git CLI cheat sheet - config, remotes, stash, tags, cherry-pick, bisect, and the plumbing commands beyond t…
Semantic Versioning Cheat Sheet: SemVer Rules & RangesA semantic versioning cheat sheet - MAJOR.MINOR.PATCH rules, prerelease and build metadata, and the npm range…