corregir-errores-con-git-commit-amend

Fixing the Last Commit in Git with Amend

  • 3 min

The modifier git commit —amend is a command that allows us to rewrite the last commit, integrating forgotten changes or correcting the message.

The typical situation. You’ve just made a commit, and two seconds later you realize you’ve misspelled the message. You wrote “Implemnetar” instead of “Implementar” 😤.

Or worse, you’ve made the commit and see that you’ve left out an important file you wanted to be in that commit.

We could solve this by creating a new additional commit for minor corrections. But, if it really doesn’t add anything, you just clutter the history.

For these cases (mainly fixing silly mistakes) Git provides the git commit --amend option.

Scenario 1: Correct the commit message

This is the simplest case. The file content is fine, but the message text has an error or doesn’t follow team standards.

To change it, simply run:

git commit --amend -m "New corrected and nice message"
Copied!

Git will take the last commit, change its description to the new one and update the history (No one will ever know you wrote ‘HARREGLADO’ with an ‘H’ 😎).

If you run git commit --amend without the -m parameter, your configured text editor (Vim/VS Code) will open with the old message, allowing you to edit it comfortably.

Scenario 2: Add a forgotten file

Imagine you’ve made a commit “Create button component”, but you’ve left the boton.css file without adding it to the Stage.

Instead of making a new commit, follow these steps:

  1. Add the forgotten file to the Stage as you normally would:
git add boton.css
Copied!
  1. Run the amend command with the --no-edit option:
git commit --amend --no-edit
Copied!

This flag tells Git: “Use the same message the previous commit already had, I don’t want to change the text, I just want to put this new file inside that commit”.

The result is a single commit containing both the original files and the forgotten one.

What happens internally?

Although it looks like we’ve “edited” the commit, Git doesn’t edit anything. Commits are immutable, they are never modified.

What amend actually does is:

  1. Takes the original commit and temporarily undoes it.
  2. Adds the new changes (if any).
  3. Creates a NEW commit that replaces the previous one (with a new SHA-1 Hash).

This means commit a1b2c3d disappears and is replaced by f9e8d7c.

Since amend changes the commit’s Hash (rewrites history), burn this into your mind:

NEVER use --amend on a commit you have already pushed to the server.

If you’ve done git push and your colleagues have already downloaded that commit, changing it on your computer will cause a serious history conflict.

To fix it you’ll have to do a Force Push, and if you work in a team you’ll have a huge mess.