Git Commit Amend: Complete Guide with Examples
What is git 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 the git commit --amend command 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 using git 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 the git commit --amend command.
To edit the last commit message:
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"
But we realize that the commit message is too vague. So, we run:
git commit --amend
Then, update the message to:
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"
Now that we know how to update the most recent commit message using git commit --amend, let’s see how to use it to add forgotten files to the last commit.
Add files to the last commit using git 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 use git commit --amend to append them to the previous commit, keeping the project history neat and intentional.
Example:
Let’s say we just committed some work but forgot to include a new file utils.js. In that case:
Use git add to include the missing file in the staging area:
git add utils.js
We can also add multiple files if needed:
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-edit flag:
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-edit flag:
git commit --amend
But what if we accidentally included a file we didn’t intend to? Let’s see how to remove files from the last commit using git commit --amend.
Remove files from the last commit using git 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 --amend also allows us to remove files from the last commit without having to create a new one.
Example:
Let’s assume we’ve just committed a file called debug.log that wasn’t supposed to be tracked. Here’s how to remove it:
Use git reset to remove or unstage the file from the staging area:
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
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-edit flag:
git commit --amend
With these examples in mind, let’s consider when we should—and shouldn’t—use git commit --amend.
When to use git commit --amend
Using git commit --amend is most appropriate when:
- The commit has not been pushed to a shared or remote repository.
- We need to correct minor mistakes, like typos or forgotten files.
- We want to maintain a clean history without unnecessary fix commits.
Avoid using git commit --amend after 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 use git commit --amend efficiently.
Best practices for using git commit --amend
Apply these best practices to make the most out of git 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 of git commit --amend.
Conclusion
In this guide, we had a detailed discussion on git 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.
Using git commit --amend wisely 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 the Learn Git & GitHub course 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 after git commit --amend?
If the commit amended using git commit --amend is 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
3. Can I undo a git commit --amend?
Yes, you can undo a git commit --amend. Before amending, the previous commit is stored in Git’s reflog. You can recover it using:
git refloggit 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 - Change
picktoeditfor 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.'
Meet the full teamRelated articles
- Article
How to Revert to Previous Commits Using Git Reset and Revert
Learn how to roll back to previous commits in Git using git reset and git revert commands. Step-by-step guide to undo changes and manage your commit history effectively. - Article
Handy Git Operations
Git provides us with a vast number of different commands that are listed on the documentation which can be intimidating at first. We will break down a couple that are powerful for daily tasks. - Article
What is Git Reset? Explained with Examples
Learn what Git reset is and how it works. Explore types, examples, and best practices for managing commits safely.
Learn more on Codecademy
- Learn how to track changes in your code and switch between different versions with Git, an open-source version control system.
- Beginner Friendly.1 hour
- Become proficient in Git for DevOps, including repository management, Git commands, GUI, distributed workflows, branching, Git server protocols, and Gitflow.
- Intermediate.1 hour
- Learn how to create, merge, clone, and fetch Git branches to collaborate with other developers.
- Beginner Friendly.1 hour
- What is `git commit --amend`?
- Modify last commit message using `git commit --amend`
- Add files to the last commit using `git commit --amend`
- Remove files from the last commit using `git commit --amend`
- When to use `git commit --amend`
- Best practices for using `git commit --amend`
- Conclusion
- Frequently asked questions