You messed up your last Git commit and want to fix it without making a whole new commit. This usually means you forgot to add a file, made a typo in the commit message, or want to combine a few small changes into that last commit.

Here’s how to fix your last commit, depending on what you need to change:

Scenario 1: You just committed and want to add more files or make more code changes

  • Diagnosis: You’ve run git status and see unstaged changes you meant to include in the last commit.
  • Common Causes & Fixes:
    1. Forgot to stage files: You made changes but didn’t git add them before git commit.
      • Diagnosis: git status shows modified files under "Changes not staged for commit".
      • Fix: git add . (or git add <specific_file>) followed by git commit --amend --no-edit.
      • Why it works: git add stages the new changes. git commit --amend --no-edit takes those staged changes and replaces the last commit with a new one that includes them, without changing the commit message.
    2. Made more code changes after committing: You realized you missed something in the code itself.
      • Diagnosis: git status shows modified files.
      • Fix: git add . (or git add <specific_file>) followed by git commit --amend --no-edit.
      • Why it works: Same as above. The new code changes are staged and then incorporated into the existing commit via --amend.
    3. Typo in the commit message: You just committed and saw a glaring typo in the message.
      • Diagnosis: You see the typo immediately after committing.
      • Fix: git commit --amend. This will open your configured editor with the last commit message. Edit it, save, and close the editor.
      • Why it works: git commit --amend rewrites the last commit. By default, it uses the last commit’s changes and lets you edit its message.
    4. Need to add files AND edit the message: A combination of the above.
      • Diagnosis: Unstaged changes and a desire to edit the message.
      • Fix: git add . (or git add <specific_file>) followed by git commit --amend. Edit the message in the editor, save, and close.
      • Why it works: Staging the new files first, then using git commit --amend incorporates both the staged changes and allows for message editing in one go.
    5. Accidentally committed too much: You committed changes that should have been separate or reverted.
      • Diagnosis: git log shows the last commit has changes you don’t want.
      • Fix: git reset HEAD~1. This unstages everything in the last commit, moving it back to your working directory. Then, git add <files_you_want_in_commit> and git commit -m "Your correct commit message". For files you don’t want at all, just leave them unstaged.
      • Why it works: git reset HEAD~1 effectively undoes the last commit but keeps the changes in your working directory. You can then re-select what to commit.
    6. Commit was empty or contained wrong files: You committed and now realize it’s not what you intended, but the message might be okay.
      • Diagnosis: git log -p -1 shows no changes or incorrect changes.
      • Fix: git reset HEAD~1. Then, git add <correct_files> and git commit -m "Your original commit message".
      • Why it works: Similar to the previous point, this unstages everything from the bad commit, allowing you to re-stage and re-commit correctly.

Important Considerations:

  • --amend rewrites history: If you’ve already pushed the commit you’re amending, you’ll need to force push (git push --force-with-lease) to update the remote. Be very careful with this on shared branches, as it can cause issues for collaborators. It’s generally safe for your own feature branches that haven’t been pulled by others.
  • git reset HEAD~1 vs. git reset --hard HEAD~1: git reset HEAD~1 (or git reset HEAD~1) keeps your changes in your working directory. git reset --hard HEAD~1 discards all changes from the last commit, which you likely don’t want if you’re trying to fix it.

The next error you’ll hit after fixing this is likely a merge conflict if you’ve pushed and pulled from a remote repository without force-pushing correctly.

Want structured learning?

Take the full Git course →