Git disasters · Chapter 12 of 14
Duplicate commits after a rebase
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Merges and detective work
- 3 tasks
- Git
- In the all labs plan
The first 2 chapters of Git disasters are free. This one comes with the all labs plan.
Why you need this
A commit's hash covers its parent. Rebasing replays each commit on a new base, so every commit gets a new hash: same message, same change, different commit. On a small recipes repo:
$ git log --oneline -1
cb96d17 Add rasam
$ git rebase main
Successfully rebased and updated refs/heads/soups.
$ git log --oneline -1
ba71893 Add rasam
Asha force-pushed that. Dev still had the old cb96d17 under his own commit, and his git pull (set to merge) joined the two histories:
$ git pull --no-edit
+ cb96d17...ba71893 soups -> origin/soups (forced update)
Merge made by the 'ort' strategy.
$ git log --oneline --graph
* b5e130c Merge branch 'soups' of /tmp/w2/ex/rc into soups
|\
| * ba71893 Add rasam
| * 01501f9 Rename the book
* | 09184f9 Add sambar
* | cb96d17 Add rasam
|/
* 7c9478c Start the recipes
(Fetch lines trimmed.) + and "forced update" are the clue that the branch was rewritten.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
See what happened: find the merge your pull made
After your git pull on rewards, Bunty's three commits are there twice. Find the merge commit that your pull made.
1. Show rewards as a graph. Two lines with the same three messages join in one commit.
2. Show the reflog of HEAD. It says which command made each move.
3. Find the entry made by the pull. Its commit is the merge.
4. Write that hash into answer.txt, in the case's folder.
To pass: answer.txt has the hash of the merge your pull made (7 characters or more), and no other hash.
- Task 2
Recover: one copy of each commit, on GitHub
Keep only one copy of each commit on rewards, then push it. Nothing of yours is pushed yet, so you may change your local commits.
1. Move rewards back to the merge's first parent. That is your branch before the pull.
2. Replay your branch on top of Bunty's new rewards from GitHub. The old copies are dropped.
3. Show the graph. It must be one straight line.
4. Push rewards to GitHub.
To pass: GitHub's rewards has exactly four commits that are not on main, each one only once: Add the rewards table, Earn a point per chai, Redeem 10 points for a free chai, Show points on the bill. It has no merge commits. It is on top of GitHub's main (with Raise cutting chai to 30). bill.js still has your pointsLine. Your rewards and GitHub's rewards are the same commit.
- Task 3
Guard it: a pull that never merges
Make git pull in this repository stop making merge commits.
1. Find the pull setting in this repository's own config. It says to merge.
2. Change it here, so that pull rebases your local commits. Or make pull refuse anything but a fast-forward.
To pass: the Check tries it in a copy of your repository, on rewards as it is on GitHub. A teammate pushes a commit to rewards. You make a local commit. Then git pull runs. HEAD must not be a merge commit. If the pull works, the teammate's commit must be on your branch.
What you will learn
- See what happened: find the merge your pull made
- Recover: one copy of each commit, on GitHub
- Guard it: a pull that never merges
More in Merges and detective work
Build "Duplicate commits after a rebase" yourself
Bunty is Tapri's new junior developer, and Kabir keeps handing you the Git incidents left behind. Each is a disaster interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Git disasters are free. This one comes with the all labs plan.