Sub topic 5
Fixing messages and contents with reword and edit
This chapter assumes you can start git rebase -i, read the todo list, and use git add, git commit and git status without help. It covers the two todo commands that change a single commit: reword and edit.
Choosing between reword and edit
Both commands replace one commit with a new one. They differ in what you can change.
| Command | What you can change | Where Git pauses |
|---|---|---|
| `reword` | The commit message only | In your editor, once, with the message open |
| `edit` | Message, files, author, or anything else | At a shell prompt, after the commit is applied |
Use reword for a typo or a vague message. Use edit when you forgot a file, committed something you should not have, or want to split the commit.
Rewording a message
Say git log --oneline -n 5 shows a commit with a poor message:
a6b2c33 Add login tests
51ab9c0 stuff
8c4d7e2 Fix typo in login form
3f2a1b9 Add login formStart the rebase from the parent of the oldest commit you want to touch:
git rebase -i HEAD~3In the todo list, change pick to reword (or r) on the weak commit and save:
pick 8c4d7e2 Fix typo in login form
reword 51ab9c0 stuff
pick a6b2c33 Add login testsGit applies the first commit unchanged, then opens your editor on stuff. Write the new message, save, and close. Git continues on its own and replays the last commit.
Every commit after a reworded one gets a new hash too, because each commit records its parent's hash. The content is the same, but the history is a new chain.
Changing a commit's contents with edit
Change pick to edit (or e) on the commit you want to change:
pick 8c4d7e2 Fix typo in login form
edit 51ab9c0 stuff
pick a6b2c33 Add login testsGit applies that commit and stops. It prints a message like Stopped at 51ab9c0... stuff. Your branch now ends at that commit, and the later commits are waiting.
Make your change, stage it, and fold it into the commit with --amend:
git status
# edit src/login.js, then:
git add src/login.js
git commit --amend -m "Add validation for the login form"
git rebase --continueIf you leave out -m, Git opens the editor with the old message. You can also use git commit --amend --author="Name <[email protected]>" to fix the author.
Common errors
- Creating a new commit instead of amending. If you run
git commitwithout--amendduring aneditstop, you add an extra commit after the original. This is not always wrong, and it is how you split a commit, but it is not what you want for a simple fix. - Forgetting
--continue. The rebase stays paused.git statusshowsinteractive rebase in progressand the commits still to apply. - Editing the wrong commit. The todo list runs oldest first, the reverse of
git log. Check the hash against the list before you save. - Conflicts after an edit. If your change collides with a later commit, the rebase stops with a conflict when it replays that commit. The next chapters cover how to handle that.
Exercise
Exercise
You run git rebase -i HEAD~3 and mark the middle commit edit. Git stops. You fix a file, run git add, and then run git rebase --continue without running git commit --amend. What is the result, and how do you make the change part of the original commit?
Show answer
Git does not refuse to continue when changes are staged. Depending on your Git version, it either stops and asks you to commit or amend the staged changes, or it refuses to continue because of uncommitted changes. Either way, the change is not yet part of the commit. Run git commit --amend (with the changes staged) to fold it into the original commit, then run git rebase --continue.