How to modify existing, unpushed commit messages?
I wrote the wrong thing in a commit message. How can I change the message? The commit has not been pushed yet.
To modify existing, unpushed commit messages in Git, you can use git commit --amend for the most recent commit or git rebase -i for older commits. These commands allow you to rewrite commit history safely before pushing to remote repositories.
Contents
- Modifying the Most Recent Commit
- Editing Older Commits with Interactive Rebase
- Practical Examples and Workflows
- Best Practices and Considerations
- Troubleshooting Common Issues
Modifying the Most Recent Commit
When you need to change the message of your most recent unpushed commit, git commit --amend is the simplest and most direct approach. This command lets you modify the commit message without changing the actual changes in the commit.
Basic Usage
The basic command to amend the most recent commit’s message is:
git commit --amend
This will open your default text editor with the current commit message, allowing you to edit it. After saving and closing the editor, Git will create a new commit with the updated message.
Command Line Editing
If you prefer to specify the new message directly from the command line:
git commit --amend -m "Your corrected commit message here"
Keeping Changes but Only Editing Message
If you’ve made additional changes that you want to include in the same commit while also changing the message:
git add . # Stage any new changes
git commit --amend -m "Updated commit message with new changes"
Important: Remember to only use
--amendon unpushed commits. Modifying pushed commits can cause issues for collaborators and requires more complex workflows.
Editing Older Commits with Interactive Rebase
For commits that aren’t the most recent, you’ll need to use interactive rebase (git rebase -i). This powerful tool allows you to edit multiple aspects of your commit history, including messages, order, and even splitting or combining commits.
Starting Interactive Rebase
To begin an interactive rebase session, specify how many commits back you want to edit:
git rebase -i HEAD~3 # Edit the last 3 commits
Or specify a specific commit hash:
git rebase -i abc123 # Edit all commits since abc123
Interactive Rebase Editor Interface
When you run the interactive rebase command, Git opens a text editor showing a list like this:
pick a1b2c3d Add new feature
pick e4f5g6h Fix typo in README
pick i7j8k9l Update documentation
# Rebase abc123..i7j8k9l onto abc123 (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# . create a merge commit using the original merge commit's
# . message (or the oneline, if no original merge commit was
# . specified). Use -c <commit> to re-use the original merge
# . commit's author and message.
#
# These lines can be re-ordered; they are executed from top to bottom.
Modifying Specific Commit Messages
To change a commit message, change pick to reword (or r) next to the commit you want to modify:
pick a1b2c3d Add new feature
reword e4f5g6h Fix typo in README # Changed from "pick" to "reword"
pick i7j8k9l Update documentation
Save and close the editor. Git will then pause and open an editor for each commit marked with reword, allowing you to edit its message individually.
Alternative: Edit Command
If you need to make actual changes to a commit (not just the message), use edit (or e) instead of reword:
pick a1b2c3d Add new feature
edit e4f5g6h Fix typo in README # Git will stop here for you to amend
pick i7j8k9l Update documentation
After saving, Git will pause at that commit. You can then:
- Make any necessary file changes
- Stage them with
git add - Amend the commit with
git commit --amend - Continue the rebase with
git rebase --continue
Practical Examples and Workflows
Example 1: Fixing a Single Message
Let’s say you have these commits:
commit a1b2c3d Add new feature
commit e4f5g6h Fix typo in README # Wrong message
commit i7j8k9l Update documentation
To fix the typo in the README commit message:
git rebase -i HEAD~2
# Change "pick e4f5g6h" to "reword e4f5g6h"
# Save and close
# Edit the message when prompted
# Save and close again
Example 2: Fixing Multiple Messages
If you need to fix several commit messages:
git rebase -i HEAD~5
# Change all the commits you want to modify from "pick" to "reword"
# Save and close
# Git will open an editor for each reword commit one by one
Example 3: Combining Multiple Commits
Sometimes you might want to combine several commits with related changes:
git rebase -i HEAD~3
# Change to:
# pick a1b2c3d Add new feature
# squash e4f5g6h Fix typo in README
# pick i7j8k9l Update documentation
Git will combine the second commit with the first and ask you to create a new combined commit message.
Complete Workflow Example
Here’s a step-by-step workflow for editing an older commit:
-
Check your commit history:
bashgit log --oneline -
Start interactive rebase (let’s say you want to edit the 3rd commit from the top):
bashgit rebase -i HEAD~3
-
Modify the rebase file:
pick a1b2c3d Add new feature pick e4f5g6h Fix typo in README reword i7j8k9l Update documentation # Changed to reword -
Save and close the editor
-
Edit the commit message when Git opens the editor for the reworded commit
-
Save and close again
-
Verify the changes:
bashgit log --oneline
Best Practices and Considerations
Safety First
- Never modify pushed commits: Changing commit messages that have been pushed to shared repositories can cause problems for collaborators
- Always work on feature branches: Keep your main/master branch clean and only modify commits on feature branches
- Pull latest changes before rebasing: Ensure your local branch is up-to-date with the remote
Commit Message Standards
According to Atlassian’s Git tutorial, good commit messages should:
- Be concise and descriptive
- Use the imperative mood (“Add feature” not “Added feature”)
- Include relevant context and reasoning
- Reference related issues when applicable
Backup Your Work
Consider creating a backup branch before major rebase operations:
git branch backup-branch
This way, if something goes wrong, you can always return to the original state.
Collaborative Workflows
When working with others:
- Communicate with your team before rewriting history
- Use
git push --force-with-leaseinstead ofgit push --forcewhen necessary - Consider using merge commits for shared branches to avoid complex rebases
Troubleshooting Common Issues
Conflicts During Rebase
If you encounter merge conflicts during rebase:
- Resolve the conflicts in your files
- Stage the resolved files with
git add - Continue the rebase with
git rebase --continue - If needed, abort with
git rebase --abort
Editor Not Opening
If your text editor doesn’t open during interactive rebase:
export EDITOR=vim # Or your preferred editor
git rebase -i HEAD~3
Mistaken Changes
If you make a mistake during rebase:
- Use
git rebase --abortto cancel and return to the original state - Or use
git reflogto find previous commits and reset to them
Large Rebase Operations
For many commits, consider breaking it into smaller operations:
git rebase -i HEAD~10 # Work in batches of 10
Conclusion
Modifying unpushed commit messages is a safe and common Git workflow that every developer should master. The git commit --amend command provides a quick way to fix recent commits, while git rebase -i offers powerful capabilities for editing older commits in your history. Always remember to only modify unpushed commits to avoid disrupting your team’s workflow, and consider creating backups before major rebase operations. With these tools and practices, you can maintain clean and professional commit histories that accurately reflect your development process.
Sources
- Stack Overflow - How to modify existing, unpushed commit messages?
- GeeksforGeeks - How To Modify Existing, Unpushed Commit Messages in Git
- Atlassian Git Tutorial - Rewriting History
- GitHub Docs - Using Git rebase on the command line
- SitePoint - A Guide to Git Interactive Rebase
- Nick Janetakis - Change a Git Commit in the Past with Amend and Rebase Interactive
- DataCamp - Git Amend Explained