我最好是用一个shell脚本替换符号链接的副本,还是有另一种方式告诉Git遵循符号链接?
PS:我知道这不是很安全,但我只是想在一些特定的情况下这样做。
我最好是用一个shell脚本替换符号链接的副本,还是有另一种方式告诉Git遵循符号链接?
PS:我知道这不是很安全,但我只是想在一些特定的情况下这样做。
当前回答
@user252400建议的另一种实现是使用bwrap,这是一个小型setuid沙盒,可以在所有主要发行版中找到——通常默认安装。Bwrap允许你无需sudo就可以绑定挂载目录,并在git或shell退出时自动解除绑定。
假设你的开发过程不是很疯狂(见下文),在一个私有的命名空间中启动bash,并在git目录下绑定外部目录:
bwrap --ro-bind / / \
--bind {EXTERNAL-DIR} {MOUNTPOINT-IN-GIT-DIR} \
--dev /dev \
bash
然后做你通常做的所有事情,比如git添加、git提交等等。当你完成时,退出bash。干净简单。
注意:为了防止沙盒转义,bwrap不允许执行其他setuid二进制文件。详见man bwrap。
其他回答
我以前在符号链接之外添加文件已经有一段时间了。这在过去工作得很好,不需要做任何特别的安排。自从我更新到Git 1.6.1后,这就不再有效了。
您可以切换到Git 1.6.0来实现这个功能。我希望Git的未来版本会有一个Git -add标志,允许它再次跟随符号链接。
我所做的是将符号链接中的文件添加到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创建一个符号链接。
这种方法可能并不总是有效,但到目前为止对我来说效果很好。
在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
在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 >
其他评论暗示了这一点,但在其他答案中没有明确提供。希望这能为大家节省一些时间。