简体   繁体   中英

Unable to symlink to the .git directory

This is probably something silly, but searching around I haven't been able to find the answer. I'm trying to setup pre-commit hooks for my git project in a way that the scripts can be versioned along with the source.

It seemed like symlinks are supported and the way to go for this. When I try to ln with anything in the .git directory I get the error "No such file or directory". Is there something weird about the way the .git directory is setup? I'm doing this through cygwin on Windows right now, in case that's pertinent.

For example, the script I'm using to setup the symlinks looks like:

#/bin/bash

HOOK_NAMES="pre-commit post-commit"
THIS_SCRIPT_DIR=$(dirname $0)
HOOK_DIR=$THIS_SCRIPT_DIR/../.git/hooks

for hook in $HOOK_NAMES; do
    ln -s -f $THIS_SCRIPT_DIR/git-hooks/$hook $HOOK_DIR/$hook
done

Thanks,

====

Followup: This does appear to be an issue with Cygwin, though I haven't been able to find documentation that explains it. This works fine on Linux and OS X. Cygwin is having issues creating symlinks in an implicitly hidden folder (. prefixed). If anyone knows why I'll happily tag that explanation as an answer.

I don't know why this doesn't work in Cygwin (seems like a bug to me) but in case anyone else has the same issue I was able to work around it by creating the symbolic link from inside the .git folder. So, for example, from the command line you would go to the .git/hooks folder:

> cd .git/hooks

And then create the link

> ln -s ../../git-hooks/pre-commit pre-commit

It seems that you are trying to symlink to your .git/hooks from the repository itself. I don't think this is supported by Git, which would explain the error you are encountering.

This answer suggests other approaches, most of them suggesting going to other way around (having .git/hooks being a symlink). You might find one that suits you best.

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.

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