我们的开发人员混合使用Windows和基于unix的操作系统。因此,在Unix机器上创建的符号链接成为Windows开发人员的一个问题。在Windows (MSysGit)中,符号链接被转换为一个文本文件,并带有它所指向的文件的路径。相反,我想将符号链接转换为实际的Windows符号链接。

我对此的(更新的)解决方案是:

编写一个检出后脚本,递归地查找“符号链接”文本文件。 将它们替换为Windows符号链接(使用mklink),具有与虚拟“symbolic link”相同的名称和扩展名 通过在文件.git/info/exclude中添加一个条目来忽略这些Windows符号链接

我还没有实现这个方法,但我相信这是解决这个问题的可靠方法。

如果有的话,你认为这种方法有什么缺点? 这个后签出脚本是可实现的吗?也就是说,我能递归地找到Git创建的虚拟“符号链接”文件吗?


当前回答

Git SCM的最新版本(在版本2.11.1上测试)允许启用符号链接。但是你必须再次用符号链接克隆存储库git clone -c core。符号链接= true > < URL。该命令需要使用管理员权限执行。也可以在Windows上使用mklink创建符号链接。

看看维基吧。

其他回答

简单的回答:如果您可以启用开发人员模式,它们现在得到了很好的支持。

来自Windows 10的Symlinks !:

Now in Windows 10 Creators Update, a user (with admin rights) can first enable Developer Mode, and then any user on the machine can run the mklink command without elevating a command-line console. What drove this change? The availability and use of symlinks is a big deal to modern developers: Many popular development tools like git and package managers like npm recognize and persist symlinks when creating repos or packages, respectively. When those repos or packages are then restored elsewhere, the symlinks are also restored, ensuring disk space (and the user’s time) isn’t wasted.

“创建者更新”的所有其他公告很容易被忽略,但如果启用了开发人员模式,则可以在没有提升权限的情况下创建符号链接。您可能必须重新安装Git并确保启用了符号链接支持,因为默认情况下并没有启用符号链接支持。

我一直在寻找一种简单的解决方案来处理Windows上的Unix符号链接。非常感谢您在之前的回答中提供Git别名。

可以对rm-符号链接进行一个小小的优化,以便在别名意外第二次运行时它不会删除目标文件夹中的文件。请观察循环中的新if条件,以确保在逻辑运行之前文件不是到目录的链接。

git config --global alias.rm-symlinks '!__git_rm_symlinks(){
for symlink in $(git ls-files -s | egrep "^120000" | cut -f2); do
    *if [ -d "$symlink" ]; then
      continue
    fi*
    git rm-symlink "$symlink"
    git update-index --assume-unchanged "$symlink"
done
}; __git_rm_symlinksenter

我们使用的一个简单的技巧是调用git add——连续两次。

例如,我们的Windows 7提交脚本调用:

git add --all
git add --all

第一个add将链接视为文本,并添加用于删除的文件夹。

第二个add正确地遍历链接,并通过恢复文件来撤销删除操作。

它没有其他一些提议的解决方案那么优雅,但它是对添加了符号链接的一些遗留环境的简单修复。

您可以通过查找模式为120000的文件来查找符号链接,可能使用以下命令:

git ls-files -s | awk '/120000/{print $4}'

一旦你替换了链接,我建议使用git update-index——assume-unchanged将它们标记为不变,而不是将它们列在.git/info/exclude中。

我刚刚尝试了Git 2.30.0(发布2020-12-28)。

这并不是一个完整的答案,但还是有一些有用的花边新闻。(你可以随意做出自己的回答。)

Git Wiki入口

在安装Git for Windows时,有一个文档链接

这个链接把你带到这里:https://github.com/git-for-windows/git/wiki/Symbolic-Links——这是一个相当长的讨论。

供你参考:至少有三种“链接”。只是为了强调这个维基条目的一个重要方面:我不知道这一点,但有几种方法在表面上都是“某种”符号链接,但在技术层面上是非常不同的:

git bash's "ln -s" Which just copies things. Oh, boy. That was unexpected to me. (FYI: Plain Cygwin does not do this. Mobaxterm does not do this. Instead they both create something that their stat command actually recognizes as "symbolic link".) cmd.exe's builtin "mklink" command with the "/D" parameter Which creates a directory symbolic link. (See the Microsoft documentation) cmd.exe's builtin "mklink" command with the "/J" parameter. Which creates a directory junction AKA soft link AKA reparse point. (See the Microsoft documentation.)

发行说明条目

此外,在发布说明中还会不断弹出符号链接。截至2.30.0,这里仍然被列为“已知问题”:

在1703年以前的Windows 10上,或者当开发人员模式被关闭时,克隆带有符号链接的存储库时需要特殊权限,因此默认情况下禁用对符号链接的支持。使用git clone -c core。symlinks=true <URL>来启用它,参见这里的详细信息。