Git Commit Amend - Fix the Last Commit Message, Add a Forgotten File, Change the Author, and Why git push --force-with-lease Can Still Delete a Teammate's Commit

TL;DR

  • git commit --amend does not edit a commit, it replaces it. The new commit has the same parent and the same author as the old one, but a different hash, and the branch pointer moves to it. The git commit reference calls it "a rough equivalent" of git reset --soft HEAD^ followed by git commit -c ORIG_HEAD.
  • Fix only the message: git commit --amend -m "new message". Add a forgotten file: git add file then git commit --amend --no-edit. --no-edit keeps the old message and skips the editor.
  • Change the author: --author="Name <email>" sets the author but leaves the committer alone. --reset-author makes the author you again. The two options cannot be combined: fatal: options '--reset-author' and '--author' cannot be used together.
  • Never amend a commit someone else already has. After amending a pushed commit a plain git push is rejected as non-fast-forward, and the usual fix, git push --force-with-lease, is safe only if you have not run git fetch in between (section 7 shows it deleting a teammate's commit).
  • To undo an amend: the old commit is still in git reflog; git reset --hard HEAD@{1} brings it back.
  • Not your case? To change an older commit, not the last one, read git squash commits and git rebase --exec; to move HEAD without making a new commit, read git reset soft, mixed and hard.

Table of Content

  1. What amend really does
  2. Fix a typo in the last commit message
  3. Add a forgotten file to the last commit
  4. Change the author and the date
  5. Undo an amend with the reflog
  6. Which flag for which job
  7. Amending a commit you already pushed
  8. What broke when I ran this
  9. Conclusion

1. What amend really does

Every commit in Git is identified by a hash that is computed from its content: the tree of files, the parent hash, the author, the committer, the timestamps and the message. Change any one of them and you get a different hash. That is why --amend cannot "edit" a commit. What it does is build a brand new commit from the current index, reuse the old message as the starting point, give it the same parent as the commit it replaces, and move the branch to it. The old commit becomes unreachable from the branch, though it survives in the reflog for a while.

The manual says it directly: the new commit "has the same parents and author as the current one". So --amend is the right tool for exactly one situation, which is "the commit I just made is wrong and nobody else has it yet".

A commit history before and after git commit --amend: the old commit is replaced by a new commit with a new hash and the same parent
git commit --amend creates a new commit with the same parent; the old one is only reachable through the reflog

I used a throwaway repository for every run in this post. The versions: git 2.43.0, run on 2026-12-03 (UTC). I set user.name and user.email locally in the repository and nothing else, so you can repeat each step on your own laptop. If you are still learning what a commit, a tree and a blob are, start with what is Git first.

2. Fix a typo in the last commit message

I committed a file with a typo in the message ("aap" instead of "app"), looked at the log, and amended:

 1$ git --version
 2git version 2.43.0
 3$ git log --oneline
 47ca2503 add aap config
 5$ git commit --amend -m "add app config"
 6[main ab92fd0] add app config
 7 Date: Thu Dec 3 10:00:00 2026 +0000
 8 1 file changed, 1 insertion(+)
 9 create mode 100644 app.txt
10$ git log --oneline
11ab92fd0 add app config

Two things to read in that output. First, the hash changed from 7ca2503 to ab92fd0, so this really is a new commit. Second, there is still only one commit in the log: the old one did not stay behind next to the new one.

If you leave out -m, Git opens your editor with the old message filled in, which is what you want when you are fixing a longer message with a body. For a message with several lines I prefer the editor, because -m with a quoted multi-line string is easy to get wrong in a shell. Set GIT_EDITOR=true or pass --no-edit if you want to skip the editor completely.

3. Add a forgotten file to the last commit

This is the case I see most often: you commit login.txt, then notice that README.md should have been part of the same commit. Do not make a second commit called "forgot README". Stage the file and amend without touching the message:

 1$ git status --short
 2?? README.md
 3$ git add README.md
 4$ git commit --amend --no-edit
 5[main ccde667] add login
 6 Date: Thu Dec 3 10:00:00 2026 +0000
 7 2 files changed, 2 insertions(+)
 8 create mode 100644 README.md
 9 create mode 100644 login.txt
10$ git show --stat --format=%s HEAD
11add login
12
13 README.md | 1 +
14 login.txt | 1 +
15 2 files changed, 2 insertions(+)

The commit before the amend had one file (login.txt); after it, git show --stat lists two. The message "add login" is unchanged, and so is the parent ab92fd0. The git commit reference documents --no-edit exactly like this: "git commit --amend --no-edit amends a commit without changing its commit message".

The same command also removes a file from the last commit: git rm --cached file (or git restore --staged file for a file added in that commit) followed by git commit --amend --no-edit. The file stays in your working directory and leaves the commit.

If you have other staged changes that you do not want in the amended commit, use git commit --amend --only --no-edit. The manual says that with --amend, --only needs no paths and amends the last commit without including what is staged. I tried it with an extra.txt staged: the amended commit did not take it in, and the commit still listed the same two files.

4. Change the author and the date

--author replaces the author of the amended commit. The committer stays whoever is configured. After an amend with --author, git log shows the author and committer separately:

 1$ git commit --amend --no-edit --author="Test Person <t@example.com>"
 2[main f47b697] add a.sh
 3 Author: Test Person <t@example.com>
 4 Date: Thu Dec 3 05:09:22 2026 +0000
 5$ git log -1 --format='after:  %h author=%an committer=%cn'
 6after:  f47b697 author=Test Person committer=Rahul Wagh
 7$ git commit --amend --no-edit --reset-author
 8[main 55d372d] add a.sh
 9$ git log -1 --format='reset:  %h author=%an <%ae>'
10reset:  55d372d author=Rahul Wagh <rahul@example.com>

--reset-author is the one to use when you committed under the wrong identity, for example your work email in a personal repository: fix user.email with git config, then run git commit --amend --no-edit --reset-author. It takes the author from your current configuration and also refreshes the author date. --date="<date>" sets the author date explicitly if you need that.

You cannot combine the two:

1$ git commit --amend --no-edit --reset-author --author="Test Person <t@example.com>"
2fatal: options '--reset-author' and '--author' cannot be used together

Note that the amend only fixes the last commit. If five commits carry the wrong email you need a rebase, not an amend.

5. Undo an amend with the reflog

Git does not delete the old commit when you amend. It records every move of HEAD in the reflog, so an amend you regret is one command away from being undone. This is the reflog from my second repository after several amends:

1$ git reflog -4
22afded9 HEAD@{0}: commit (amend): add a.sh v2
355d372d HEAD@{1}: commit (amend): add a.sh
4f47b697 HEAD@{2}: commit (amend): add a.sh
555d372d HEAD@{3}: commit (initial): add a.sh

Every commit (amend) line is one amend, and the hash at the left is the commit you ended up on. To go back one step: git reset --hard HEAD@{1}. Use --soft instead of --hard if you want to keep the amended content staged. The git reflog reference says reflog entries are pruned after 90 days by default (gc.reflogExpire), and entries no longer reachable from the branch tip after 30 days (gc.reflogExpireUnreachable), so the old commit you amended away is safe for 30 days at the default settings, not forever. Also, the reset post explains what each reset mode does to your files.

One surprise in that output: 55d372d shows up twice (HEAD@{1} and HEAD@{3}). My amend with --reset-author produced exactly the same commit as the original, because the tree, parent, author, committer and the timestamp to the second were identical. Same inputs give the same hash; that is how Git works, not a bug.

6. Which flag for which job

JobCommandNew hash?Message changes?
Fix the messagegit commit --amend -m "msg"YesYes
Fix a longer message in the editorgit commit --amendYesYes
Add a forgotten filegit add f then git commit --amend --no-editYesNo
Keep staged files out of the amendgit commit --amend --only --no-editYesNo
Change the author to someone elsegit commit --amend --no-edit --author="N <e>"YesNo
Change the author back to yougit commit --amend --no-edit --reset-authorYes (usually)No
Skip the pre-commit hookgit commit --amend --no-verifyYesPer flags
Change an older commitgit rebase -i with reword or editYes, plus every later commitYes

The pre-commit hook, by the way, runs on an amend like on any commit. The githooks reference says pre-commit "can be bypassed with the --no-verify option". In my test hook that printed a line, it printed on the amend too.

7. Amending a commit you already pushed

A commit you have pushed and amended exists in two versions: the old one on the remote and the new one locally, and they are not ancestors of one another. Git refuses a plain push (I waited 1 s between the amend and the push so the timestamps differ):

1$ git commit -q --amend -m "add a.txt"
2$ git push
3To ~/lab/remote.git
4 ! [rejected]        main -> main (non-fast-forward)
5error: failed to push some refs to '~/lab/remote.git'
6hint: Updates were rejected because the tip of your current branch is behind
7hint: its remote counterpart. If you want to integrate the remote changes,
8hint: use 'git pull' before pushing again.

The hint is misleading here. It is not that you are behind; you rewrote a commit that the remote already has. If you follow the hint and run git pull, you merge your own old commit back in next to the amended one. The proper tool is git push --force-with-lease, which the git push reference describes as the safer form of force: it only overwrites the remote branch if the remote is still where your remote-tracking branch says it is.

1$ git push --force-with-lease
2To ~/lab/remote.git
3 + 3cd0e9c...853683d main -> main (forced update)

Now the part that cost me a moment. A teammate (a second clone of the same remote) pushed a commit on top of mine. I amended again and pushed with the lease. The lease did what it should and refused:

1$ git push --force-with-lease
2To ~/lab/remote.git
3 ! [rejected]        main -> main (stale info)
4error: failed to push some refs to '~/lab/remote.git'

Good, it protected the teammate. Then I did what many people do on a rejection: I ran git fetch and tried again.

1$ git fetch && git push --force-with-lease
2To ~/lab/remote.git
3 + 2dfa63e...152b66c main -> main (forced update)
4$ git log --oneline origin/main
5152b66c add a.txt, take 3

The push succeeded, and origin/main now has only my commit. The teammate's teammate work commit is gone from the branch. The fetch moved my remote-tracking branch to the teammate's tip, so the lease saw "remote is exactly where I last looked" and allowed the overwrite, even though I had never looked at what was in that new commit. --force-with-lease checks that you have the latest ref, not that you merged it. After the fetch, run git log HEAD..origin/main and git rebase origin/main before you push again, so the teammate's commit stays in the history.

My rules for amending after a push:

  1. Only amend on a branch that is yours alone, such as a pull request branch you opened.
  2. Say so in the PR if a reviewer may have pulled it.
  3. Never fetch then force-push without looking at what arrived.
  4. Never amend on main or any shared long-lived branch. Make a git revert or a follow-up commit instead.

8. What broke when I ran this

1. The lease passed after a fetch. Described above; this is the one that matters. The rejection looked like a nuisance and the "fix" quietly deleted a commit. I only noticed because I ran git log --oneline origin/main afterward and saw one line instead of two. The commit was recoverable from the teammate's clone, and on the server it would have been recoverable from the remote's reflog only if that existed (bare repositories do not keep one by default).

2. --reset-author together with --author. Git stops with fatal: options '--reset-author' and '--author' cannot be used together and changes nothing. Pick one.

3. An amend that looked like it did nothing. After the author reset I got 55d372d, a hash I had already seen. Git builds the commit from the same inputs, including a timestamp with 1 s resolution, and gets the same hash. If you need to prove the amend happened, look at git reflog, which still logs commit (amend).

4. The pull hint. After amending a pushed commit, git push suggests git pull. For this case that advice is wrong, as shown in section 7.

5. git commit --amend with nothing staged. It still succeeds and rewrites the commit: it refreshed the committer date and gave me a new hash even though the content was identical. That is harmless on a local commit and a reason not to run it "just to check" on a pushed commit.

9. Conclusion

git commit --amend is the right way to fix the last commit message, to add a forgotten file and to correct the author, as long as the commit is still only yours. The three habits that keep it safe are to read git log -1 afterward, to remember that the old commit lives in git reflog until it expires, and to never fetch-then-force-push without reading what arrived.

Checklist before you amend:

  1. Is the commit pushed? If yes, is the branch yours alone?
  2. Did you git add everything that belongs in it, and nothing else?
  3. Do you want the message changed (-m or the editor) or kept (--no-edit)?
  4. After a push with --force-with-lease, does git log origin/main still show everyone's commits?

This is lesson Day 7 of the DevOps Master Course on YouTube; the written foundation for branches, rebase and pull requests is in the free Git and GitHub blueprint course. The branching side is in git branching explained.

Did this save you a debugging session? Stay in the loop -