Is there a way to force (atleast warn) git commits for a given branch to update only a part of the repo?
For instance, assume I am working on docs on a special update-docs
branch, and would like to make sure I don't commit some changes I made to another part of the code by mistake. I would like git to reject the commit or at least warn me that there's a change to a file outside the scope of the docs
folder...
Is there a way to achieve this?
CodePudding user response:
This would be possible using a pre-commit hook.
The
pre-commit
hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit, although you can bypass it withgit commit --no-verify
. You can do things like check for code style (runlint
or something equivalent), check for trailing whitespace (the default hook does exactly this), or check for appropriate documentation on new methods.
In your case, you can look at the list of files to see whether anything outside the "docs" tree has been added for commit.
If you are collaborating with others and want to make sure other people don't push non-docs commits to the update-docs
branch, then you can install a server side pre-receive
or update
hook to verify this.