DEV Community

Cover image for Git rebase exec to validate every commit
Axel Navarro for Cloud(x);

Posted on • Edited on

Git rebase exec to validate every commit

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"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

You can make this the default behavior by using the rebase.rescheduleFailedExec setting:

git config --global rebase.rescheduleFailedExec true
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Or, you can unset this globally:

git rebase --global --unset rebase.rescheduleFailedExec
Enter fullscreen mode Exit fullscreen mode

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)