Git pre-commit hook not running on windows

前端 未结 9 862
盖世英雄少女心
盖世英雄少女心 2021-01-31 08:15

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.

相关标签:
9条回答
  • 2021-01-31 08:49

    TL;DR

    Git hooks work on Git for Windows by default assuming the git hook script is simple.

    Background of Git and Windows

    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.

    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-commit Note

      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.txt using 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
      

    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.

    Answering user1578653 Question

    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.

    0 讨论(0)
  • 2021-01-31 08:53

    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)

    0 讨论(0)
  • 2021-01-31 08:59

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

    0 讨论(0)
提交回复
热议问题