日本語

Running Tests on Each Commit with git rebase –exec

​​Git​command line,shell script,Testing

Running git rebase -x "test command" main executes that command each time a commit from your branch is applied. When a test fails, the rebase stops at the commit that broke it, so you can see right away which commit caused the failure, fix it, and then continue. This is useful when you want to confirm before opening a pull request that tests pass not only at the tip of the branch but also at the commits in between.

Note

The environment was Ubuntu 24.04 with Git 2.43.0, and /bin/sh was dash. I set the commit author to an example value (Neko <neko@example.com>) and fixed the commit dates with environment variables. Where I show the contents of the todo list, I set the environment variable GIT_SEQUENCE_EDITOR to a command that prints the given content instead of opening an editor. The rebase progress lines (such as Rebasing (1/8)) are overwritten on the same line in a terminal, so I left them out of the output shown here. I did not test Windows or macOS, Git 2.44 or later, branches that contain merge commits (--rebase-merges), or combining this with --autosquash.

Example branch and test script

For the example, I use a repository with small arithmetic functions such as addition and subtraction in lib/, and their tests in tests/. I created a feature branch from main, added four commits, and then one more commit was added to main in the meantime.

console
$ git log --oneline --graph --all
* 0f6c442 LICENSEを追加
| * c0fcfee 割り算を追加
| * a12ad7d READMEを追加
| * 1261a67 掛け算を追加
| * aa0c3f7 引き算を追加
|/  
* 22ca1c1 足し算とテストを追加

The tests are run by test.sh. It loads the functions in lib/, runs the tests in tests/ in order, and exits with status 1 if any result is wrong.

sh
fail=0
check() {
  if [ "$2" = "$3" ]; then echo "ok  $1"; else echo "NG  $1(期待 $3、結果 $2)"; fail=1; fi
}
for f in lib/*.sh; do . "./$f"; done
for t in tests/*.sh; do . "./$t"; done
exit $fail

The mul function in lib/mul.sh, added in the multiplication commit, has a typo: it uses + where it should use *. So when the tests run at the tip of feature, the multiplication test fails.

sh
mul() { echo $(($1 + $2)); }
console
$ sh test.sh; echo "終了コード $?"
ok  add 2 3
ok  div 8 2
NG  mul 2 4(期待 8、結果 6)
ok  sub 5 3
終了コード 1

Running the tests at the tip of the branch only tells you about the current state. It does not tell you which commit introduced the breakage, or whether the commits after it pass. So I use git rebase to apply the commits one at a time and run the tests after each one.

Running tests on each commit with git rebase -x

Pass the command you want to run to -x (the long form is --exec). Writing the branch you want to rebase onto (here main) at the end is the same as a normal git rebase main.

console
$ git rebase -x "sh test.sh" main
Executing: sh test.sh
ok  add 2 3
ok  sub 5 3

Executing: sh test.sh
ok  add 2 3
NG  mul 2 4(期待 8、結果 6)
ok  sub 5 3
warning: execution failed: sh test.sh
You can fix the problem, and then run

  git rebase --continue

The Executing: sh test.sh lines mark where the tests ran after a commit was applied. The first run came after the subtraction commit was applied, and all tests passed. The second run came after the multiplication commit was applied. The multiplication test failed, the output showed warning: execution failed, and the rebase stopped. The exit status of git rebase at this point was 1. The remaining commits, the README commit and the division commit, had not been applied yet.

The official git rebase reference explains that --exec adds an exec <cmd> line after each line in the todo list that creates a commit (such as pick), and that the rebase is interrupted (stops) if the command fails. This works through the mechanism of interactive rebase (git rebase -i), but you can use it without -i.

If you run it with -i, you can check the added exec lines in the todo list. The todo list passed to the editor was as follows, with exec sh test.sh after every pick line. (I omitted the # comment lines.)

text
pick aa0c3f7 引き算を追加
exec sh test.sh
pick 1261a67 掛け算を追加
exec sh test.sh
pick a12ad7d READMEを追加
exec sh test.sh
pick c0fcfee 割り算を追加
exec sh test.sh
A diagram showing the pick and exec lines of the todo list from top to bottom. The exec after the multiplication commit fails the tests and stops the rebase, and the lines below it have not run yet. The commit messages in the diagram are in Japanese.
Each pick is followed by an exec, and the rebase stops at the exec whose tests fail

Fixing the failing commit where the rebase stopped

Running git status at the stopped point shows that a rebase is in progress, and that the last commands run were the pick for the multiplication commit and the exec after it.

console
$ git status
interactive rebase in progress; onto 0f6c442
Last commands done (4 commands done):
   pick 1261a67 掛け算を追加
   exec sh test.sh
  (see more in file .git/rebase-merge/done)
Next commands to do (4 remaining commands):
   pick a12ad7d READMEを追加
   exec sh test.sh
  (use "git rebase --edit-todo" to view and edit)
You are currently editing a commit while rebasing branch 'feature' on '0f6c442'.
  (use "git commit --amend" to amend the current commit)
  (use "git rebase --continue" once you are satisfied with your changes)

nothing to commit, working tree clean

$ git log --oneline -3
80e88e7 掛け算を追加
1605169 引き算を追加
0f6c442 LICENSEを追加

At this point HEAD is the multiplication commit, the one where the test failed. If you fix the file here and run git commit --amend, you can correct that commit itself. I changed + to *, committed again without changing the message (--no-edit), and then continued with git rebase --continue.

console
$ git diff
diff --git a/lib/mul.sh b/lib/mul.sh
index ce73246..429ef50 100644
--- a/lib/mul.sh
+++ b/lib/mul.sh
@@ -1 +1 @@
-mul() { echo $(($1 + $2)); }
+mul() { echo $(($1 * $2)); }

$ git commit -q -a --amend --no-edit

$ git rebase --continue
Executing: sh test.sh
ok  add 2 3
ok  mul 2 4
ok  sub 5 3

Executing: sh test.sh
ok  add 2 3
ok  div 8 2
ok  mul 2 4
ok  sub 5 3

Successfully rebased and updated refs/heads/feature.

The remaining two commits were tested too, both passed, and the rebase finished. The exit status of git rebase was 0.

However, the first Executing: in this output came after the README commit was applied. The exec line that failed was already treated as done, so the tests were not run again on the fixed multiplication commit. This time, the multiplication test passed in the next commit's run, but this output alone does not show that the fixed commit passes by itself.

Re-running tests on the fixed commit before continuing

To have a failed exec run again when you continue, add --reschedule-failed-exec. I returned to the original state and ran the rebase again with this option.

console
$ git rebase -x "sh test.sh" --reschedule-failed-exec main
Executing: sh test.sh
ok  add 2 3
ok  sub 5 3

Executing: sh test.sh
ok  add 2 3
NG  mul 2 4(期待 8、結果 6)
ok  sub 5 3
warning: execution failed: sh test.sh
You can fix the problem, and then run

  git rebase --continue


hint: Could not execute the todo command
hint: 
hint:     exec sh test.sh
hint: 
hint: It has been rescheduled; To edit the command before continuing, please
hint: edit the todo list first:
hint: 
hint:     git rebase --edit-todo
hint:     git rebase --continue

The stopping point is the same, but the output says It has been rescheduled, and the failed exec was put back into the todo list. In git status, the first of the next commands to run is exec sh test.sh.

console
$ git status | head -8
interactive rebase in progress; onto 0f6c442
Last commands done (4 commands done):
   pick 1261a67 掛け算を追加
   exec sh test.sh
  (see more in file .git/rebase-merge/done)
Next commands to do (5 remaining commands):
   exec sh test.sh
   pick a12ad7d READMEを追加

As before, I fixed lib/mul.sh, ran git commit -q -a --amend --no-edit, and then ran git rebase --continue. This time the tests ran three times.

console
$ git rebase --continue
Executing: sh test.sh
ok  add 2 3
ok  mul 2 4
ok  sub 5 3

Executing: sh test.sh
ok  add 2 3
ok  mul 2 4
ok  sub 5 3

Executing: sh test.sh
ok  add 2 3
ok  div 8 2
ok  mul 2 4
ok  sub 5 3

Successfully rebased and updated refs/heads/feature.

The first run was on the fixed multiplication commit. The tests then ran again after the README commit and the division commit, and the rebase finished only after the tests passed on all four commits. The progress count also grew from the 8 steps without the option to 9 steps.

According to the official description of --reschedule-failed-exec, you get the same behavior without the option if the configuration rebase.rescheduleFailedExec is set to true.

Re-running tests without rewriting commits

When the branch already sits on top of the latest commit of main, running git rebase -x "sh test.sh" main again does not rewrite any commits. It only runs the tests. The HEAD hash was the same before and after.

console
$ git rev-parse --short HEAD
0471a2c

$ git rebase -x "sh test.sh" main
(途中は省略。4つのコミットそれぞれでテストが実行され、すべて ok)
Successfully rebased and updated refs/heads/feature.

$ git rev-parse --short HEAD
0471a2c

As the --force-rebase description in the official reference says, the normal behavior is to fast-forward over commits that do not change, instead of recreating them. So if the base has not changed, you can use rebase only to run tests on each commit.

If you want to narrow down which commits get checked, specify a commit instead of a branch name as the base. With git rebase -x "sh test.sh" HEAD~2, the tests ran only on the two newest commits (the README commit and the division commit).

Running other commands after each commit

You can pass -x more than once. With two -x options, each pick was followed by two exec lines, in the order given.

console
$ git rebase -i -x "sh test.sh" -x "git log -1 --format=%s" main
text
pick bc8151d 引き算を追加
exec sh test.sh
exec git log -1 --format=%s
pick b0c35cc 掛け算を追加
exec sh test.sh
exec git log -1 --format=%s
pick c9502cf READMEを追加
exec sh test.sh
exec git log -1 --format=%s
pick 0471a2c 割り算を追加
exec sh test.sh
exec git log -1 --format=%s

When it ran, the second exec showed the subject of each commit after the test results. Here is the output for the first commit.

console
Executing: sh test.sh
ok  add 2 3
ok  sub 5 3

Executing: git log -1 --format=%s
引き算を追加

The official reference also describes writing several commands in one -x, such as "cmd1 && cmd2". It says that exec commands run in a shell, from the root directory of the working tree.

​Posted by 86