PRO 30% Free • Stripe ($15) & Razorpay (₹899)

Git Commit Amend: Complete Guide with Examples

Top Course Staff · 5 min read · Developer tools · PRO · 30% Free Preview Available

Git Commit Amend: Complete Guide with Examples

What isgit commit --amend?

Git commit amend modifies your most recent commit without creating a new one. It’s used to fix commit messages, add forgotten files, or remove unwanted files from the last commit.

This is particularly useful when we’ve just made a commit and quickly realize there was something missing—like an extra file, an incorrect message, or an unintended change.

Now that we know what thegit commit --amendcommand is, let’s take a look at some practical examples that showcase its usability, starting with modifying the last commit message.

Modify last commit message usinggit commit --amend

We’ve all been there—writing a commit message in a rush, only to realize afterward that it’s incomplete, misspelt, or just doesn’t clearly describe the changes made. Rather than creating an entirely new commit just to fix the message, Git provides a cleaner solution through thegit commit --amendcommand.

To edit the last commit message:

git commit --amend 
Copy to clipboard
git commit --amend

This command opens our default text editor with the most recent commit message pre-filled. We can now:

  • Correct typos
  • Add more detail
  • Follow our team’s commit message guidelines

Once we’re done editing, we save and close the editor. Git will overwrite the previous commit with the newly modified message.

Example:

Let’s say our last commit was:

git commit -m "Fix bug" 
Copy to clipboard
git commit -m "Fix bug"

But we realize that the commit message is too vague. So, we run:

git commit --amend 
Copy to clipboard

Then, update the message to:

Fix login bug causing session timeout on Safari 
Copy to clipboard
Fix login bug causing session timeout on Safari

This improved message gives a much better context.

To avoid opening an editor, we can directly amend the message inline using:

git commit --amend -m "Fix login bug causing session timeout on Safari" 
Copy to clipboard
git commit --amend -m "Fix login bug causing session timeout on Safari"

Now that we know how to update the most recent commit message usinggit commit --amend, let’s see how to use it to add forgotten files to the last commit.

Add files to the last commit usinggit commit --amend

It’s common to realize right after a commit that we forgot to include one or more files. Instead of creating a new commit just for these missing files, we can usegit commit --amendto append them to the previous commit, keeping the project history neat and intentional.

Let’s say we just committed some work but forgot to include a new fileutils.js. In that case:

Usegit addto include the missing file in the staging area:

git add utils.js 
Copy to clipboard
git add utils.js

We can also add multiple files if needed:

git add utils.js components.js 
Copy to clipboard
git add utils.js components.js

Then, if the original commit message is still appropriate, we can amend the commit without editing the message using the--no-editflag:

git commit --amend --no-edit 
Copy to clipboard
git commit --amend --no-edit

This will add the staged changes to our previous commit while keeping the message intact.

If we want to update the commit message too, we simply run the last command without the--no-editflag:

git commit --amend 
Copy to clipboard

But what if we accidentally included a file we didn’t intend to? Let’s see how to remove files from the last commit usinggit commit --amend.

Remove files from the last commit usinggit commit --amend

Sometimes, we realize we’ve committed files that shouldn’t have been included—like debug logs, build artifacts, or sensitive configuration files. Fortunately,git commit --amendalso allows us to remove files from the last commit without having to create a new one.

Let’s assume we’ve just committed a file calleddebug.logthat wasn’t supposed to be tracked. Here’s how to remove it:

Usegit resetto remove or unstage the file from the staging area:

git reset HEAD debug.log 
Copy to clipboard
git reset HEAD debug.log

This unstages the file so it won’t be included in the amended commit.

Then, update the commit:

git commit --amend --no-edit 
Copy to clipboard

This will remove the file from the most recent commit.

Just like in the previous example, if we want to edit the commit message too, we remove the--no-editflag:

git commit --amend 
Copy to clipboard

With these examples in mind, let’s consider when we should—and shouldn’t—usegit commit --amend.

When to usegit commit --amend

Usinggit commit --amendis most appropriate when:

  • The commit has not been pushed to a shared orremote repository.
  • We need to correct minor mistakes, like typos or forgotten files.
  • We want to maintain a clean history without unnecessary fix commits.

Avoid usinggit commit --amendafter pushing the commit unless you’re confident and ready to force-push, as it rewrites commit history, which can affect team members pulling from the same branch.

Lastly, let’s have a look at some best practices that we can follow to usegit commit --amendefficiently.

Best practices for usinggit commit --amend

Apply these best practices to make the most out ofgit commit --amend:

  • Amend only local commits: Never amend commits that are already pushed unless you coordinate with your team.
  • Use--no-editcarefully: Only use it when you’re sure the commit message doesn’t need changes.
  • Amend soon after committing: This minimizes conflicts and confusion.
  • Double-check staged changes: Always review your staged files before amending to avoid unintended updates.

Following these best practices will ensure effective usage ofgit commit --amend.

Conclusion

In this guide, we had a detailed discussion ongit commit --amend, covering what it is, why it matters, the different ways of using it, when to use it, and some best practices for using it efficiently.

Usinggit commit --amendwisely helps us maintain a clean and professional project history. It’s a powerful tool when used correctly, offering flexibility and control over our version tracking.

If you want to expand your knowledge of Git, check out theLearn Git & GitHubcourse on Codecademy.

Frequently asked questions

1. What is the difference between revert and amend in Git?

  • git commit --amendmodifies the most recent commit.
  • git revertproduces a new commit that reverts the changes of a previous commit.

Amend rewrites history; revert preserves it but negates the effect.

2. What to do aftergit commit --amend?

If the commit amended usinggit commit --amendis not pushed yet, you’re good to go. If you’ve already pushed the original commit, you will need to force push the changes:

git push --force 
Copy to clipboard
git push --force

3. Can I undo agit commit --amend?

Yes, you can undo agit commit --amend. Before amending, the previous commit is stored in Git’s reflog. You can recover it using:

git reflog git reset --hard <commit_hash> 
Copy to clipboard
git reflog git reset --hard <commit_hash>

This resets your branch to the state before the amend.

4. How do I cancel my last commit?

There are different commands for different scenarios:

  • Cancel last commit and keep changes staged:git reset --soft HEAD~1
  • Cancel last commit and keep changes unstaged:git reset --mixed HEAD~1
  • Cancel last commit and discard changes completely:git reset --hard HEAD~1

5. How to amend the 2nd last commit?

  • Start an interactive rebase:git rebase -i HEAD~2
  • Changepicktoeditfor the 2nd last commit
  • Amend the commit:git commit --amend
  • Continue rebase:git rebase --continue

'The Codecademy Team, composed of experienced educators and tech experts, is dedicated to making tech skills accessible to all. We empower learners worldwide with expert-reviewed content that develops and enhances the technical skills needed to advance and succeed in their careers.'

30% Free Preview Reached · Unlock Remaining 70%

Complete Payment to Unlock Full Guide & Source Code

You are viewing the 30% preview. Join TopCourseAI Pro to unlock the remaining 70% of this article, unblur all code snippets, and gain full access to all 412 technical projects and guides.

✓ Full Access to All 160+ Projects
✓ Instant Blur Removal Everywhere
✓ Production Source Code & Blueprints
✓ All Future Deep Dives Included
Instant Unhindered Activation ·
End of Preview · Unlock Complete Blueprint & Source Code

Ready to access the complete step-by-step tutorial?

Gain full unhindered access to all 412 technical projects and guides, unblur all source code snippets, and download runnable scripts.

Production AI Blueprints

Ready to build production-grade AI systems?

Enjoyed this guide? Continue exploring our library of 166+ production blueprints, multi-agent swarms, and autonomous agent systems with 100% unblurred, executable source code.