Linux and Git commands · Lesson 18 of 23

Rebase and interactive rebase

Move a branch onto a newer base to keep history in a straight line, clean up messy commits with interactive rebase, and decide when to rebase and when to merge.

6 min readFree

On this page
  1. Why this matters
  2. What rebase does
  3. Conflicts during a rebase
  4. Interactive rebase: rewriting your own commits
  5. Undoing a rebase
  6. The golden rule
  7. Try it

Merging is not the only way to combine work. Rebasing rewrites your branch so that it looks as if you had started it from the newest commit on main. Interactive rebase goes one step further and lets you tidy your commits before anyone else sees them. Both are powerful, and both have one rule you must remember.

Why this matters#

Most teams ask for clean pull requests: a few meaningful commits, no "wip" or "fix typo again" noise, and a history that reads like a story. Rebase is how you get there. It is also behind git pull --rebase, which you met in the last lesson. The same tool can cause real damage on shared branches, so you need to know where the line is.

All the output comes from small practice repositories created for this lesson.

What rebase does#

Suppose feature/search was created from main, and two commits were then added to main. The history has forked:

Text
* 4d83d15 Add search tests
* 4a1b522 Add search
| * 1c4441f Update README
| * b2ba707 Add health check
|/  
* 11899fb Initial API

A merge would join the two lines with a merge commit. A rebase does something different: it takes your commits (Add search and Add search tests), sets them aside, moves your branch to the tip of main, and then replays your commits on top, one at a time.

Shell
git switch feature/search
git rebase main
git log --oneline --graph --all
Text
Rebasing (1/2)Rebasing (2/2)Successfully rebased and updated refs/heads/feature/search.
* a720b22 Add search tests
* b051821 Add search
* 1c4441f Update README
* b2ba707 Add health check
* 11899fb Initial API

The history is now one straight line, with no merge commit. Compare the hashes: our two commits were 4a1b522 and 4d83d15 before, and are b051821 and a720b22 now. A rebase creates new commits. They have the same changes and messages, but different parents, so Git gives them new ids. The old ones are no longer on any branch.

Because feature/search is now directly ahead of main, merging it back is a fast-forward:

Shell
git switch main
git merge --ff-only feature/search
Text
Updating 1c4441f..a720b22
Fast-forward

The usual workflow is: rebase your feature branch onto the latest main while you work, so it stays current, then merge it with a fast-forward or let your hosting service do it.

Conflicts during a rebase#

Each replayed commit can conflict with what is on the new base, and then the rebase stops at that commit. The commands are similar to a merge, with three choices:

Text
Auto-merging config.yml
CONFLICT (content): Merge conflict in config.yml
error: could not apply cb56494... Port 8080
Shell
git status
Text
interactive rebase in progress; onto 2e7a0a4
You are currently rebasing branch 'feature/port' on '2e7a0a4'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

Edit the file to resolve it (lesson 16), then run git add on it and git rebase --continue. Use git rebase --skip to drop the commit that conflicts, or git rebase --abort to go back to where you started, with your original commits untouched.

Pay attention to the markers. Here HEAD is the branch you are rebasing onto, which means main's content, and the other side is your own commit:

Text
<<<<<<< HEAD
port: 5200
=======
port: 8080
>>>>>>> cb56494 (Port 8080)

That is the opposite of a merge, where HEAD is your own branch. The reason is that during a rebase Git is currently standing on the new base and replaying your commit onto it. If you resolve a conflict so your commit no longer changes anything, Git drops that now-empty commit. (We saw that happen when we chose the main value.) Because a rebase replays commits one at a time, you may have to resolve the same kind of conflict more than once.

Interactive rebase: rewriting your own commits#

git rebase -i opens a list of commits and lets you decide what to do with each before they are replayed. Here is a branch with five messy commits:

Shell
git log --oneline
Text
581058d debug print
89c7235 fix typo again
8bfb169 add password check
cbee8e0 fix typo
0525a26 wip login
146709c Initial API

Start an interactive rebase of the last five commits:

Shell
git rebase -i HEAD~5

Your editor opens with a todo list, oldest first, which is the reverse of git log:

Text
pick 0525a26 wip login
pick cbee8e0 fix typo
pick 8bfb169 add password check
pick 89c7235 fix typo again
pick 581058d debug print

# Rebase 146709c..581058d onto 146709c (5 commands)
#
# Commands:

Below that, Git lists the commands you can use. Change the word at the start of a line and save the file:

  • pick keeps the commit as it is.
  • reword keeps the changes and lets you edit the message.
  • squash merges the commit into the previous one and lets you combine the messages.
  • fixup is like squash but throws the message away.
  • drop (or deleting the line) removes the commit and its changes.
  • edit stops after the commit so you can change its content.

You can also reorder commits by moving lines. We scripted our edit so it could be repeated, but in your editor it is simply typing. The new list was:

Text
pick 0525a26 wip login
fixup cbee8e0 fix typo
reword 8bfb169 add password check
fixup 89c7235 fix typo again
drop 581058d debug print

After saving, Git replayed the list. The reword line then opened the editor for a new message, and we wrote "Add login with password check". The result:

Shell
git log --oneline
Text
a017f72 Add login with password check
e9a5071 wip login
146709c Initial API

Five commits became two, the stray debug print change is gone, and the file still has the lines from the fixed-up commits. In a real clean-up you would more likely squash everything into one commit named Add login. Choose what makes the history clearest.

Interactive rebase can also split a commit and edit an old one, but these four commands (reword, squash/fixup, drop and reorder) cover most of the work.

Autosquash saves effort for small corrections. If you commit with a message like fixup! Add login with password check, then git rebase -i --autosquash HEAD~2 moves it next to the right commit and marks it fixup for you. You can create such a commit with git commit --fixup <hash>.

Undoing a rebase#

A rebase can feel risky because it rewrites commits, but the old ones are still in Git's memory for a while. git reflog shows every place HEAD has been:

Shell
git reflog | head -4
Text
a017f72 HEAD@{0}: rebase (finish): returning to refs/heads/feature/log
a017f72 HEAD@{1}: rebase (fixup): Add login with password check
5cf1f1f HEAD@{2}: rebase (reword): Add login with password check
5a1b6b9 HEAD@{3}: rebase (reword): add password check

Right after a rebase, the special name ORIG_HEAD points to where your branch was before. We created a branch from it and found all six original commits there:

Shell
git branch backup-before ORIG_HEAD

You can do the same on purpose: make a quick backup branch before a risky rebase, with git branch backup. Lesson 19 covers the reflog in more detail.

The golden rule#

Never rebase commits that other people already have. Rebasing replaces commits with new ones. If a teammate has the old commits, their history and yours no longer match, and they will see duplicates and conflicts. After rebasing a branch that you already pushed, a normal git push is rejected, because the remote has the old commits. You would need git push --force-with-lease, which is acceptable for your own feature branch that nobody else uses, and a serious problem on a shared one like main.

A simple test: rebase freely what is only on your computer or only your own branch. Merge, do not rebase, anything others build on.

The trade-off: Your feature branch is behind main and has a few commits. How do you bring it up to date?

  • Merge main into the branch: Safe on shared branches and keeps the real history, including when the branch was merged. The cost is extra merge commits, and a history with many crossing lines that is harder to read and bisect.
  • Rebase the branch onto main: A straight, readable history with no merge commits, and a good place to tidy commits. The cost is rewritten commit ids. You must force-push a pushed branch, and rebasing anything shared breaks your teammates' copies.

Many teams use both: rebase private work before opening a pull request, and merge (or squash-merge) into main through the hosting service. Lesson 21 returns to this.

Try it#

  1. Create a repository, then a feature branch with two commits, then add two commits to main. View the graph with git log --oneline --graph --all. Rebase feature onto main and view the graph again. Compare the commit hashes.
  2. Merge feature into main with --ff-only. What does Git say?
  3. Make five small commits on a branch. Run git rebase -i HEAD~5, squash them into two commits and reword one message.
  4. Run git reflog and find the commit your branch pointed at before the rebase. Create a branch at that commit.
  5. Create a conflict on purpose between your branch and main, start a rebase and resolve it. Check which side HEAD is, then finish with git rebase --continue. Try the same again, ending with git rebase --abort.