DEV Community

Cover image for How to Fix a Git Commit Message 3 Steps Back (Without Losing Your Mind)
Md. Abdur rahman
Md. Abdur rahman

Posted on

How to Fix a Git Commit Message 3 Steps Back (Without Losing Your Mind)

We’ve all been there: you’ve made a few great commits, you’re about to wrap up, and then you see it—a typo in a commit message from three steps ago. A simple commit --amend won't reach that far back.

If you're on Windows and the thought of using the "Vim" editor in the terminal makes you nervous, here is the easiest way to fix your history using Notepad.

Step 1: Tell Git to use Notepad
Before you start, run this command. It tells Git to open all edit windows in Notepad instead of the confusing terminal editor:

git config --global core.editor "notepad"
Enter fullscreen mode Exit fullscreen mode

Step 2: Start the Interactive Rebase
We want to look at the last three commits:

git rebase -i HEAD~3
Enter fullscreen mode Exit fullscreen mode

Step 3: Reword the Commit
A Notepad window will pop up. You’ll see your last 3 commits listed. Find the one you want to fix and change the word pick to reword.

  • Save (Ctrl + S) and Close the Notepad window.

Step 4: Fix the Message
Immediately, a second Notepad window will open showing only that one commit message.

  • Delete the typo, type the correct message, Save (Ctrl + S), and Close it.

Step 5: Handling the Remote Push
If those commits were already on GitHub, a regular git push will be rejected. Since you've rewritten history, you need to "force" the update:

# Replace 'main' with your branch name
git push origin yourbranch --force
Enter fullscreen mode Exit fullscreen mode

Note: Only do this if you are working alone on this branch!

Top comments (0)