I'm trying to create a precommit hook to prevent committing merge conflicts accidentally... basically cancelling the commit when it includes >>>>. I've found multiple viable-looking approaches, but I am wondering why the "official" approach is not working (trying to use https://stackoverflow.com/a/27150330/752916 which refers to https://github.com/git/git/blob/v2.26.0/templates/hooks--pre-commit.sample#L49). When I execute this command, I am presented with usage information (see below) instead of an error. I expected something like this leftover conflict marker because I have a test conflict in my codebase. I am not looking for alternatives, those are readily available on SO already. I just want to know why this command doesn't work.
$ git diff-index --check --cached $against -- usage: git diff-index [-m] [--cached] [<common-diff-options>] <tree-ish> [<path>...] common diff options: -z output diff-raw with lines terminated with NUL. -p output patch format. Debugging info:
$ git --version git version 2.25.0.windows.1 I tried this in Git Bash and Windows Powershell, same result in both.
$againstexpand to here?echo "Against is $against"in my githook outputsAgainst isgit diff-indexrequires a<tree-ish>argument, as indicated by the syntax. Which tree would you like to diff the index against?$againstis not available, then is a git hook with this command impossible on Windows despite it being the official example? Or does it just involve an extra step of setting $against to the right tree-ish? If so, what should the right tree-ish be?https://github.com/git/git/blob/v2.26.0/templates/hooks--pre-commit.sample?againstis set at line 12 or line 15, depending on other conditions. If you copied only line 47,against—which is a shell variable, not a Git one—is not going to be set.