Is there any way of preventing files with merge conflict from getting committed in git? No one is going to commit files with conflict intentionally. But is there a way to prevent files from getting committed in git?
Does git have any settings or configurable values anywhere, where it can prevent files by looking for <<<<<<<
, =======
or >>>>>>>
symbols?
I added a unit test to go through all files in the solution directory for the conflict marker string
SolutionValidationScripts defined separately below:
You can use a
pre-commit
hook, but be aware that agit commit --no-verify
would effectively ignore that.I generally put a
pre-receive
hook in order to control in a (more) central point what is being pushed.But a
pre-commmit
allows for a more timely detection (earlier in the development cycle).Here is another example (in addition of jthill's comment), in perl.
It uses
git diff-index -p -M --cached HEAD
, that isgit diff-index
instead ofgit diff
.I have left a few other controls done by this hook, just to showcase the kind of checks you can do in such a script.
A straightforward approach using a pre-commit hook, adapted from here but improved to be a bit more careful and thorough:
Don't forget to make the hook executable (
chmod u+x pre-commit
)!I've since put this on github: https://github.com/patrickvacek/git-reject-binaries-and-large-files/blob/master/pre-commit
VonC's answer already explains the different kinds of hooks where you might want to check for merge commits.
If you just want a simple solution to avoid committing conflicts, that is already included with git in the default pre-commit hook sample. Just enable the hook by renaming
.git/hooks/pre-commit.sample
to.git/hooks/pre-commit
. If you then try to commit a conflict:Note:
The script uses
git diff --check
internally, which also checks for various white space problems - so you might get whitespace errors as well. You can also rungit diff --check
before committing to find problems. See the manpage ofgit diff
for details and config options.This is valid for git V2.0; no idea when it was introduced.