The golden rule
Last updated
Every rewriting command makes new commits to replace old ones. That's harmless while you're the only one with the old ones. It causes real trouble once someone else has them. In this lesson you'll watch what happens to Joaquin's copy when Ana rewrites shared history, learn the rule that prevents it, and use the safe way to update your own pushed branch.
By the end of this lesson you can
- State the golden rule: don't rewrite commits others have built on
- Explain what goes wrong in a teammate's copy when shared history is rewritten
- Update your own pushed branch with git push --force-with-lease
- Recover when someone has rewritten a shared branch
The rule
Don't rewrite commits that others have built on.
In practice: don't amend, rebase or squash commits that you've pushed to a branch others use, such as main. Rewrite freely on your own branch before sharing, or on a branch nobody else works on.
The rule exists because of lesson 6.1: rewriting makes new commits. Your copy then has the new ones; everyone else's has the old ones. Git sees them as different commits that happen to look alike.
What goes wrong
Ana pushed "Cover the leche flan with foil" yesterday. Joaquin pulled it, and committed on top. Today Ana amends that pushed commit's message and force-pushes main.
(Joaquin) $ echo "4. Serve with lime wedges." >> pancit.md (Joaquin) $ git commit -am "Serve the pancit with lime" [main debf383] Serve the pancit with lime 1 file changed, 1 insertion(+) (Joaquin) $ git log --oneline -3 debf383 (HEAD -> main) Serve the pancit with lime 67e435d (origin/main) Cover the leche flan with foil ba04ca2 Add lumpiang shanghai recipe
debf383HEAD →main Serve the pancit with lime67e435dorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe
and 1 older commit
Your own branch: --force-with-lease
There is one normal reason to force-push: you rebased or tidied your own branch, which you'd already pushed (for example, to a pull request). The host's copy has the old commits, so a normal push is rejected.
Use git push --force-with-lease. It forces only if the hosted branch is still where your Git last saw it (your origin/your-branch). If someone else pushed to it since, it refuses with "stale info" instead of throwing their work away. git push --force-with-leaseCareful: changes files or history, but Git can usually get it back
One catch: a git fetch updates what your Git "last saw", so after fetching, the lease would let you overwrite commits you've never looked at. Git 2.30 added --force-if-includes to guard against that; use both together if your team pushes to the same branches.
- Make a branch
bibingka, commit a recipe with a sloppy message, and push it with an upstream - Fix the message with
git commit --amend. Try a plaingit pushand read why it’s rejected - Push with
--force-with-lease
Working folder
no changes
Staging area
nothing staged
Repository
10 commits on main
Remote
origin: example.com/ana/kusina.git · in step, as of your last fetch
You are in ~/kusina. It is connected to a hosted copy, origin, and Joaquin has his own clone on his computer.
Commit graph
67e435dHEAD →mainorigin/main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
The second task expects the message Add bibingka recipe.
When someone broke the rule
If a shared branch was rewritten and you have the old commits:
- Don't pull with a merge straight away (that's what doubled Joaquin's commits).
- Put your own new commits safe on a branch:
git branch my-work. - Reset your copy to the rewritten one:
git fetch, thengit reset --hard origin/main. - Bring your work back on top:
git cherry-pickyour commits from my-work (lesson 6.6), orgit rebase --onto origin/main <old base> my-work.
And talk to whoever did it. Lesson 7.4 shows host rules that block force pushes to main entirely.
Spot the trap: push --force to a shared branch
Ana rebased main locally to tidy it, and her push is rejected. She’s in a hurry.
What happens if…
Check what you learned
Sources
Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.
- Pro Git: The Perils of Rebasing: the rule, and what happens to others when it's broken
- Git documentation: git-push, --force-with-lease: how the lease works, and --force-if-includes
- Git documentation: git-rebase, Recovering from upstream rebase: what to do when a shared branch was rewritten