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

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


当前回答

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

sudo mount --bind SOURCEDIRECTORY TARGETDIRECTORY

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

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

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

/sourcedir /targetdir none bind

其他回答

这是一个预提交钩子,它用这些符号链接的内容替换索引中的符号链接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 2.3.2+(2015年第一季度)中,还有一种情况下Git将不再遵循符号链接:参见由Junio C Hamano (gitster) (Git的主要维护者)提交e0d201b

适用:不要触及符号链接以外的文件

Because Git tracks symbolic links as symbolic links, a path that has a symbolic link in its leading part (e.g. path/to/dir/file, where path/to/dir is a symbolic link to somewhere else, be it inside or outside the working tree) can never appear in a patch that validly applies, unless the same patch first removes the symbolic link to allow a directory to be created there. Detect and reject such a patch. Similarly, when an input creates a symbolic link path/to/dir and then creates a file path/to/dir/file, we need to flag it as an error without actually creating path/to/dir symbolic link in the filesystem. Instead, for any patch in the input that leaves a path (i.e. a non deletion) in the result, we check all leading paths against the resulting tree that the patch would create by inspecting all the patches in the input and then the target of patch application (either the index or the working tree). This way, we: catch a mischief or a mistake to add a symbolic link path/to/dir and a file path/to/dir/file at the same time, while allowing a valid patch that removes a symbolic link path/to/dir and then adds a file path/to/dir/file.

这意味着,在这种情况下,错误消息不会是像“%s: patch不适用”这样的通用错误,而是更具体的错误:

affected file '%s' is beyond a symbolic link

我厌倦了这里的每个解决方案,要么已经过时,要么需要root,所以我做了一个基于ld_preload的解决方案(仅限Linux)。

它连接到Git的内部,覆盖'这是一个符号链接吗?函数,允许符号链接被视为它们的内容。默认情况下,所有到回购外部的链接都是内联的;详情请参见链接。

嗯,mount -bind似乎对达尔文不起作用。

有人有什么诀窍吗?

(编辑)

好吧,我在Mac OS X上找到了答案,那就是做一个硬链接。只是这个API不是通过ln公开的,所以你必须用你自己的小程序来做这件事。以下是该程序的链接:

在Mac OS X中创建目录硬链接

享受吧!

注意:从Git 1.6.1开始,这个通知就已经过时了。Git过去是这样的,现在不再是这样了。


Git默认情况下尝试存储符号链接,而不是遵循它们(为了紧凑,这通常是人们想要的)。

但是,当符号链接是一个目录时,我意外地设法让它在符号链接之外添加文件。

例如:

  /foo/
  /foo/baz
  /bar/foo --> /foo
  /bar/foo/baz

通过做

 git add /bar/foo/baz

当我尝试时,它似乎起作用了。然而,这种行为在当时是我不想要的,所以我不能给你更多的信息。