TExcelTAD
Home › Git › Git Amend

Git Amend

Just committed and spotted a typo in the message? Forgot to add a file? git commit --amend replaces the last commit with a corrected one. It is the most common way to fix a commit before you share it.

Fixing the message

bash · try it yourself
mkdir site && cd site && git init -q
echo "<h1>Contact</h1>" > contact.html && git add . && git commit -q -m "Add contcat page"
git log --oneline
git commit --amend -m "Add contact page"
git log --oneline
b8b3f12 Add contcat page
[main b80d922] Add contact page
 Date: Thu Jan 15 10:00:00 2026 +0000
 1 file changed, 1 insertion(+)
 create mode 100644 contact.html
b80d922 Add contact page

The commit got a new hash: amend doesn't edit a commit (commits are immutable), it creates a new one and moves the branch to it. Without -m, Git opens your editor with the old message to edit.

Adding a forgotten file

Stage the missing change, then amend. --no-edit keeps the message as it is:

bash · try it yourself
mkdir site && cd site && git init -q
echo "<h1>Contact</h1>" > contact.html
echo "form { margin: 1em; }" > contact.css
git add contact.html && git commit -q -m "Add contact page"
git show --stat --format="%h %s" HEAD
git add contact.css
git commit -q --amend --no-edit
git show --stat --format="%h %s" HEAD
git log --oneline
b80d922 Add contact page

 contact.html | 1 +
 1 file changed, 1 insertion(+)
7df986d Add contact page

 contact.css  | 1 +
 contact.html | 1 +
 2 files changed, 2 insertions(+)
7df986d Add contact page

Still one commit, now with both files.

Fixing the author

Committed with the wrong email (for example a work address in a personal project)? Fix the config, then reset the author on the last commit:

bash · try it yourself
mkdir site && cd site && git init -q
git config user.email "ada@work.example"
echo "hi" > a.txt && git add . && git commit -q -m "Add greeting"
git log --format="%h %an <%ae>"
git config user.email "ada@example.com"
git commit -q --amend --no-edit --reset-author
git log --format="%h %an <%ae>"
631e7f7 Ada Lovelace <ada@work.example>
2a8329c Ada Lovelace <ada@example.com>

Amending older commits

--amend only touches the latest commit. For older ones, make a fixup commit and let rebase fold it into its target. git commit --fixup=<commit> names the commit to fix, and git rebase --autosquash moves and merges it:

bash · try it yourself
mkdir site && cd site && git init -q
echo "home" > index.html && git add . && git commit -q -m "Add home page"
echo "aboot us" > about.html && git add . && git commit -q -m "Add about page"
echo "contact" > contact.html && git add . && git commit -q -m "Add contact page"
echo "about us" > about.html
git commit -q -a --fixup=HEAD~1
git log --oneline
GIT_SEQUENCE_EDITOR=: git rebase -q -i --autosquash HEAD~3
echo "--- after autosquash:"
git log --oneline
cat about.html
6fbbd0c fixup! Add about page
df24f9c Add contact page
841780f Add about page
ff139b4 Add home page
--- after autosquash:
b69a608 Add contact page
fa8e63e Add about page
ff139b4 Add home page
about us

GIT_SEQUENCE_EDITOR=: accepts the prepared rebase plan without opening an editor (: is the shell's do-nothing command). In your own terminal you would just run git rebase -i --autosquash HEAD~3 and save the plan. Rebase is covered in the next lesson.

Amending pushed commits

Amending creates a different commit, so if the old one was already pushed, the remote branch no longer fast-forwards:

bash · try it yourself
git init -q --bare ~/github/site.git
mkdir site && cd site && git init -q
echo "hi" > a.txt && git add . && git commit -q -m "Add greting"
git remote add origin ~/github/site.git && git push -q -u origin main
git commit -q --amend -m "Add greeting"
git status -sb | head -1
git push -q 2>&1 | head -2
## main...origin/main [ahead 1, behind 1]
To /home/learner/github/site.git
 ! [rejected]        main -> main (non-fast-forward)

On your own branch (say, a pull request nobody else builds on) it's fine to git push --force-with-lease. On main or other shared branches, don't amend pushed commits; make a new commit instead.

Test yourself

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

  1. Creates a second commit with the same message
  2. Replaces the last commit, adding the staged changes and keeping its message
  3. Edits the commit in place with the same hash
  4. Undoes the last commit

Why does an amended commit get a new hash?

  1. Git renames commits daily
  2. Commits are immutable; amend creates a new commit with different content/message
  3. Because of the push
  4. It doesn't

How do you fix a mistake in the third-last commit?

  1. git commit --amend~3
  2. git commit --fixup=HEAD~2, then git rebase -i --autosquash
  3. git revert --amend
  4. It's impossible