Sub topic 3
Starting git rebase -i and picking the base
This chapter covers how to launch an interactive rebase and how to choose the base commit, which decides which commits you can edit. It assumes you know add, commit, branch, log and status, and that you are comfortable in a terminal and a text editor. It also assumes you know what a remote branch is and how git push works.
What the base means
git rebase -i <base> lets you edit every commit after <base>. The base itself is not touched. Git collects the commits reachable from your current branch tip that are not reachable from the base, and lists them in a todo file. It then replays them one by one onto the base. Replaying creates new commits, which is why the base must be chosen with care.
Choosing a base that is too old gives you extra commits to read through. Choosing one that is too recent leaves out commits you meant to change.
Look before you rebase
Start by listing recent commits. Then open the todo list for the last four.
git log --oneline -n 5
git rebase -i HEAD~4HEAD~4 means "four commits before HEAD", so the four newest commits are editable. The fifth commit in the log is the base. Count carefully: with -n 5 you see the four commits you will edit plus the base.
Three ways to pick the base
| Base | Command | Use it when |
|---|---|---|
| Relative to HEAD | `git rebase -i HEAD~4` | You know how many recent commits to edit. |
| Upstream branch | `git rebase -i origin/main` | You want to clean up everything your branch adds on top of main. |
| A specific commit | `git rebase -i 3f2a1b9` | You know the hash of the last commit to leave alone. |
For a feature branch, origin/main is usually the best choice. It selects exactly the commits that a reviewer will see in a pull request. Run git fetch first so that origin/main is current.
To check what a base will select before you start, list the same range:
git fetch
git log --oneline origin/main..HEADThe output is the list of commits that will appear in the todo file, newest first.
The todo list
Git opens your editor with one line per commit. The order is reversed from git log: the oldest commit is at the top, and the list is applied from top to bottom.
pick 3f2a1b9 Add login form
pick 8c4d7e2 Fix typo in login form
pick 51ab9c0 Add validation
pick 7d90e1f Debug logging
pick a6b2c33 Add login testsEach line has a command, a short hash and the commit subject. Every line starts as pick, which keeps the commit as it is. The following chapters cover the other commands. When you save and close the editor, Git starts replaying. If you delete all the lines, or leave the file empty, the rebase is cancelled.
Common mistakes when starting
- Rebasing with a dirty working tree. Git refuses to start if you have uncommitted changes to tracked files. Commit them or run
git stashfirst. - Counting wrongly with HEAD~N. If the todo list shows commits you did not write, N is too large. Abort by emptying the file, then try again with a smaller number.
- Using a stale base. If you skip
git fetch,origin/mainmay be older than the real remote branch. You will then edit commits that are already on main. - Rebasing shared commits. If a commit has been pushed and others built on it, rewriting it causes trouble for them. The publishing chapter covers this.
Exercise
Your feature branch has six commits on top of origin/main. You run git rebase -i HEAD~3. Which commits can you edit, and which command would select all six?
Show answer
You can edit only the three newest commits, because HEAD~3 is the base and stays untouched. To select all six, use git rebase -i origin/main (after git fetch) or git rebase -i HEAD~6.