0
0
Gitdevops~5 mins

Editing commit messages with rebase in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you make a commit message that is unclear or has a typo. Git lets you change past commit messages using a tool called rebase. This helps keep your project history clean and easy to understand.
When you want to fix a typo in the last few commit messages before sharing your work.
When you want to add more detail to a commit message to explain what you did.
When you realize a commit message is too vague and want to make it clearer.
When you want to reorder commits and update their messages at the same time.
When preparing your commits before pushing them to a shared repository.
Commands
Shows the last 5 commits in short form so you can see which commit messages you want to edit.
Terminal
git log --oneline -5
Expected OutputExpected
a1b2c3d Fix typo in README b2c3d4e Add user login feature c3d4e5f Update styles for homepage d4e5f6a Initial commit
--oneline - Shows each commit in one line for easy reading
-5 - Limits output to last 5 commits
Starts an interactive rebase for the last 3 commits so you can choose which commit messages to edit.
Terminal
git rebase -i HEAD~3
Expected OutputExpected
pick a1b2c3d Fix typo in README pick b2c3d4e Add user login feature pick c3d4e5f Update styles for homepage # Rebase 1234567..c3d4e5f onto 1234567 (3 commands) # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # # To change a commit message, replace 'pick' with 'reword' for that commit.
-i - Starts interactive mode to edit commits
HEAD~3 - Specifies the last 3 commits to rebase
After choosing to reword a commit, this command lets you change the commit message in your text editor.
Terminal
git commit --amend
Expected OutputExpected
No output (command runs silently)
After editing the commit message, this command tells git to continue applying the rest of the commits.
Terminal
git rebase --continue
Expected OutputExpected
Successfully rebased and updated refs/heads/main.
Checks the last 3 commit messages to confirm your changes were saved.
Terminal
git log --oneline -3
Expected OutputExpected
a1b2c3d Fix typo in README (edited) b2c3d4e Add user login feature c3d4e5f Update styles for homepage
--oneline - Shows commits in one line
-3 - Limits output to last 3 commits
Key Concept

If you remember nothing else from this pattern, remember: interactive rebase lets you safely change past commit messages before sharing your code.

Common Mistakes
Trying to edit commit messages after pushing to a shared repository without coordination.
Changing pushed commits rewrites history and can confuse others working on the project.
Only edit commit messages for commits that have not been pushed or coordinate with your team before rewriting history.
Using 'git rebase -i HEAD~3' but not changing 'pick' to 'reword' for the commit message you want to edit.
Git will not prompt you to edit the message if you leave 'pick' unchanged.
Change 'pick' to 'reword' for the commit(s) whose messages you want to edit.
Closing the editor without saving the new commit message during amend.
The commit message will not change if you do not save your edits.
Make sure to save and close the editor properly to apply the new commit message.
Summary
Use 'git rebase -i HEAD~N' to start editing the last N commits interactively.
Change 'pick' to 'reword' for commits whose messages you want to change.
Edit the commit message when prompted, then run 'git rebase --continue' to finish.
Verify changes with 'git log --oneline' to see updated commit messages.