Git Commit --Amend: Fix the Last Commit (Files, Author, Date)

Tested on RHEL 10.2 (Coughlan)
Package git 2.52.0
Applies to Any host with Git installed
Privilege Normal user
Scope Fix the last commit with git commit --amend: add forgotten files, keep the same message with --no-edit, update author or date, undo a bad amend, and force-push a rewritten commit. Message-only rewrites with --only, older commits, and multi-commit message fixes live in change a Git commit message.
Related guides Change a Git commit message
Git undo add
Git reflog tutorial
Git push force examples
Git rebase tutorial

You committed, then noticed the problem: a config file never made it into the snapshot, the author email is wrong, or you need to fold a tiny fix into that same commit instead of adding noise to history. git commit --amend rebuilds the latest commit on your branch — it does not patch the old object in place.

If you only need to reword the message, use git commit --amend --only and follow change a Git commit message. This page covers everything else amend is for: files, author, date, recovery, and pushed branches.


Choose the Right Amend Command

Your situation What to run Notes
Forgot a file in the last commit git add file then git commit --amend --no-edit Stages the missing path into the rebuilt commit
Small code fix belongs in the last commit git add changed paths, then git commit --amend --no-edit Same message, updated tree
Change only the commit message git commit --amend --only -m "New message" See change a Git commit message
Wrong author on the last commit git commit --amend --author="Name <email>" --no-edit Updates author metadata
Wrong author date on the last commit git commit --amend --no-edit --date=now --date sets AuthorDate; CommitDate reflects when you amend
Last commit already on the remote Amend locally, then git push --force-with-lease Rewrites history — coordinate first
Regret the last amend git reflog, then git reset --soft to the prior entry Reflog keeps old commits for a while
Need to change a commit before the last one Not amend — use git rebase -i See Git rebase tutorial

Plain git commit --amend without --only can pull in any file already staged with git add. Stage deliberately before you amend.


What Amend Actually Does

Amend replaces the tip commit on your current branch. Git builds a new commit object — new hash — even when the file tree looks identical. Your branch pointer moves forward to that replacement; the previous tip may linger in git reflog until Git expires it.

That hash change is why amending a commit someone else already pulled requires a force push, and why --force-with-lease is safer than a bare --force. Git push force examples compares both flags.


Add a Forgotten File to the Last Commit

Scenario: you committed an app change but left config.yaml untracked on disk.

Start from a repo with two commits and an untracked config file:

bash
git log --oneline
output
86677d1 Update app.txt
d27418e Initial commit: add app and readme

Short status shows the missing file is still untracked:

bash
git status -sb
output
## main
?? config.yaml

The latest commit is 86677d1, and config.yaml was never staged. Stage it, then amend without opening the editor:

bash
git add config.yaml

With the file staged, rebuild the tip commit without opening the editor:

bash
git commit --amend --no-edit
output
[main c52946f] Update app.txt
 Date: Fri Aug 21 08:31:55 2026 +0530
 2 files changed, 2 insertions(+)
 create mode 100644 config.yaml

The subject line stayed Update app.txt, but the hash changed to c52946f and Git now tracks config.yaml in that commit:

bash
git show --stat --oneline -1
output
c52946f Update app.txt
 app.txt     | 1 +
 config.yaml | 1 +
 2 files changed, 2 insertions(+)

If you also want to reword the subject, drop --no-edit or pass -m "Update app.txt and add config.yaml". Message-only edits are covered in change a Git commit message.


Amend Without Changing the Message

Scenario: you already wrote a good subject line and only need to update the files in that commit.

Stage the paths that belong in the snapshot, then reuse the existing message:

bash
git add app.txt

With the fix staged, reuse the existing commit message:

bash
git commit --amend --no-edit

Git rebuilds the tip commit with whatever is currently staged. The message in git log stays the same; the hash and file list may not.

Use this pattern when you:

  • add a file you forgot moments after committing;
  • fix a one-line typo and want it in the same commit;
  • adjust a config file that belongs with the change you just recorded.

If nothing is staged, amend still runs but may only refresh metadata — stage first when you mean to change files.


Change the Author on the Last Commit

Scenario: user.email was wrong when you committed, and the last commit shows the old identity.

Check what Git recorded:

bash
git log --format='%h %an <%ae>' -1
output
c52946f Lab User <lab@example.com>

Replace the author on the rebuilt commit:

bash
git commit --amend --author="Jane Doe <jane@example.com>" --no-edit
output
[main 6016c4d] Update app.txt
 Author: Jane Doe <jane@example.com>
 Date: Fri Aug 21 08:31:55 2026 +0530
 2 files changed, 2 insertions(+)
 create mode 100644 config.yaml

Confirm the metadata Git will show in history:

bash
git log --format='%an <%ae>' -1
output
Jane Doe <jane@example.com>

--author sets the author name and email on the new commit. Pair it with --no-edit when the message is already correct.


Change the Author Date

Scenario: the author timestamp on the last commit is wrong and you want it to reflect the current time or a specific moment.

Inspect author and committer dates — they are separate fields and can differ:

bash
git log --pretty=fuller -1
output
commit 6016c4dc3a2a436b224af1c58dd4ece5d74a0e7d
Author:     Jane Doe <jane@example.com>
AuthorDate: Fri Aug 21 08:31:55 2026 +0530
Commit:     Lab User <lab@example.com>
CommitDate: Fri Aug 21 08:32:11 2026 +0530

    Update app.txt

--date changes the author date. Because amend creates a new commit, Git also records a new committer date based on when you run the amend. Update AuthorDate to now while keeping the message:

bash
git commit --amend --no-edit --date=now
output
[main e0559ec] Update app.txt
 Author: Jane Doe <jane@example.com>
 Date: Fri Aug 21 08:32:17 2026 +0530
 2 files changed, 2 insertions(+)
 create mode 100644 config.yaml

You can pass an explicit author timestamp instead of now:

bash
git commit --amend --no-edit --date="2026-08-21 10:30:00 +0530"

Re-run git log --pretty=fuller -1 and check that AuthorDate matches the value you passed. CommitDate reflects when the amend ran, not the string you gave to --date.


Amend a Commit You Already Pushed

Scenario: the last commit is on the remote, but it is missing a file or metadata you can still fix locally.

Work through the same local amend steps — stage, then git commit --amend --no-edit (or with --author / --date as needed). A normal push is rejected afterward because your local tip no longer matches the remote hash.

Push the replacement history only when rewriting that branch is acceptable:

bash
git push --force-with-lease
output
To gitlab.devlab.io:stackforge/deploy-kit.git
 + 86677d1...c52946f main -> main (forced update)

--force-with-lease refuses to overwrite the remote if someone else pushed since your last fetch. Do not use git pull simply to clear a non-fast-forward rejection after amend — you mean to replace the remote tip, not merge unrelated histories.

WARNING
Amending a shared branch disrupts anyone who already pulled the old commit. Prefer amend-before-push on feature branches you own alone.

Undo git commit --amend

Scenario: you amended once too often and want the previous tip commit back.

git reflog lists recent positions of HEAD, including amend operations:

bash
git reflog -3
output
e0559ec HEAD@{0}: commit (amend): Update app.txt
6016c4d HEAD@{1}: commit (amend): Update app.txt
c52946f HEAD@{2}: commit (amend): Update app.txt

Pick the entry before the amend you regret — here HEAD@{1} at 6016c4d, not a fixed offset you memorize for every repo:

bash
git reset --soft 'HEAD@{1}'

git reset --soft exits silently on success, so confirm the branch tip moved back:

bash
git log --oneline -1
output
6016c4d Update app.txt

Your branch points at the earlier commit again; staged and working-tree changes from the undone amend remain unless you used --hard. Git reflog tutorial covers harder recovery paths; Git reset examples explains --soft, --mixed, and --hard.


Amend Only Works on the Last Commit

git commit --amend always targets the newest commit on your current branch. To edit an older snapshot you need interactive rebase (edit on the target commit, amend there, then git rebase --continue) or a message-only reword flow.

For commit messages on older or multiple commits, use change a Git commit message instead of repeating amend here. For broader history surgery, see Git rebase tutorial.


Troubleshooting

Symptom Likely cause Fix
Unexpected files appeared in the amended commit Staged paths were included because --only was not used Reset if needed via reflog; restage only intended files; amend again with --no-edit
fatal: You have nothing to amend No commits on the branch yet Create an initial commit first — see Git init tutorial
git push rejected after amend Remote still has the old hash git push --force-with-lease when rewriting is intentional
--force-with-lease rejected Remote moved since your last fetch Fetch, inspect new remote commits, coordinate before replacing history
Amended the wrong commit Amend only ever hits HEAD Use reflog to return, then git rebase -i for older commits
Lost a commit after amend Branch no longer points at it Search git reflog for the old hash; git reset --soft or cherry-pick it back
Wanted message-only change Plain amend can change files if staged Use git commit --amend --only — see change a Git commit message

References


Summary

git commit --amend rebuilds the last commit on your branch — new hash, same branch tip role. Stage forgotten or fixed files, run git commit --amend --no-edit, and the message stays while the tree updates. Use --author when identity metadata was wrong, or --date when AuthorDate was wrong, and pair --no-edit so you do not open the editor unnecessarily.

Every amend creates a replacement commit, so a pushed branch needs git push --force-with-lease after you rewrite locally. If an amend went wrong, git reflog still lists the old tip; reset to the entry before the mistake. Amend never reaches commits below the latest — older fixes belong in interactive rebase, and message-only work belongs in change a Git commit message.


Frequently Asked Questions

1. What does git commit --amend do?

It rebuilds the most recent commit. You can change its files, message, author, or date. Git stores a new commit with a new hash and moves your branch to it; the old commit is no longer on the branch.

2. Can I amend a commit after pushing it to remote?

Yes, but the rewritten commit has a different hash, so you must push with git push --force-with-lease. Only do this on a branch you own or after coordinating with teammates.

3. How do I undo git commit --amend?

Run git reflog, find the entry from before the amend, and run git reset --soft to that reflog entry or commit hash. Use git reset --hard only if you also want to discard working-tree changes.

4. Can git commit --amend modify older commits?

No. Amend only ever touches the newest commit on your current branch. To change an older commit, use interactive rebase or the workflows in a commit-message guide.

5. Does git commit --amend create a new commit?

Yes. Git does not edit the existing commit in place. It creates a replacement commit with a new hash, even when you only change metadata such as the author or date.

6. What is the difference between git commit --amend and git commit --amend --only?

Plain amend can include files you already staged with git add. The --only flag limits the rewrite to the commit message and leaves the file snapshot unchanged; use that when you only need to fix wording.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)