Git and GitHub · Part 2 · Branches and merging
Interactive rebase
The problem
Committing often is a good habit, but it leaves a messy history. Before pushing and opening a pull request, it should go from this:
6760c2c debugging
15baf33 Add password reset link
ec2e7d8 fix typo
8b27d6e more stuff
dea73f7 WIP
190b71b add login form066ae40 Add login form
4466ab4 Add password reset linkInteractive rebase does that. It lists recent commits, and an instruction next to each one decides what happens to it:
| Instruction | What it does |
|---|---|
pick | Keep the commit as it is |
reword | Change its message |
squash | Combine it into the commit above, and write a new message |
fixup | Combine it into the commit above, and drop its message |
drop | Delete it |
edit | Stop at it, to change its contents or split it |
| Moving lines | Changes the order of commits |
A messy history to clean up
cd ~/Desktop
mkdir irebase-lab
cd irebase-lab
git init
echo "app" > app.txt
git add app.txt
git commit -m "Initial commit"
echo "<form></form>" > login.html
git add login.html
git commit -m "add login form"
echo "<input type='email'>" >> login.html
git commit -am "WIP"
echo "<input type='pasword'>" >> login.html
git commit -am "more stuff"
sed -i '' 's/pasword/password/' login.html
git commit -am "fix typo"
echo "<a>Forgot password?</a>" > reset.html
git add reset.html
git commit -m "Add password reset link"
echo "console.log('debug')" >> login.html
git commit -am "debugging"git log --oneline shows the six messy commits from above, on top of beb1792 Initial commit.
The todo list
git rebase -i HEAD~6-i means interactive, and HEAD~6 means the last six commits (git rebase -i beb1792, everything after that commit, does the same). The editor opens with the list:
pick 190b71b # add login form
pick dea73f7 # WIP
pick 8b27d6e # more stuff
pick ec2e7d8 # fix typo
pick 15baf33 # Add password reset link
pick 6760c2c # debugging
# Rebase beb1792..6760c2c onto beb1792 (6 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
# d, drop <commit> = remove commit- Oldest first. The list is a set of instructions, run top to bottom, so it's the reverse of
git log. - Saving it unchanged keeps everything as it is. Deleting every line and saving cancels the rebase.
- Older versions of Git leave out the
#before each message. It works the same.
Using each instruction
1.drop: delete a commit
git rebase -i HEAD~1drop 6760c2c # debuggingSave and close. The commit is gone, and so is the console.log line it added. (Deleting the line does the same, but drop is clearer.)
2.reword: change a message
git rebase -i HEAD~5reword 190b71b # add login form
pick dea73f7 # WIP
pick 8b27d6e # more stuff
pick ec2e7d8 # fix typo
pick 15baf33 # Add password reset linkSave and close. A second editor opens with the old message; change it to Add login form, then save and close.
96f16ae Add password reset link
6bfbd68 fix typo
26ae188 more stuff
ce84238 WIP
b99e10f Add login form
beb1792 Initial commitEvery commit after the reworded one has a new hash too, because its parent changed. git commit --amend can only fix the last commit; reword fixes any recent one.
3.fixup and squash: combine commits
The most used one. “WIP”, “more stuff” and “fix typo” are all part of the login form, so fold them into it:
git rebase -i HEAD~5pick b99e10f # Add login form
fixup ce84238 # WIP
fixup 26ae188 # more stuff
fixup 6bfbd68 # fix typo
pick 96f16ae # Add password reset link007eadd Add password reset link
eccb460 Add login form
beb1792 Initial commitFour commits became one, with all their changes. git show HEAD~1 shows them together.
| The combined commit's message | |
|---|---|
fixup | Keeps the message above, and drops this one. For junk like “WIP”. |
squash | Opens the editor with every message, to write a new one. For messages worth keeping. |
For example, squashing three commits:
pick 1a2b3c4 # Add search input
squash 5d6e7f8 # Add search button
squash 9a8b7c6 # Connect search to APIThe editor shows all three messages, and they can become one:
Add search feature
- Search input with debounce
- Search button
- Connected to /api/search4.Reorder: move the lines
git rebase -i HEAD~2pick 007eadd # Add password reset link
pick eccb460 # Add login form51d0b86 Add login form
6cd6d24 Add password reset link
beb1792 Initial commitIf a commit depends on one that now comes after it, for example it edits a line the other one creates, reordering causes a conflict. Resolve it as in Lesson 13 (git add, then git rebase --continue) or back out with --abort. Moving a fix right under the commit it belongs to and making it a fixup is a common combination.
5.edit: change an old commit
git rebase -i HEAD~2edit 6cd6d24 # Add password reset link
pick 51d0b86 # Add login formStopped at 6cd6d24... # Add password reset link
You can amend the commit now, with
git commit --amend
Once you are satisfied with your changes, run
git rebase --continueThe rebase is paused inside that commit. Add the forgotten change and continue:
echo "<p>Check your email</p>" >> reset.html
git add reset.html
git commit --amend --no-edit
git rebase --continueThe old commit now includes the change. To split a commit instead, run git reset HEAD~1 when it stops. That undoes the commit and keeps its changes in the files, so they can be committed in pieces before git rebase --continue.
--fixup and --autosquash
A few commits into a feature, a bug turns up in an earlier one. Instead of moving lines by hand later, tell Git which commit the fix belongs to:
3bea7d9 Add dashboard charts
f934b59 Add dashboard API
ebcb9ba Add dashboard layout ← the bug is heregit add .
git commit --fixup ebcb9baThat makes a commit named fixup! Add dashboard layout. When it's time to clean up, --autosquash moves and marks it:
git rebase -i --autosquash HEAD~4pick ebcb9ba # Add dashboard layout
fixup 97afd8a # fixup! Add dashboard layout
pick f934b59 # Add dashboard API
pick 3bea7d9 # Add dashboard chartsSave and close. To make it the default: git config --global rebase.autoSquash true.
Rules and safety
| Situation | Do this |
|---|---|
| Changed your mind in the todo list | Delete every line and save |
| Something went wrong during the rebase | git rebase --abort |
| It finished and the result is wrong | git reset --hard ORIG_HEAD, or git reflog |
HEAD~N can't reach past the first commit. To edit the whole history, use git rebase -i --root.
Quick reference
| To… | Use |
|---|---|
| Edit the last N commits | git rebase -i HEAD~N |
| Edit everything after a commit | git rebase -i hash |
| Edit from the first commit | git rebase -i --root |
| Keep a commit | pick or p |
| Change a message | reword or r |
| Combine, with a new message | squash or s |
| Combine, dropping the message | fixup or f |
| Delete a commit | drop or d |
| Change or split a commit | edit or e |
| Reorder | Move the lines |
| Make a fix for an old commit | git commit --fixup hash |
| Arrange fixups automatically | git rebase -i --autosquash HEAD~N |
| Continue after an edit or conflict | git rebase --continue |
| Cancel | git rebase --abort |
| Undo after finishing | git reset --hard ORIG_HEAD |