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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20609816/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 11:11:39  来源:igfitidea点击:

Git pre-commit hook not running on windows

windowsgitversion-controlgithooks

提问by user1578653

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. 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:

我设置了一个本地存储库,因此我的项目文件夹中现在有一个“.git”目录。我在名为“pre-commit.cmd”的 C:/path/to/my/project/.git/hooks 目录中添加了一个“.cmd”文件。这是这个文件的内容:

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.

这应该回显文本“HOOK RUNNING”并在该目录中创建一个空文本文件。但是,如果我通过我的 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 上。

回答by Asenar

What about naming your hook pre-commit(without any extension) ?

命名你的钩子pre-commit(没有任何扩展名)怎么样?

EDIT:and add #!/bin/shon the first line or #!/bin/bash(suggested in comments)

编辑:#!/bin/sh在第一行添加或 #!/bin/bash(在评论中建议)

回答by edi9999

You probably don't have the permissions to run the pre-commitfile

您可能没有运行该pre-commit文件的权限

Run in your terminal:

在终端中运行:

chmod +x .git/hooks/pre-commit

Thanks to @vaughan for giving the idea

感谢@vaughan 提出这个想法

回答by Dalton

TL;DR

TL; 博士

Git hooks workon 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: Gitwas made for shell interpretation; thus, using git hooks on a Windows command prompt or Windows made PowerShell will inherently have its flaws, and complete interoperabilityis not to be expected.

请注意Git是为 shell解释而制作的;因此,在 Windows 命令提示符或 Windows 制造的 PowerShell 上使用 git hooks 将有其固有的缺陷,并且无法预期完全的互操作性

Using git hooks on Windows is possible, but has many drawbacks.

在 Windows 上使用 git hooks 是可能的,但有很多缺点。

Git for Windowsuses 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-committhat ships with an initialization of a git repository can be run with no modification.

Windows 版 Git使用一个 shell 模拟器,使 bash 和 shell 命令成为可能。这意味着当一个 git hook 被 git 激活时,windows 版本将使用 shell 模拟器运行命令。反过来,将这些 shell 命令转换为 Windows 操作系统可以理解的命令。意思是,简单的 shell 脚本可以立即运行。例如,pre-commit随 git 存储库初始化一起提供的 git hook无需修改即可运行。

Example Of Default Behavior

默认行为示例

  1. Initialize a git repository with the command git init
  2. Navigate to the git hooks directory with the command cd .git\hooks
  3. This directory holds all the git hook scripts. Create a file named pre-commitNote

    The name of the file is important

  4. replace the contents with the following shell script

    #!/bin/sh
    
    echo "Hello, World!"
    
    
  5. Navigate back to your root directory of the project and create a file named test.txtusing the command echo "something" > text.txt
  6. Stage the file to commit using the command git add test.txt
  7. Commit the change and watch the pre-commit hook activate using the command 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
    
  1. 使用命令初始化 git 存储库 git init
  2. 使用命令导航到 git hooks 目录 cd .git\hooks
  3. 该目录包含所有 git hook 脚本。创建一个名为pre-commitNote的文件

    文件名很重要

  4. 用以下 shell 脚本替换内容

    #!/bin/sh
    
    echo "Hello, World!"
    
    
  5. 导航回项目的根目录并创建一个test.txt使用命令命名的文件echo "something" > text.txt
  6. 使用命令暂存要提交的文件 git add test.txt
  7. 提交更改并使用命令观察预提交挂钩激活 git commit -m "test commit"
  8. 验证输出如下所示
    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. 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.

当使用非常高级的 shell 脚本在 git hooks 中执行操作时,Windows shell 解释并不总是堆积如山。例如,当为 NPM 使用 Husky git hook 插件以及更漂亮的格式化程序时,命令不会映射 1-1。这意味着您的预提交 git 钩子将在 Windows 上失败。

Answering user1578653 Question

回答 user1578653 问题

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-commitand place it in the .git\hooksdirectory (relative to the project you are working on). Then place the following content in that file.

git hook 是一个可执行脚本;但是,您使用的是命令提示符脚本 ( .cmd) 而不是 shell 脚本 ( .sh)。如果您想要在 Windows 操作系统上描述的这种行为,则创建命名的文件pre-commit并将其放置在.git\hooks目录中(相对于您正在处理的项目)。然后将以下内容放入该文件中。

.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 RUNNINGto the console, and the thisCauseErrorwill print an error message to stderrthat will be printed to the file EmptyFile.txt.

您会注意到 git 钩子工作并将单词输出HOOK RUNNING到控制台,并且thisCauseError将打印一条错误消息stderr并将其打印到文件中EmptyFile.txt

回答by Nilotpal

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

回答by mtraton

Maybe it'll help someone - in my case, I had set core.hooksPathto wrong directory. Reseting it with git config --global core.hooksPath '~/.githooks'solved the issue :)

也许它会帮助某人 - 就我而言,我设置core.hooksPath了错误的目录。重置它git config --global core.hooksPath '~/.githooks'解决了问题:)

回答by Theo

in my case where i did npm install& accidentally deleted .gitfolder, npm install pre-commit --saveworked

在我的情况下,我不npm install小心删除了.git文件夹,npm install pre-commit --save工作

回答by Yogesh

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 命令