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:

Before
6760c2c debugging
15baf33 Add password reset link
ec2e7d8 fix typo
8b27d6e more stuff
dea73f7 WIP
190b71b add login form
After
066ae40 Add login form
4466ab4 Add password reset link

Interactive rebase does that. It lists recent commits, and an instruction next to each one decides what happens to it:

InstructionWhat it does
pickKeep the commit as it is
rewordChange its message
squashCombine it into the commit above, and write a new message
fixupCombine it into the commit above, and drop its message
dropDelete it
editStop at it, to change its contents or split it
Moving linesChanges the order of commits

A messy history to clean up

Terminal
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

Terminal
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:

Todo 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

Terminal
git rebase -i HEAD~1
Todo list
drop 6760c2c # debugging

Save 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

Terminal
git rebase -i HEAD~5
Todo list
reword 190b71b # add login form
pick dea73f7 # WIP
pick 8b27d6e # more stuff
pick ec2e7d8 # fix typo
pick 15baf33 # Add password reset link

Save and close. A second editor opens with the old message; change it to Add login form, then save and close.

git log --oneline
96f16ae Add password reset link
6bfbd68 fix typo
26ae188 more stuff
ce84238 WIP
b99e10f Add login form
beb1792 Initial commit

Every 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:

Terminal
git rebase -i HEAD~5
Todo list
pick b99e10f # Add login form
fixup ce84238 # WIP
fixup 26ae188 # more stuff
fixup 6bfbd68 # fix typo
pick 96f16ae # Add password reset link
git log --oneline
007eadd Add password reset link
eccb460 Add login form
beb1792 Initial commit

Four commits became one, with all their changes. git show HEAD~1 shows them together.

The combined commit's message
fixupKeeps the message above, and drops this one. For junk like “WIP”.
squashOpens the editor with every message, to write a new one. For messages worth keeping.

For example, squashing three commits:

Todo list
pick 1a2b3c4 # Add search input
squash 5d6e7f8 # Add search button
squash 9a8b7c6 # Connect search to API

The editor shows all three messages, and they can become one:

Commit message
Add search feature

- Search input with debounce
- Search button
- Connected to /api/search

4.Reorder: move the lines

Terminal
git rebase -i HEAD~2
Todo list, lines swapped
pick 007eadd # Add password reset link
pick eccb460 # Add login form
git log --oneline
51d0b86 Add login form
6cd6d24 Add password reset link
beb1792 Initial commit

If 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

Terminal
git rebase -i HEAD~2
Todo list
edit 6cd6d24 # Add password reset link
pick 51d0b86 # Add login form
Output
Stopped 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 --continue

The rebase is paused inside that commit. Add the forgotten change and continue:

Terminal
echo "<p>Check your email</p>" >> reset.html
git add reset.html
git commit --amend --no-edit
git rebase --continue

The 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:

git log --oneline
3bea7d9 Add dashboard charts
f934b59 Add dashboard API
ebcb9ba Add dashboard layout     ← the bug is here
Terminal
git add .
git commit --fixup ebcb9ba

That makes a commit named fixup! Add dashboard layout. When it's time to clean up, --autosquash moves and marks it:

Terminal
git rebase -i --autosquash HEAD~4
Todo list, already arranged
pick ebcb9ba # Add dashboard layout
fixup 97afd8a # fixup! Add dashboard layout
pick f934b59 # Add dashboard API
pick 3bea7d9 # Add dashboard charts

Save and close. To make it the default: git config --global rebase.autoSquash true.

Rules and safety

SituationDo this
Changed your mind in the todo listDelete every line and save
Something went wrong during the rebasegit rebase --abort
It finished and the result is wronggit 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 commitsgit rebase -i HEAD~N
Edit everything after a commitgit rebase -i hash
Edit from the first commitgit rebase -i --root
Keep a commitpick or p
Change a messagereword or r
Combine, with a new messagesquash or s
Combine, dropping the messagefixup or f
Delete a commitdrop or d
Change or split a commitedit or e
ReorderMove the lines
Make a fix for an old commitgit commit --fixup hash
Arrange fixups automaticallygit rebase -i --autosquash HEAD~N
Continue after an edit or conflictgit rebase --continue
Cancelgit rebase --abort
Undo after finishinggit reset --hard ORIG_HEAD