Rewriting history · lesson 3 of 6
Commands and ideas in this lesson
AThe golden ruledon't rewrite what others have
B--force-with-leaseforce, only if nobody else pushed

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
  1. debf383 HEAD →main Serve the pancit with lime
  2. 67e435d origin/main Cover the leche flan with foil
  3. ba04ca2 Add lumpiang shanghai recipe
  4. 98a5edc Add a script that builds the index page
  5. d078487 Make the sinigang steps clearer
  6. 145dc1e Add pancit bihon recipe
  7. 52d1f4d Say how much salt goes in the adobo
  8. 1c17fad Add a README
  9. 9c53888 Add leche flan recipe
  10. 1f359a2 Add sinigang recipe

and 1 older commit

1 of 4 Joaquin has a new commit on top of “Cover the leche flan with foil”, not pushed yet.

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.

Git sandbox
  1. Make a branch bibingka, commit a recipe with a sloppy message, and push it with an upstream
  2. Fix the message with git commit --amend. Try a plain git push and read why it’s rejected
  3. 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

  1. 67e435d HEAD →mainorigin/main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add 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:

  1. Don't pull with a merge straight away (that's what doubled Joaquin's commits).
  2. Put your own new commits safe on a branch: git branch my-work.
  3. Reset your copy to the rewritten one: git fetch, then git reset --hard origin/main.
  4. Bring your work back on top: git cherry-pick your commits from my-work (lesson 6.6), or git 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

Spot the trapAna

Ana rebased main locally to tidy it, and her push is rejected. She’s in a hurry.

What happens if…

Check what you learned

Which is safe?
What does --force-with-lease add over --force?
Why did Joaquin end up with two “Cover the leche flan” commits?

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.

All sources for Simple Git