It happens to everyone: you make a commit, and then immediately realize you misspelled something in the commit message, or perhaps you forgot to include a small change or a related file. Instead of creating a brand new commit just to fix the previous one, Git provides a convenient way to revise your most recent commit: the git commit --amend
command.
Think of git commit --amend
as a way to say, "Actually, this is what I meant for the last commit." It doesn't technically edit the previous commit (commits in Git are generally considered immutable snapshots). Instead, it takes your currently staged changes, combines them with the changes from the commit you're amending, and creates an entirely new commit that replaces the original last one.
git commit --amend
WorksWhen you run git commit --amend
, Git does the following:
git add
).This is perhaps the most frequent use. If you just made a commit and spotted a typo in the message:
# No changes staged, just amending the message
git commit --amend
This command will open your configured text editor (like Vim, Nano, VS Code, etc.), showing the previous commit message. Simply edit the message, save the file, and close the editor. Git will create the new commit with the corrected message.
Alternatively, you can provide the new message directly on the command line using the -m
option:
git commit --amend -m "Fix: Correct variable name in calculation"
Another common scenario is realizing you forgot to stage a file or a specific change that logically belonged with the last commit.
First, make the necessary changes to your file(s).
Stage these new changes using git add
:
# Make corrections to style.css
git add style.css
Now, run git commit --amend
. Git will combine the newly staged style.css
changes with the changes that were already in the original last commit.
git commit --amend
This will typically open your editor again, allowing you to refine the commit message if needed (since you added more changes). If you want to keep the original commit message exactly as it was, you can add the --no-edit
flag:
# Make corrections to style.css
git add style.css
# Amend the last commit, adding style.css, but keep the original message
git commit --amend --no-edit
It can be helpful to visualize how amending changes the commit history.
Before amending, the
main
branch points to Commit B, which has an error. After runninggit commit --amend
(perhaps after fixing the message or adding a file), themain
branch now points to a completely new commit, B', which incorporates the desired changes. The original Commit B is no longer part of themain
branch's direct history.
There's a significant caution with git commit --amend
: You should generally avoid amending commits that you have already pushed to a shared remote repository.
Remember, git commit --amend
rewrites the history of your branch by replacing the last commit. If others have already pulled the original commit that you later replace with an amended version, their history will diverge from yours. When they try to pull new changes, or you try to push your amended commit, Git will see conflicting histories, leading to confusion and potentially complex merge scenarios. Pushing amended history usually requires a "force push" (git push --force
or git push --force-with-lease
), which can overwrite others' work and is often discouraged in collaborative workflows.
Rule of Thumb: Use git commit --amend
freely for commits that exist only in your local repository. Once a commit has been shared (pushed), it's usually better to create a new commit that fixes the issue (using git revert
, which we'll discuss later, or simply adding a corrective commit) rather than amending the shared history.
Amending is a powerful tool for keeping your local commit history clean and logical before sharing it with others. Use it to fix typos or bundle small, related changes into the immediately preceding commit.
© 2025 ApX Machine Learning