In the previous post we checked out how the git rebase
works and how to use the --interactive
mode to keep your topic branch up-to-date with the target (e.g. main
). We also talked about how to use the exec
command into the git-rebase-todo
file to run custom commands for the rebased commits.
Now, let's check how to useexec
in a short way.
Exec, exec, exec
If you need to run the build, tests, or any format tool while rebasing because you must guarantee that every commit is valid, you can speed things up using the git rebase --exec
command.
git rebase main --exec "npm test" --exec "npm run build"
If the command fails 😩 you will need to fix your code and then resume the rebase with git rebase --continue
.
git add .
git commit --amend
git rebase --continue
Reschedule failed exec
If an exec
command exits with a failure, your rebase will return the shell control to you. The git rebase --continue
command will resume the execution but it won't run the failed exec
again. To avoid this you can use --reschedule-failed-exec
.
git rebase main --exec "npm test" --exec "npm run build" --reschedule-failed-exec
You can make this the default behavior by using the rebase.rescheduleFailedExec setting:
git config --global rebase.rescheduleFailedExec true
To turn this off when the rescheduleFailedExec
is globally true
you should use the --no-reschedule-failed-exe
flag:
git rebase main --exec "npm test" --no-reschedule-failed-exec
Or, you can unset this globally:
git rebase --global --unset rebase.rescheduleFailedExec
Conclusion
If your commits will live on your Git repository forever (e.g. inside the main
branch) without being squashed (combined into one) it's useful to guarantee the validity of each commit individually. Git provides this exec
mechanism to automate this process.
How often do you check the tests and build for every commit of your created pull request? 😉
In the next chapter we're going to see how to split the commits 🔪 using git rebase
too.
Top comments (0)