How to Edit a Specific Commit Message in Git

Umar Farooque Khan
2 min readApr 18, 2024

--

If you want to modify the commit message of a specific commit (not the most recent one), you can use the git rebase command with the interactive mode. Here's how you can do it:

Start an interactive rebase:

Begin an interactive rebase from the commit before the one you want to modify. You can specify the commit hash (SHA) or a reference such as HEAD~n where n is the number of commits before HEAD that you want to rebase from.

Example
if you want to modify a commit that is 3 commits back from HEAD, use:

git rebase -i HEAD~2

Locate the commit you want to edit:

In the interactive rebase file that opens, you will see a list of commits. Locate the commit you want to modify and change the word “pick” at the beginning of the line to “reword”:

pick <commit-hash> Commit message

Change to:

reword <commit-hash> Commit message

Save and exit:

Once you’ve marked the commit you want to reword, save the file and exit the editor.

Edit the commit message:

After you exit the editor, Git will prompt you to modify the commit message for the specified commit. Edit the commit message as desired and save the changes.

Continue with the rebase:

After modifying the commit message and saving your changes, Git will continue with the rebase process and allow you to resolve any conflicts that may arise.

Finish the rebase:

Once you have completed the rebase process, you may need to force push the changes to your remote repository, especially if the rebased commits have already been pushed:

git push --force

Remember that force-pushing can overwrite changes made by others, so use it with caution and ensure that you communicate with your team if you’re working in a collaborative environment.

--

--

Umar Farooque Khan
Umar Farooque Khan

Written by Umar Farooque Khan

Experienced software developer with a passion for clean code and problem-solving. Full-stack expertise in web development. Lifelong learner and team player.

No responses yet