I'm just starting to look into Git hooks, but I can't seem to get them to run.
I set up a local repository, so there is now a '.git' directory in my project folder. I have added a '.cmd' file into the C:/path/to/my/project/.git/hooks directory named 'pre-commit.cmd'. Here is the contents of this file:
echo "HOOK RUNNING"
echo. 2>C:/path/to/my/project/.git/hooks/EmptyFile.txt
This should echo the text "HOOK RUNNING" and create an empty text file in that directory. However, if I commit changes through my IDE (NetBeans) or use Git Bash to commit, neither of them seem to run my pre-commit hook, as no file is created.
My understanding is that all you have to do to get a hook to run is add an executable with the name of the hook (as I have done). Am I doing something wrong?
Note: This is on a Windows 7 PC.
What about naming your hook pre-commit
(without any extension) ?
EDIT: and add #!/bin/sh
on the first line or #!/bin/bash
(suggested in comments)
You probably don't have the permissions to run the pre-commit
file
Run in your terminal:
chmod +x .git/hooks/pre-commit
Thanks to @vaughan for giving the idea
Git hooks work on Git for Windows by default assuming the git hook script is simple .
Please Note : Git was made for shell interpretation ; thus, using git hooks on a Windows command prompt or Windows made PowerShell will inherently have its flaws, and complete interoperability is not to be expected.
Using git hooks on Windows is possible, but has many drawbacks.
Git for Windows uses a shell emulator that makes bash and shell commands possible. This means that when a git hook is activated by git, the windows version will run the command using the shell emulator. Which in turn, will convert those shell commands to commands that the Windows operating system can understand. Meaning, simple shell scripts will work right off the bat. For example, the git hook pre-commit
that ships with an initialization of a git repository can be run with no modification.
git init
cd .git\\hooks
This directory holds all the git hook scripts. Create a file named pre-commit
Note
The name of the file is important
replace the contents with the following shell script
#!/bin/sh echo "Hello, World!"
test.txt
using the command echo "something" > text.txt
git add test.txt
git commit -m "test commit"
git commit -m "test commit" Hello, World! [master f00ccea] test commit
When using a very advanced shell script to do things in git hooks, Windows shell interpretation doesn't always stack up. For example, when using the Husky git hook plugin for NPM, along with the prettier formatter, the commands do not map 1-1. Meaning that your pre-commit git hook will fail on Windows.
A git hook is an executable script; however, you are using a command prompt script ( .cmd
) and not a shell script ( .sh
). If you would like this behavior you described on a Windows operating system then create the file named pre-commit
and place it in the .git\\hooks
directory (relative to the project you are working on). Then place the following content in that file.
.git\\hooks\\pre-commit
#!/bin/sh
echo "HOOK RUNNING"
thisCausesError 2> .git/hooks/EmptyFile.txt
You will notice that the git hook works and outputs the words HOOK RUNNING
to the console, and the thisCauseError
will print an error message to stderr
that will be printed to the file EmptyFile.txt
.
在我执行npm install
& 不小心删除了.git文件夹的情况下, npm install pre-commit --save
工作
For me i none of the above solution worked. I moved the pre-commit file from hooks to some other location, effectively deleting the file from hooks.
That Worked for me :D
Maybe it'll help someone - in my case, I had set core.hooksPath
to wrong directory. Reseting it with git config --global core.hooksPath '~/.githooks'
solved the issue :)
For me, I tried to run a .bat
file.
I discovered that backslashes need to be escaped :
For example:
#!/bin/bash
C:\\somefolder\\somefile.bat
If it helps anyone: I was getting following error:
error: cannot spawn .git/hooks/pre-commit: No error
Turned out that in my pre-commit file I did not have 'newline' character after last exit command:
#!/bin/sh
# From gist at https://gist.github.com/chadmaughan/5889802
# stash any unstaged changes
git stash -q --keep-index
# run the tests with the gradle wrapper
./gradlew test --daemon
# store the last exit code in a variable
RESULT=$?
# unstash the unstashed changes
git stash pop -q
# return the './gradlew test' exit code
exit $RESULT
# << must have a newline after above command >>
I was running gradle project on windows and gradle commands in cmder shell and cmd
Tried solutions suggested in other answers and it didn't help to fix this problem: cannot spawn .git/hooks/pre-commit: No such file or directory
.
The solution which worked for me was to rename the file .git/pre-commit.sample
to .git/pre-commit
and insert the script for formatting changed files with prettier. The file with the name 'pre-commit' which I have created manually must have had some problems (encoding or end-line symbols remains unclear).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.