How to modify existing, unpushed commit messages?
Categories:
How to Modify Existing, Unpushed Git Commit Messages

Learn how to correct typos, clarify descriptions, or completely rewrite commit messages for commits that haven't yet been pushed to a remote repository. This guide covers amending the last commit and interactively rebasing for older commits.
Accidentally made a typo in your last commit message? Or perhaps you've realized a previous commit's message isn't clear enough? Modifying commit messages is a common task in Git, especially when working on a local branch before pushing your changes to a shared remote repository. This article will guide you through the process of changing commit messages, distinguishing between the most recent commit and older commits in your history.
Understanding Git History Rewriting
Before diving into the commands, it's crucial to understand that modifying commit messages, especially for older commits, involves rewriting Git history. This is generally safe to do only for commits that exist solely in your local repository and have not been pushed to a shared remote. Pushing rewritten history can cause significant problems for collaborators who have already pulled the original commits, as it creates divergent histories. Always ensure you are working on unpushed commits when performing these operations.
flowchart TD A[Start] --> B{Commit Message Needs Change?} B -->|Yes| C{Is it the last commit?} C -->|Yes| D[Use `git commit --amend`] C -->|No| E[Use `git rebase -i`] D --> F[Commit message updated] E --> F F --> G{Pushed to Remote?} G -->|Yes| H[Avoid rewriting history] G -->|No| I[Safe to proceed] H --> J[End] I --> J
Decision flow for modifying Git commit messages.
Modifying the Last Commit Message
The easiest scenario is when you need to change the message of your very last commit, provided it hasn't been pushed yet. Git provides a straightforward command for this: git commit --amend
. This command doesn't create a new commit; instead, it replaces the previous commit with a new one that incorporates any staged changes and allows you to edit the commit message.
1. Step 1: Ensure no uncommitted changes
Before amending, make sure your working directory is clean or that any changes you want to include in the amended commit are staged. If you only want to change the message, ensure nothing is staged.
2. Step 2: Run the amend command
Execute git commit --amend
. This will open your default Git editor (e.g., Vim, Nano) with the previous commit message. Edit the message as desired, save, and close the editor.
3. Step 3: Verify the change
Use git log
to confirm that your last commit message has been updated. The commit hash will also change because a new commit object has effectively replaced the old one.
git commit --amend
# This will open your editor. Edit the message, save, and exit.
git log -1 --pretty=format:"%h %s"
Amending the last commit message and verifying the change.
git commit --amend
. If you have staged changes, they will be included in the amended commit.Modifying Older Commit Messages with Interactive Rebase
For commits that are not the most recent one, you'll need to use git rebase --interactive
(or git rebase -i
). This powerful command allows you to rewrite a series of commits, including changing their messages, reordering them, combining them, or splitting them. This is a more advanced operation and requires careful execution.
1. Step 1: Identify the commit to modify
You need to know the commit before the one you want to modify. For example, if you want to change the message of the 3rd commit from the top, you'll rebase starting from the 4th commit from the top (or HEAD~4
).
2. Step 2: Start an interactive rebase session
Run git rebase -i <commit-hash-or-ref>
. Replace <commit-hash-or-ref>
with the hash of the commit just before the one you want to modify. For example, to modify the 3rd last commit, you'd use HEAD~4
.
3. Step 3: Mark commits for editing
Your editor will open with a list of commits. For each commit whose message you want to change, replace pick
with reword
(or r
). Save and close the editor.
4. Step 4: Edit commit messages
Git will then step through each commit marked reword
. For each one, it will open your editor again, allowing you to change its message. Save and close for each commit.
5. Step 5: Complete the rebase
Once all messages are edited, Git will complete the rebase. If there are any conflicts, you'll need to resolve them. After resolution, use git rebase --continue
.
6. Step 6: Verify the changes
Use git log
to confirm that the commit messages have been updated. Note that all commit hashes from the rebased commit onwards will have changed.
# To modify the 3rd commit from HEAD (i.e., HEAD~2):
git rebase -i HEAD~4
# In the editor, change 'pick' to 'reword' for the desired commit(s):
# pick 1a2b3c4 Commit message to change
# pick 5d6e7f8 Another commit
# pick 9g0h1i2 Latest commit
#
# Change to:
# reword 1a2b3c4 Commit message to change
# pick 5d6e7f8 Another commit
# pick 9g0h1i2 Latest commit
# Git will then open the editor for each 'reword' commit.
# Edit the message, save, and exit.
git log --oneline
Using interactive rebase to modify an older commit message.
git push --force-with-lease
or git push -f
), but this should only be done with extreme caution and after coordinating with your team.