If you’re a bit too pedantic about your git
history, like I am, you probably want to make sure that all the commits are passing whatever checks the project has set up before merging it into the main
branch.
It turns out there’s a way to automatically run a command for each commit when you’re doing a git-rebase
— you can pass the --exec
flag (or the shorthand -x
flag).
With this flag, git
will run the given command after applying each commit onto the new HEAD
. If the command returns a non-zero exit code, the rebase will stop on that commit and allow you to update the commit so that it passes.
For example, if you have a Ruby project (using RSpec) and want to check that all commits after the main
branch are passing the tests, you can run the following command.
$ git rebase -x “bundle exec rspec” main
Why bother?
There are a few good reasons, other than the warm fuzzy feeling it brings you inside, to make sure that the commits are valid.
My main reason for caring about this is that it helps you keep track that all commits are working. If you have commits that aren’t passing the tests or have incomplete/broken features, it will be more challenging to track down a separate issue using git-bisect
. Since you might land on a commit that is not in a working state, it is harder to tell if a commit is good or bad.
It also forces you to think about commits as an atomic piece of completed work, which makes it easier to write a great commit message which summarizes the unit of work.
Top comments (0)