Sub topic 10
Why rewriting shared history causes problems
This chapter assumes you can commit, branch, push, and pull, and that you know what HEAD and a branch pointer are. It explains why some undo commands are safe on shared branches and others are not.
Adding history versus rewriting history
Every commit has an ID that is computed from its content, its message, and its parent commit's ID. If you change any of those, you get a new commit with a different ID. Git cannot edit a commit in place.
Some commands add new commits on top of the old ones. git revert is one of these. Other commands replace commits with new versions and move the branch pointer to them. git commit --amend, git reset followed by new commits, and git rebase -i all do this. The old commits still exist for a while, but the branch no longer points to them.
Rewriting is harmless while the old commits exist only on your machine. The trouble starts when someone else already has them.
What goes wrong after a push
Suppose you and a teammate both have commits A, B, and C on main. You amend C into C', then push.
Your teammate builds commit D on top of C. Now the two histories have forked at B. Git treats C and C' as unrelated commits, even if their content is nearly identical. This leads to three problems:
- The push is rejected. The remote has C, and your branch does not contain C. Git refuses a normal push because it would discard commits.
- A forced push removes work. If you force the push, the remote branch moves to C'. Any commit built on C, such as D, is no longer reachable from the branch.
- Pulls produce duplicates and conflicts. Your teammate pulls, and Git sees C and C' as two separate changes to the same lines. The merge may conflict, and even when it succeeds, history contains both versions of the change.
Choosing the safe tool
The rule is about where the commit has been shared, not about which command you use.
| Situation | Safe approach |
|---|---|
| Commit exists only in your local repository | Amend, reset, or rebase freely |
| Commit is on your own feature branch that nobody else uses | Rewrite, then push with `--force-with-lease` |
| Commit is on a shared branch such as `main` | Use `git revert` |
A plain git push --force overwrites the remote branch no matter what is on it. --force-with-lease is safer. It refuses to push if the remote branch has commits that you have not fetched.
git commit --amend --no-edit
git push --force-with-lease origin my-featureThe lease check only compares against what you last fetched. If you ran git fetch and did not look at the new commits, the check passes and you can still overwrite a teammate's work. Read what a fetch brought in before you force-push.
Fixing a mistaken rewrite
If you did overwrite shared history, do not make more changes right away. Your teammates' clones and your own reflog still hold the old commits. Find the old tip with git reflog or a teammate's local branch, then create a branch at that commit. Later chapters cover recovery with the reflog in detail.
Exercise
You pushed commit C to main, and a teammate pulled it. You then notice a typo in C's message. Why is git commit --amend followed by git push --force a bad idea here, and what do you do instead?
Show answer
Amending creates a new commit C' with a different ID, and the force push moves main to C'. The teammate's clone still has C, so their next pull sees diverged history, and any commit they built on C can be lost or duplicated. Leave the message as it is. If the change itself was wrong, use git revert to add a new commit that undoes it.
Exercise
What does --force-with-lease protect you from that --force does not, and what is one case it does not protect you from?
Show answer
It refuses the push if the remote branch contains commits you have not fetched, so it will not silently remove a teammate's recent push. It does not protect you if you fetched their commits without reviewing them, because your local copy of the remote branch is then up to date and the check passes.