Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 78 ยท Rewriting History

Amending, Splitting and Rewriting Commits

Fix your last commit with --amend, split a commit in two, change authors and dates, remove a file or secret from all history and learn why force-with-lease beats force.

Commits look permanent, but Git lets you rewrite them: change the message, add a forgotten file, split one commit into two, correct the author, or erase a file from every commit in the project. All of these work the same way. Git cannot modify a commit (its hash is computed from its content), so it creates a new commit and moves the branch to it. The old one stays around, unreferenced, until garbage collection.

This lesson starts with the most common fix, git commit --amend, and works up to rewriting the entire history. Throughout, remember the rule from Rebasing: rewrite only commits that other people do not have yet.

Amend the last commit

git commit --amend replaces the tip of your branch with a new commit built from whatever is in the index, plus a message you can edit. It covers three everyday cases.

Fix the message

TEXT
git commit --amend -m "Better message"

Add a forgotten change or file

Stage the missing piece, then amend without touching the message: --no-edit reuses it.

Fix the author or date

--reset-author takes the author from your current configuration (and the current time), and --author="Name <email>" sets one explicitly. --date changes the author date.

Bash Amend: message, forgotten file and author

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.

Each amend produced a different hash (rev-parse printed a new value every time), but the history still has a single commit. The author is now Grace while the committer is still the identity that ran the command. Git stores both: the author wrote the change; the committer is whoever created that particular commit object (which is why committer changes when you rewrite someone else's commit).

git commit --amend with files staged includes them. With nothing staged it only changes the message. If you want to amend and also commit all tracked changes at once, use git commit -a --amend.

Amend a commit that is not the last one

--amend only works on the tip. For an older commit you have three options:

  • git commit --fixup=<commit> and later git rebase -i --autosquash (see Interactive Rebase),
  • mark the commit edit in an interactive rebase, amend there, then git rebase --continue,
  • git commit --fixup=amend:<commit> or reword:<commit> to also change that commit's message during autosquash.

Split a commit into two

You made one commit that does two things, and a reviewer would like them apart. The recipe uses an edit stop:

  1. git rebase -i <before-the-commit> and mark the commit edit.
  2. When the rebase stops there, run git reset HEAD^. This undoes the commit but keeps its changes in your files, unstaged (see git reset in Depth).
  3. Stage and commit the first part, then the second part, with the messages you want. git add -p splits one file into hunks (Staging Changes).
  4. git rebase --continue.
Bash Split one commit into two with an edit stop

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.

The one commit with two files became two commits, and Add tail was replayed on top. The trick is the middle step: the rebase stops after the commit was applied, so undoing that commit with reset HEAD^ hands its changes back to you to recommit in pieces.

Change dates

A commit has two dates: the author date (when the change was written) and the committer date (when this commit object was created, which updates on every rewrite). git log shows the author date by default. You can set them with environment variables or --date:

TEXT
GIT_COMMITTER_DATE="2026-02-01T09:00:00Z" git commit --amend --no-edit --date="2026-02-01T09:00:00Z"

Only change dates for a reason, such as fixing a wrong clock. Pretending work happened at another time confuses tools that rely on order, and is a bad idea for audit trails.

Rewrite authors across many commits

To change the author of the last several commits, rebase and run an amend at each step. exec does exactly that:

Bash Fix the author of several commits with rebase --exec

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.

All three commits now name Ada as author. For a whole repository there are specialised tools (below), and if you only need to display a different name without rewriting, a .mailmap file maps old names and addresses onto new ones in git log and git shortlog.

Remove a file or a secret from all of history

Sometimes a file must disappear from every commit: a large binary somebody added by mistake, or a password that was committed. A normal delete commit is not enough, because the file remains in the old commits. You have to rewrite every commit that contains it.

The modern tool is git filter-repo, a separate Python program that you install yourself. It is fast and safe by default (it works on a fresh clone and removes the remote on purpose). Its usual forms are read-only here because it is not part of Git itself and not installed in our sandbox:

Terminal
# read-only: needs the git-filter-repo tool
git filter-repo --path secrets.env --invert-paths      # delete a path from all history
git filter-repo --replace-text expressions.txt         # replace strings (e.g. a password) everywhere
git filter-repo --subdirectory-filter backend          # keep only a folder, as the new root

The older built-in command is git filter-branch. Git's own documentation recommends against it: it is slow and has many subtle pitfalls. It still works, and we can use it for a small demonstration. FILTER_BRANCH_SQUELCH_WARNING=1 silences the long warning (otherwise it waits a few seconds):

Bash Erase a file from every commit with filter-branch

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.

The file is gone from every commit on the branch, and Add config became an empty commit that is still in the list (filter-branch keeps empty commits unless asked). The last number is the key surprise: refs/original/... holds a backup of the old branch, so the secret is still stored in the repository. You would have to delete those refs, expire the reflog and run git gc --prune=now to really remove it locally.

If you committed a real secret, rewriting history is not enough. Treat the secret as compromised, revoke it and issue a new one first. Anyone who cloned, forked or cached the repository, and any service that scanned it, may already have the old value. History rewriting only cleans up afterwards.

After a successful rewrite you must also remove old copies: git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin, git reflog expire --expire=now --all, and git gc --prune=now. Everyone else must re-clone or hard-reset, because all hashes changed. Hosting providers also keep cached views of old commits, and their support can purge those.

Force-with-lease: pushing rewritten history safely

After you rewrite commits that you have already pushed (your own pull-request branch, say), a normal git push is rejected, because your branch no longer contains what the server has. The usual answer is a force push. The right way is git push --force-with-lease:

Command Behaviour
git push --force overwrites the remote branch no matter what, even if a colleague pushed new commits since you last looked
git push --force-with-lease overwrites only if the remote branch still points to the commit your last fetch saw; otherwise it refuses
git push --force-with-lease --force-if-includes also checks that your local work contains the remote tip you fetched
Bash force-with-lease refuses a stale overwrite (fails on purpose)

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.

The push was refused with ! [rejected] main -> main (stale info) and an error line, so the colleague's commit was not destroyed. Plain --force would have deleted it from the server. After a fetch, you look at what arrived, rebase or merge it into your work, and only then push again. (A bare git push --force-with-lease without a prior fetch compares against your remote-tracking branch, so a background git fetch in an editor can silently renew the lease. Pass the expected hash explicitly, --force-with-lease=main:<hash>, if you want to be strict.)

Choosing the right tool

Goal Tool
Fix the last commit git commit --amend
Fix an older commit --fixup + rebase -i --autosquash, or edit in an interactive rebase
Split a commit edit stop + git reset HEAD^ + several commits
Change authors of a few commits git rebase --exec 'git commit --amend --reset-author --no-edit'
Show a corrected name only .mailmap
Remove a file or text from everything git filter-repo (preferred) or filter-branch
Undo public history without rewriting it git revert

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does git commit --amend --no-edit do?

    Amend builds a new commit with a different hash and moves the branch to it. --no-edit keeps the message.
  2. 2. How do you split an older commit into two?

    At the edit stop the commit is applied; reset HEAD^ undoes it but keeps the changes in the files so you can commit them in pieces.
  3. 3. You ran git filter-branch to delete a file that contained a password. Which statement is true?

    Backup refs (refs/original), the reflog, other clones and hosting caches may still contain the old commits. A leaked secret must be revoked.
  4. 4. Why is git push --force-with-lease better than --force?

    The lease checks that the remote branch is still where you last saw it, so you do not delete a colleague's new commits.
  5. 5. Which two identities does a commit store?

    The author wrote the change; the committer created this commit object, which changes when a commit is rewritten, for example by rebase or amend.

Summary

  • Git never edits a commit; amend, rebase and filter tools create new commits and move the branch.
  • git commit --amend fixes the last commit (message, content, author). For older commits use --fixup with autosquash or an edit stop.
  • To split a commit: edit it in a rebase, git reset HEAD^, make several commits, git rebase --continue.
  • Authors and dates can be rewritten with --author, --reset-author, --date and rebase --exec; .mailmap fixes names for display only.
  • git filter-repo (preferred) or filter-branch removes files and text from all history, but leaks remain in backups, reflogs and clones: revoke exposed secrets.
  • After rewriting pushed history use git push --force-with-lease, never plain --force.

Next: Reverting Commits Safely