Show contents
Chapter 4 of 6 · 20 min
Merge, rebase and undo
By the end of this chapter, you'll be able to
- Compare a merge and a rebase on the same history
- Apply the golden rule: never rewrite history that is already shared
- Undo a published commit with git revert
- Recover "lost" work thanks to the reflog
Two diverging branches
Let's start from a repository where main and feat/contact have each moved forward on their own since the home page commit:
git init -q
echo "Home" > index.html
git add index.html
git commit -q -m "feat: home page"
git switch -q -c feat/contact
echo "Contact" > contact.html
git add contact.html
git commit -q -m "feat: contact page"
git switch -q main
echo "Prices" > prices.html
git add prices.html
git commit -q -m "feat: prices page"
git log --oneline main
git log --oneline feat/contact
1ec4073 feat: prices page
787554d feat: home page
815f8f8 feat: contact page
787554d feat: home page
The two branches share the first commit, then diverge. The -q (quiet) option silences the commands so that only the logs are shown. There are two ways to bring this work together.
Merge: keeping history as it happened
git switch -q -c merge-trial
git merge -q --no-edit feat/contact
git log --oneline --graph
* d23ec6e Merge branch 'feat/contact' into merge-trial
|\
| * 815f8f8 feat: contact page
* | 1ec4073 feat: prices page
|/
* 787554d feat: home page
The merge creates a merge commit with two parents. Nothing is rewritten: the history faithfully shows that two lines of work existed in parallel. We work here on a trial branch, merge-trial, so we can compare with the other method.
Rebase: replaying your commits on top of main
git switch -q feat/contact
git rebase -q main
git log --oneline --graph
* 4cae75f feat: contact page
* 1ec4073 feat: prices page
* 787554d feat: home page
The rebase takes your branch's commits and replays them one by one on top of main, as if you had started your work today. The history becomes a straight line. But look at the identifier: the "contact page" commit was called 815f8f8, it is now called 4cae75f. It is not the same commit: it is a copy, with a different parent. The rebase rewrites history.
The history is linear; the old contact commit is no longer on any branch.
Text description of the animation
- Both branches have moved forward since the home page commit.
- Git finds the commits of your branch that are not in main.
- Your branch starts again from main's latest commit.
- Each commit is recreated with the same content but a new parent, hence a new identifier.
- The history is linear; the old contact commit is no longer on any branch.
Which one to choose?
| Merge | Rebase | |
|---|---|---|
| History | faithful, with branches | linear, easier to read |
| Rewrites commits | no | yes (new identifiers) |
| Safe on a shared branch | yes | no |
| Typical use | integrating a pull request into main | updating your own branch before review |
Many teams combine both: everyone rebases their branch on main to keep it up to date, then the pull request is merged. Hosting services also offer squash and merge: all the pull request's commits are combined into a single commit on main. The team chooses its rule; what matters is that everyone follows the same one.
Undoing a published commit: revert
The commit fix: correct the prices is published… and wrong. On a shared branch, you don't erase: you add a commit that does the opposite.
git switch -q main
git merge -q feat/contact
echo "Prices: 50,000 F CFA" > prices.html
git commit -q -am "fix: correct the prices"
git revert --no-edit HEAD > /dev/null
git log --oneline
cat prices.html
8f0430a Revert "fix: correct the prices"
5f6bc35 fix: correct the prices
4cae75f feat: contact page
1ec4073 feat: prices page
787554d feat: home page
Prices
git revert erases nothing: it adds commit 8f0430a, which cancels exactly the changes of 5f6bc35. The file is back to "Prices", and the whole team can fetch this new commit normally. The first command, git merge feat/contact, is a fast-forward: main simply moves up to the rebased commit.
Reset and reflog: for local work only
git reset --hard moves the branch backwards and abandons the following commits. This is acceptable only for commits that have never been pushed:
git reset -q --hard HEAD~2
git log --oneline
4cae75f feat: contact page
1ec4073 feat: prices page
787554d feat: home page
HEAD~2 means "two commits before the current commit". The last two commits have disappeared from the history… but not from the repository. Git records every movement of HEAD in the reflog (reference log), kept locally for about 90 days:
git reflog -3
git reset -q --hard "HEAD@{1}"
git log --oneline -2
4cae75f HEAD@{0}: reset: moving to HEAD~2
8f0430a HEAD@{1}: revert: Revert "fix: correct the prices"
5f6bc35 HEAD@{2}: commit: fix: correct the prices
8f0430a Revert "fix: correct the prices"
5f6bc35 fix: correct the prices
HEAD@{1} means "where HEAD was just before the last movement". One reset too many, a botched rebase: as long as you had committed, the reflog almost always lets you go back. The quotes around HEAD@{1} prevent some shells from interpreting the braces.
Your turn
Worked exercise
Worked exercise: which command?
For each situation, choose between merge, rebase, revert and reset --hard, and justify in one sentence.
- Your
feat/searchbranch (pushed, only you work on it) has fallen behindmain; your pull request has not been reviewed yet. - A commit merged into
mainyesterday broke the discount calculation. - You have just made two local test commits that you have not pushed and want to throw away.
- Mei's pull request is approved and must go into
main.
Hint 1
Hint 2
revert adds; reset and rebase rewrite; merge combines.Show the solution and common mistakes
rebaseonmain(thengit push --force-with-lease): only you use the branch, and the review will be easier on an up-to-date history. Mergingmaininto your branch is also acceptable if the team prefers it.revert: the commit is shared inmain, so you undo it with a new commit without rewriting history.reset --hard HEAD~2: the commits only exist on your machine, nobody depends on them.merge(the pull request's merge button): it is the normal integration into the shared branch.
Common mistakes:
- Using
resetthenpush --forceonmainto "clean up": everyone ends up with an incompatible history. - Believing that
revertdeletes the commit: it stays in the history, which is intended (you keep a record of what happened). - Panicking after an unfortunate
reset --hard:git reflogfinds the commits.
Summary
- 1A merge keeps the real story; a rebase replays commits and produces a linear history.
- 2A rebase creates new commits with new identifiers: it rewrites history.
- 3Golden rule: never rewrite commits that are already shared.
- 4git revert undoes a published commit by adding an opposite commit, safely for the team.
- 5The reflog recovers "lost" work after a reset or a botched rebase.
Your progress is saved on this device. Create a free account to keep your progress on all your devices.