我最好是用一个shell脚本替换符号链接的副本,还是有另一种方式告诉Git遵循符号链接?

PS:我知道这不是很安全,但我只是想在一些特定的情况下这样做。


当前回答

这是一个预提交钩子,它用这些符号链接的内容替换索引中的符号链接blobs。

把它放在.git/hooks/pre-commit中,并使其可执行:

#!/bin/sh
# (replace "find ." with "find ./<path>" below, to work with only specific paths)

# (these lines are really all one line, on multiple lines for clarity)
# ...find symlinks which do not dereference to directories...
find . -type l -exec test '!' -d {} ';' -print -exec sh -c \
# ...remove the symlink blob, and add the content diff, to the index/cache
    'git rm --cached "$1"; diff -au /dev/null "$1" | git apply --cached -p1 -' \
# ...and call out to "sh".
    "process_links_to_nondir" {} ';'

# the end

笔记

我们尽可能多地使用POSIX兼容功能;然而,diff -a不符合POSIX,可能还有其他原因。

这段代码中可能有一些错误/错误,即使它经过了一些测试。

其他回答

我所做的是将符号链接中的文件添加到Git中(我没有使用符号链接,但是):

sudo mount --bind SOURCEDIRECTORY TARGETDIRECTORY

在Git-managed目录下执行此命令。必须在将SOURCEDIRECTORY挂载到TARGETDIRECTORY之前创建TARGETDIRECTORY。

它在Linux上工作得很好,但在OS X上就不行了!这个技巧也帮助了我的Subversion。我用它来包括来自Dropbox帐户的文件,网站设计师在那里做他/她的事情。

如果你想让这个绑定永久存在,在/etc/fstab中添加以下行:

/sourcedir /targetdir none bind

为什么不反过来创建符号链接呢?这意味着不是从Git存储库链接到应用程序目录,而是以相反的方式链接。

例如,假设我正在设置一个安装在~/application中的应用程序,它需要一个配置文件config.conf:

我将config.conf添加到Git存储库中,例如~/repos/application/config.conf。 然后我通过运行ln -s ~/repos/application/config.conf从~/application创建一个符号链接。

这种方法可能并不总是有效,但到目前为止对我来说效果很好。

在macOS上(我有Mojave/ 10.14, git版本2.7.1),使用bindfs。

酿造安装bindfs

cd / path / to / git_controlled_dir

mkdir local_copy_dir

bindfs </full/path/ source_dir> </full/path/ local_dir >

其他评论暗示了这一点,但在其他答案中没有明确提供。希望这能为大家节省一些时间。

我使用的是Git 1.5.4.3,如果传递的符号链接后面有斜杠,它会跟着它。如。

# Adds the symlink itself
$ git add symlink

# Follows symlink and adds the denoted directory's contents
$ git add symlink/

符号链接的转换可能很有用。链接到Git文件夹中,而不是脚本中的符号链接。