Skip to content
Latchkey

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

CommandDoes
git switch -c nameCreate + switch
git switch nameSwitch existing
git switch -Back to previous branch
git branch -aList all (incl. remote)
git branch -m old newRename

Merge & rebase

CommandDoes
git merge featureMerge into current branch
git merge --no-ff featureForce a merge commit
git rebase mainReplay commits onto main
git rebase -i HEAD~3Squash / edit last 3
git rebase --abortBail out of a rebase

Delete & sync

CommandDoes
git branch -d nameDelete merged branch
git branch -D nameForce-delete
git push origin --delete nameDelete remote branch
git pull --rebaseUpdate, replaying local commits

Merge vs rebase

ApproachResult
mergePreserves history, adds merge commit
rebaseLinear history, rewrites commit SHAs
fast-forwardMoves 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →