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

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

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

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

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


当前回答

下面是一个PowerShell脚本,用Windows替换Unix符号链接。

# This fixes permission denied errors you might get when
# there are Git symbolic links being used on repositories that
# you share in both POSIX (usually the host) and Windows (VM).
#
# This is not an issue if you are checking out the same
# repository separately in each platform. This is only an issue
# when it's the same working set (AKA make a change without
# committing on OS X, go to Windows VM and Git status would show
# you that change).
#
# Based on this answer on Stack Overflow: http://stackoverflow.com/a/5930443/18475
#
# No warranties. Good luck.
#
# NOTE: It must be run in elevated PowerShell

$ROOT = $PWD

$symlinks = &git ls-files -s | gawk '/120000/{print $4}'
foreach ($symlink in $symlinks) {
  $content = &Get-Content $symlink
  $content = $content.Replace("/", "\")
  $filename = $symlink.Split("/")[-1]
  cd (dirname $symlink)
  rm $filename
  echo Linking $content -> $filename
  New-Item -ItemType SymbolicLink -Path $filename -Target $content
  &git update-index --assume-unchanged $symlink
  cd $ROOT
}

其他回答

因此,自从发布了很多这些答案以来,Git已经发生了变化,下面是正确的指令,以使符号链接在Windows中正确工作:

2018年8月


1. 确保Git安装时带有符号链接支持

2. 告诉Bash创建硬链接而不是符号链接

(git)文件夹/ etc / bash . bashrc

添加到底部- MSYS=winsymlinks:nativestrict

3.将Git配置设置为使用符号链接

git config core.symlinks true

or

git clone -c core.symlinks=true <URL>

注意:我已经尝试将其添加到全局Git配置中,但目前它对我来说并不适用,所以我建议将其添加到每个存储库中…

4. 提取存储库

注意:除非您在最新版本的Windows 10中启用了开发人员模式,否则您需要以管理员身份运行Bash来创建符号链接

5. 重置所有符号链接(可选)

如果您有一个现有的存储库,或者正在使用子模块,您可能会发现符号链接没有被正确地创建,因此可以运行这些命令来刷新存储库中的所有符号链接。

find -type l -delete
git reset --hard

注意:这将重置自上次提交以来的所有更改,所以请确保您已经先提交了

下面是一个PowerShell脚本,用Windows替换Unix符号链接。

# This fixes permission denied errors you might get when
# there are Git symbolic links being used on repositories that
# you share in both POSIX (usually the host) and Windows (VM).
#
# This is not an issue if you are checking out the same
# repository separately in each platform. This is only an issue
# when it's the same working set (AKA make a change without
# committing on OS X, go to Windows VM and Git status would show
# you that change).
#
# Based on this answer on Stack Overflow: http://stackoverflow.com/a/5930443/18475
#
# No warranties. Good luck.
#
# NOTE: It must be run in elevated PowerShell

$ROOT = $PWD

$symlinks = &git ls-files -s | gawk '/120000/{print $4}'
foreach ($symlink in $symlinks) {
  $content = &Get-Content $symlink
  $content = $content.Replace("/", "\")
  $filename = $symlink.Split("/")[-1]
  cd (dirname $symlink)
  rm $filename
  echo Linking $content -> $filename
  New-Item -ItemType SymbolicLink -Path $filename -Target $content
  &git update-index --assume-unchanged $symlink
  cd $ROOT
}

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

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

git add --all
git add --all

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

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

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

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

看看维基吧。

我建议您不要在存储库中使用符号链接。将实际内容存储在存储库中,然后在存储库外放置指向内容的符号链接。

因此,假设您正在使用一个存储库来比较在类unix系统上托管站点与在Windows上托管站点。将内容存储在您的存储库中,例如/httpRepoContent和c:\httpRepoContent,这是通过Git、SVN等同步的文件夹。

然后,替换你的web服务器的内容文件夹(/var/www和c:\程序文件\web server\www{名称并不重要,如果你必须编辑}),用一个符号链接到你的存储库中的内容。web服务器将看到内容实际上是在“正确”的位置,但你可以使用你的源代码控制。

但是,如果您需要在存储库中使用符号链接,您将需要研究一些类似于前/后提交脚本的东西。我知道您可以使用它们来做一些事情,例如通过格式化程序解析代码文件,因此应该可以在平台之间转换符号链接。

如果有人知道一个好地方可以学习如何为常见的源代码控制、SVN、Git和MG执行这些脚本,那么请添加评论。