Skip to content

Rebase and Clean History

bparlaker edited this page Oct 6, 2026 · 2 revisions

Rebase and Clean History

Merge vs. Rebase

Both integrate changes from one branch into another:

            merge                               rebase
A---B---E---M (main)             A---B---E (main)
     \     /                               \
      C---D (feature)                       C'---D' (feature)
  • merge keeps the true history, adds a merge commit.
  • rebase replays your commits on top of the new base → a straight line. The commits get new hashes (C', D').
git switch feature
git rebase main            # replay feature on top of latest main
# on conflict: fix files, git add, git rebase --continue   (or --abort)

Interactive rebase - tidy up before sharing

git rebase -i HEAD~4

Git opens an editor:

pick a41c7e2 docs: add labels page
pick 9d2b113 typo
pick 3b9f0d1 docs: explain colours
pick 0cc8a17 wip

Change the verbs:

pick   a41c7e2 docs: add labels page
fixup  9d2b113 typo              # melt into previous, drop message
reword 3b9f0d1 docs: explain colours
drop   0cc8a17 wip

Result: 2 clean, meaningful commits instead of 4 noisy ones.

cherry-pick - copy one commit

git switch main
git cherry-pick 3b9f0d1      # apply just that commit here

bisect - binary-search for the commit that broke something

git bisect start
git bisect bad                 # current version is broken
git bisect good v1.0           # this tag was fine
# Git checks out a midpoint; test it, then:
git bisect good   # or: git bisect bad
# ...repeat ~log2(n) times...
git bisect reset

With 1,000 commits between good and bad, bisect finds the culprit in about 10 steps. It can even run automatically: git bisect run ./test.sh.

Handy aliases

git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.st "status -sb"
git config --global alias.undo "reset --soft HEAD~1"

Clone this wiki locally