DEV Community

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

Posted on • Edited on

5

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.

Please leave your appreciation by commenting on this post!

It takes just one minute and is worth it for your career.

Get started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay