Why Tidy Up
Real work produces messy history: "wip" commits, a typo fixed three commits later, a debugging file added and then removed. Before you share a branch, you can turn that into a few clear commits that each make sense on their own. Reviewers can follow them, and anyone undoing a change later can undo exactly one idea.
Interactive rebase, git rebase -i, lets you edit your unshared commits
before you share them: combine them, reword their messages, reorder them or
drop them.
The Todo List
Start it with the commit your work is based on, usually the branch you started from:
git rebase -i main
Git opens your configured editor with a todo list: one line per commit on
your branch that main does not have, oldest first. Illustrative start of
the list:
pick 3d7dd12 # Add tomato soup recipe
pick 9a1c0e4 # wip
pick 5b2f7aa # Add debug print
Older Git versions show the same lines without the #. Below the commits, a
block of # lines explains the commands; Git ignores those lines.
Change the word at the start of a line to choose what happens to that commit. Leave the ID alone: Git uses it, not the message, to find the commit.
| Command | What happens to the commit |
|---|---|
pick | Kept as it is |
reword | Kept; Git opens the editor so you can change its message |
squash | Combined into the commit above it; you edit the combined message |
fixup | Combined into the commit above it; its own message is thrown away |
drop | Removed, with its changes |
Lines run from top to bottom, so you can also reorder commits by moving lines.
Save and close the editor to start. Git replays the commits as you asked,
stopping to open the editor for each reword and squash.
Staying Safe
Interactive rebase rewrites commits, so every edited commit gets a new ID. Use it only on commits nobody else has yet, for the same reason as any rebase.
You can back out at every stage:
- While the todo list is open: delete every line and save. Git reports that there is nothing to do and changes nothing.
- While the rebase is stopped, for example on a conflict: run
git rebase --abort. - After it finishes:
git reset --hard ORIG_HEADreturns the branch to its tip from before the rebase, provided you have not run another command that changesORIG_HEAD, such as a reset.
Check the result before moving on: git log --oneline for the new commits,
and git diff ORIG_HEAD to confirm the final content is what you expected.
Exercise: Tidy a Messy Unpublished History
The soup branch has six commits for one recipe. You will turn them into two:
the recipe itself, and the change that lists it in the recipe book.
Starting state: Git configured as in the configuration lesson, including an editor you are comfortable using.
-
Run the setup:
mkdir -p ~/git-practice/interactive-rebase cd ~/git-practice/interactive-rebase git init -b main cat > recipes.md <<'EOF' # Recipe book EOF git add recipes.md git commit -m "Add recipe book" git switch -c soup cat > soup.md <<'EOF' # Tomato soup Ingredients: - 4 tomatos EOF git add soup.md git commit -m "Add tomato soup recipe" cat >> soup.md <<'EOF' - 1 onion Steps: 1. Chop everything and simmer for 20 minutes. EOF git commit -am "wip" cat > debug.txt <<'EOF' Checking the step numbers. EOF git add debug.txt git commit -m "Add debug print" cat > soup.md <<'EOF' # Tomato soup Ingredients: - 4 tomatoes - 1 onion Steps: 1. Chop everything and simmer for 20 minutes. EOF git commit -am "Fix typo" git rm debug.txt git commit -m "Remove debug print" cat >> recipes.md <<'EOF' - Tomato soup EOF git commit -am "Update index" -
Look at the messy history:
git log --oneline main..soupmain..soupmeans "commits onsoupthat are not onmain". Six commits are listed, newest first. -
Start the interactive rebase:
git rebase -i mainYour editor opens with six
picklines, oldest first. Change the first word of each line so the list reads like this, keeping your own IDs:pick 3d7dd12 # Add tomato soup recipe fixup 9a1c0e4 # wip drop 5b2f7aa # Add debug print fixup 0c41d2e # Fix typo drop 8e9f310 # Remove debug print reword 61ab7c5 # Update indexThe "wip" and typo commits fold into the recipe commit, the debug file's two commits disappear, and "Update index" gets a better message. Save and close.
Git then opens the editor again for the
reword, showing "Update index". Replace that line withList the soup in the recipe book, then save and close. -
Check the result:
git log --oneline main..soup git diff ORIG_HEADTwo commits remain: "Add tomato soup recipe" and "List the soup in the recipe book".
git diff ORIG_HEADprints nothing: the files are exactly what they were before tidying, because the debug file was added and removed, and every other change was kept. -
Practise undoing a finished tidy, then do it again:
git reset --hard ORIG_HEAD git log --oneline main..soup git rebase -i mainAfter the reset, all six commits are back. Edit the todo list and the reword message as in step 3 to tidy them again.
Success: git log --oneline main..soup shows the two intended commits with
their messages, and git diff ORIG_HEAD shows the content is unchanged.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
| An unfamiliar editor opened | Git's editor setting is not the one you expected | Check git config --global core.editor; the configuration lesson shows how to set it |
Nothing to do | The todo list was saved empty | Run git rebase -i main again |
A CONFLICT during the rebase | Reordered or dropped commits change the same lines | Resolve as in the previous lesson, or git rebase --abort |
cannot 'fixup' without a previous commit | fixup or squash is on the first line | git rebase --abort, then start again with pick on the first line |
debug.txt is still in the folder | Only one of its two commits was dropped | git reset --hard ORIG_HEAD and try again |
-
Clean up:
cd ~/git-practice ls rm -rf interactive-rebase ls
Independent task: before cleaning up, add a bread recipe in two commits:
cat > bread.md <<'EOF'
# Flatbread
Flour, water and salt.
EOF
git add bread.md
git commit -m "Add bread recipe"
cat >> recipes.md <<'EOF'
- Flatbread
EOF
git commit -am "Add bread to the recipe book"
Combine these two into one commit with the message "Add bread recipe and list
it", using squash so that you write the combined message yourself. Leave the
two soup commits as they are.
Check Your Understanding
- Why should you tidy commits before pushing a branch rather than after?
- What is the difference between
squashandfixup?
Answer and Reasoning
Independent task
Run git rebase -i main. The list now has four lines. Leave the two soup
commits and "Add bread recipe" as pick, and change the last line to
squash:
pick 3d7dd12 # Add tomato soup recipe
pick 61ab7c5 # List the soup in the recipe book
pick 4f0a8b2 # Add bread recipe
squash 7c2d915 # Add bread to the recipe book
Your IDs will differ. After you save, the editor opens with both messages under
# This is a combination of 2 commits. Replace them with the single line
Add bread recipe and list it, then save and close. git log --oneline main..soup
shows three commits, and the newest one changes both bread.md and
recipes.md.
Understanding check
- Tidying rewrites commits, giving them new IDs. Before you push, nobody else has the originals, so nothing breaks. After pushing, others may already have the old commits or have built on them; replacing them would make their history disagree with yours.
- Both combine a commit into the one above it.
squashopens the editor with both messages so you can write a combined one.fixupkeeps only the message of the commit above and discards its own, without asking.