Git Reference ยท Concepts
Rewriting history safely
Know which Git commands rewrite history, why it changes commit ids, how to use --force-with-lease and how to recover with ORIG_HEAD and the reflog.
A commit's id is a hash of its content, its parent ids, the author and the message. Change any of those and you get a different commit. So "editing" history never edits anything: Git creates new commits and moves a branch pointer to them, leaving the old commits orphaned (but still in the object database for a while). Commands that do this are said to rewrite history: commit --amend, rebase, reset (followed by new commits), cherry-pick plus branch moves, filter-branch and filter-repo.
Rewriting is perfectly safe on commits that exist only in your repository. It becomes risky once others have the old commits, because their branches still contain them and Git will see two diverging histories. Plan ahead, push carefully and know the way back.
git commit --amend # replace the last commit git rebase -i HEAD~5 # reorder, squash, edit, drop git push --force-with-lease # publish rewritten history safely git reset --hard ORIG_HEAD # undo the last rewrite
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.
Usage notes
- The rule. Rewrite freely what is private; avoid rewriting what is shared; if you must, tell your team and use
--force-with-lease. --force-with-leaserefuses to overwrite a remote branch if it moved since you last fetched, so you cannot destroy a teammate's push by accident. Plain--forcehas no such check. Add--force-if-includesfor an extra guard against a fetch that you did not look at.- Protect important branches. Hosting services can forbid force pushes to
main;receive.denyNonFastForwardsdoes the same on a self-hosted bare repository. - Undo right away. After
rebase,reset,mergeorcommit --amend,ORIG_HEADholds the previous tip, sogit reset --hard ORIG_HEADgoes back. Later, usegit reflog. - Safety net. Old commits stay in the repository until
git gcprunes them, which by default happens after the reflog entries expire (30 days for unreachable commits). Do not rely on this on a server. - Teammates after your force-push: they should
git fetchand thengit rebase --onto <new> <old> <branch>or reset their local branch to the new tip. A plaingit pullwould merge old and new history, duplicating commits. - Secrets are not removed by a new commit. Rewriting all affected commits and invalidating the credential is the only fix (see Removing secrets from history).
- Tags and signatures. Rewriting invalidates signed commits and moves nothing for existing tags: they keep pointing to the old commits unless you move them.
- Prefer
git revertwhenever people already depend on the commit (see Undoing things).