简体   繁体   English

Git 预提交钩子不在 Windows 上运行

[英]Git pre-commit hook not running on windows

I'm just starting to look into Git hooks, but I can't seem to get them to run.我刚刚开始研究 Git 钩子,但我似乎无法让它们运行。

I set up a local repository, so there is now a '.git' directory in my project folder.我设置了一个本地存储库,因此我的项目文件夹中现在有一个“.git”目录。 I have added a '.cmd' file into the C:/path/to/my/project/.git/hooks directory named 'pre-commit.cmd'.我在名为“pre-commit.cmd”的 C:/path/to/my/project/.git/hooks 目录中添加了一个“.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.这应该回显文本“HOOK RUNNING”并在该目录中创建一个空文本文件。 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.但是,如果我通过我的 IDE (NetBeans) 提交更改或使用 Git Bash 提交,它们似乎都没有运行我的预提交钩子,因为没有创建文件。

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.注意:这是在 Windows 7 PC 上。

What about naming your hook pre-commit (without any extension) ?如何命名您的钩子pre-commit (没有任何扩展名)?

EDIT: and add #!/bin/sh on the first line or #!/bin/bash (suggested in comments)编辑:并在第一行添加#!/bin/sh#!/bin/bash (在评论中建议)

You probably don't have the permissions to run the pre-commit file您可能没有权限运行pre-commit文件

Run in your terminal:在终端中运行:

chmod +x .git/hooks/pre-commit

Thanks to @vaughan for giving the idea感谢@vaughan 提出这个想法

TL;DR TL; 博士

Git hooks work on Git for Windows by default assuming the git hook script is simple .默认情况下,假设 git hook脚本很简单, Git hooks 在 Git for Windows 上工作

Background of Git and Windows Git和Windows的背景

Please Note : Git was made for shell interpretation ;请注意Git是为 shell解释而制作的; 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.因此,在 Windows 命令提示符或 Windows 制造的 PowerShell 上使用 git hooks 将有其固有的缺陷,并且无法预期完全的互操作性

Using git hooks on Windows is possible, but has many drawbacks.在 Windows 上使用 git hooks 是可能的,但有很多缺点。

Git for Windows uses a shell emulator that makes bash and shell commands possible.适用于 Windows 的 Git使用 shell 模拟器,使 bash 和 shell 命令成为可能。 This means that when a git hook is activated by git, the windows version will run the command using the shell emulator.这意味着当一个 git hook 被 git 激活时,windows 版本将使用 shell 模拟器运行命令。 Which in turn, will convert those shell commands to commands that the Windows operating system can understand.反过来,将这些 shell 命令转换为 Windows 操作系统可以理解的命令。 Meaning, simple shell scripts will work right off the bat.意思是,简单的 shell 脚本可以立即运行。 For example, the git hook pre-commit that ships with an initialization of a git repository can be run with no modification.例如,随 git 存储库初始化一起提供的 git hook pre-commit无需修改即可运行。

Example Of Default Behavior默认行为示例

  1. Initialize a git repository with the command git init使用命令git init初始化 git 存储库
  2. Navigate to the git hooks directory with the command cd .git\\hooks使用命令cd .git\\hooks导航到 git hooks 目录
  3. This directory holds all the git hook scripts.该目录包含所有 git hook 脚本。 Create a file named pre-commit Note创建一个名为pre-commit Note 的文件

    The name of the file is important文件名很重要

  4. replace the contents with the following shell script用以下 shell 脚本替换内容

    #!/bin/sh echo "Hello, World!"
  5. Navigate back to your root directory of the project and create a file named test.txt using the command echo "something" > text.txt导航回项目的根目录并使用命令echo "something" > text.txt创建一个名为test.txt的文件
  6. Stage the file to commit using the command git add test.txt使用命令git add test.txt要提交的文件
  7. Commit the change and watch the pre-commit hook activate using the command git commit -m "test commit"使用命令git commit -m "test commit"提交更改并观察预提交挂钩激活
  8. Verify the output to look like the following验证输出如下所示
    git commit -m "test commit" Hello, World! [master f00ccea] test commit

Example of Bad Behavior不良行为示例

When using a very advanced shell script to do things in git hooks, Windows shell interpretation doesn't always stack up.当使用非常高级的 shell 脚本在 git hooks 中执行操作时,Windows shell 解释并不总是堆积如山。 For example, when using the Husky git hook plugin for NPM, along with the prettier formatter, the commands do not map 1-1.例如,当为 NPM 使用 Husky git hook 插件以及更漂亮的格式化程序时,命令不会映射 1-1。 Meaning that your pre-commit git hook will fail on Windows.这意味着您的预提交 git 钩子将在 Windows 上失败。

Answering user1578653 Question回答 user1578653 问题

A git hook is an executable script; git hook 是一个可执行脚本; however, you are using a command prompt script ( .cmd ) and not a shell script ( .sh ).但是,您使用的是命令提示符脚本 ( .cmd ) 而不是 shell 脚本 ( .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).如果您喜欢在 Windows 操作系统上描述的这种行为,则创建名为pre-commit的文件并将其放置在.git\\hooks目录中(相对于您正在处理的项目)。 Then place the following content in that file.然后将以下内容放入该文件中。

.git\\hooks\\pre-commit .git\\钩子\\预提交

#!/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 .您会注意到 git 钩子工作并将单词HOOK RUNNING输出到控制台,而thisCauseError将向stderr打印一条错误消息,该消息将打印到文件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这对我有用:D

Maybe it'll help someone - in my case, I had set core.hooksPath to wrong directory.也许它会帮助某人 - 就我而言,我已将core.hooksPath设置为错误的目录。 Reseting it with git config --global core.hooksPath '~/.githooks' solved the issue :)git config --global core.hooksPath '~/.githooks'重置它解决了这个问题:)

For me, I tried to run a .bat file.对我来说,我尝试运行一个.bat文件。

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我在 windows 上运行 gradle 项目,在 cmder shell 和 cmd 中运行 gradle 命令

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 .尝试了其他答案中建议的解决方案,但对解决此问题没有帮助: 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.对我.git/pre-commit.sample的解决方案是将文件.git/pre-commit.sample重命名为.git/pre-commit并插入脚本以更漂亮地格式化更改的文件。 The file with the name 'pre-commit' which I have created manually must have had some problems (encoding or end-line symbols remains unclear).我手动创建的名为“pre-commit”的文件一定有问题(编码或结束行符号仍不清楚)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM