Sub topic 13
Testing each commit with --exec and using --onto
This chapter assumes you can start an interactive rebase, read the todo list, and resolve conflicts. It covers two options that extend what a rebase can do. --exec checks every commit in a cleaned series. --onto moves a range of commits to a new base.
Why test every commit
After you reorder, squash, and split commits, each commit in the series should still build and pass tests. A reviewer may check out any single commit. git bisect also lands on individual commits. If an early commit depends on a change that you moved to a later commit, the branch tip can pass while the middle commits fail.
Running tests only at the tip does not catch this. You need to test each commit as it is applied.
Running a command after each commit with --exec
The --exec option adds an exec line after every pick in the todo list. Git runs the command in the working tree once that commit has been applied.
git rebase -i --exec "npm test" origin/mainThe todo list that opens looks like this:
pick 3f2a1b9 Add login form
exec npm test
pick 51ab9c0 Add validation
exec npm test
pick a6b2c33 Add login tests
exec npm testIf a command exits with a non-zero status, the rebase stops at that commit. You can then fix the problem and amend the commit.
git add src/login.js
git commit --amend --no-edit
git rebase --continueYou can also write exec lines by hand in a normal interactive rebase. Put one after a single commit when only that commit needs checking.
Moving commits with --onto
A plain rebase replays your commits onto the branch you name. The --onto option separates three things: the new base, the old base, and the branch to move. The form is:
git rebase --onto <newbase> <oldbase> <branch>Git takes the commits that are reachable from <branch> but not from <oldbase>. It replays them on top of <newbase>.
A common case is a branch that was started from another feature branch. Suppose topic was created from feature-a, but feature-a should not be a dependency. You want topic to sit directly on main.
git rebase --onto main feature-a topicOnly the commits of topic that are not in feature-a are replayed on main.
You can combine the options. This command moves the range and opens the todo list so you can also clean it up:
git rebase -i --onto main feature-a topicThe same idea removes commits from the middle of a branch. If you want to drop the commits between two points, rebase the later range onto the earlier one.
git rebase --onto HEAD~4 HEAD~2This replays the commits after HEAD~2 onto HEAD~4. The two commits between them are left out.
Common errors
| Problem | Cause | Fix |
|---|---|---|
| Rebase stops after a pick with `exec` failure | The command returned non-zero | Fix the code, amend, then `git rebase --continue` |
| Wrong commits replayed with `--onto` | The old base was not the point where the branch diverged | Run `git log --oneline --graph` first and pick the right old base |
| Tests pass only at the tip | Fixes landed in a later commit | Move the fix into the earlier commit and rerun with `--exec` |
Exercise
A branch topic was started from feature-a. You want its own commits to sit directly on main, and you want to confirm that every commit still passes npm test. Which command does this in one step?
Show answer
git rebase -i --onto main feature-a topic --exec "npm test". The --onto option moves only the commits that are not in feature-a onto main. The --exec option adds an exec npm test line after each pick, so the rebase stops at the first commit that fails.