Lesson

Git Amend

10 min reading
Free Course

Git Amend: Fixing the Most Recent Commit Cleanly

git commit --amend replaces the very last commit on your current branch with a new combined commit. Use it when you spot a typo in your commit message or forget to stage a file like styles.css right after committing.

Folding a Forgotten File into the Last Commit

Suppose you ran git commit for index.html and immediately noticed styles.css was still untracked or unstaged. Stage the missing file and pass --no-edit to keep the original commit message unchanged:

git add styles.css
git commit --amend --no-edit

Git folds styles.css into the previous snapshot without creating a noisy "forgot css file" follow-up commit.

Fixing Only the Commit Message

If your staged files were fine and you only want to reword the message of the latest commit, run:

git commit --amend -m "Add responsive header navigation in index.html"

You can also fix the commit author identity after updating git config user.email by running:

git commit --amend --reset-author --no-edit

Amending on a Personal Feature Branch vs Shared Branches

Because git commit --amend generates a brand-new commit SHA-1 hash, never amend commits that teammates have already pulled from main. On your own unshared pull request branch, push the amended commit safely with --force-with-lease:

git push --force-with-lease origin feature/header-nav

Common Amending Mistakes

Using git push --force instead of --force-with-lease can overwrite commits if a CI bot or teammate pushed to your PR branch moments earlier. --force-with-lease checks the remote branch tip first and aborts safely if anything changed.

Frequently Asked Questions

Does git commit --amend edit older commits before the latest one?

No. --amend only modifies the tip (HEAD) commit of your current branch. To edit commits further back in history, use interactive rebase (git rebase -i).

Can I amend a commit if I have nothing new staged?

Yes. Running git commit --amend -m "Updated message" with no staged changes keeps the exact same file snapshot and only updates the message.

What happens if I accidentally run --amend when I meant to create a second commit?

Run git reflog to locate the hash of your original commit before the amend, create a branch or reset back to it, and then commit your new changes normally.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol
Git Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum