How to Modify Your Last Git Commit Without Creating a New Commit
- Published on
- -3 mins read
- Authors
- Name
- Muhammad Iqbal
- @dibaliqaja
Introduction
There are times when you realize you need to tweak something in your code after committing it — maybe it’s a small typo, a missing line, or even just a formatting fix. The tricky part? You’ve already pushed that commit to your remote repository.
In situations like this, creating a new commit just for a tiny fix can make your commit history look messy. Ideally, we want to keep things clean and easy to follow.
So, how do we update the last commit without creating a new commit?
Let’s Get Started
Let’s assume you’re working on your own feature branch and your commit history looks like this:
be43bz4 feat: add update for city master data324dsa2 docs: add explanation notesa53da32 chore: update local environment settings Now, suppose you notice a typo in the latest commit (be43bz4). If you fix it and create a new commit, your history will look like this:
a2d4ls3 fix: typo in city titlebe43bz4 feat: add update for city master data324dsa2 docs: add explanation notesa53da32 chore: update local environment settings While this is technically fine, it can clutter your commit history — especially for small fixes.
Instead, we can update the last commit directly using Git’s amend feature.
Using git commit --amend
First, stage your changes as usual:
git add .Then, run the following command:
git commit --amendAfter running this, your terminal will open an editor showing the previous commit message. It will look something like this:
# Please enter the commit message for your changes. Lines starting# with ‘#’ will be ignored, and an empty message aborts the commit. ## Author: Muhammad Iqbal <dibaliqaja@gmail.com># Date: Sat May 22 21:20:43 2022 -0300## On branch some_branch# Your branch is up-to-date with ‘origin/features/update-city.## Changes to be committed:# modified: city.goAt this point, you can either update the commit message or keep it as it is. If you don’t want to change anything, simply save and exit the editor.
Now your latest commit includes the new changes — without creating a new commit.
Skip Editing the Commit Message
If you don’t want to open the editor at all, you can use --no-edit like this:
git commit --amend --no-editThis will update the commit while keeping the existing message unchanged.
Pushing the Changes
Since you’ve rewritten the last commit, you’ll need to force push:
git push -f origin features/update-city Important Note:
Using -f (force push) will overwrite the remote history. Make sure you’re pushing to the correct branch.
Also, this technique should only be used on your own branches. Avoid rewriting commit history on shared branches like main or master, as it can disrupt your team’s workflow.
Final Thoughts
Using git commit --amend is a simple yet powerful way to keep your commit history clean and meaningful. It’s especially useful for small fixes that don’t deserve a separate commit.
Just remember: with great power comes great responsibility — use it carefully, especially when working with others.
That’s all. I hope this was helpful. See you later.