windows 忽略 .gitignore 中的符号链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3511312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:03:49  来源:igfitidea点击:

Ignore symbolic links in .gitignore

windowslinuxgitsymlinkgitignore

提问by Andrei

Is it possible to tell Git to ignore symlinks ? I'm working with a mixed Linux / Windows environment and, as you know, symlinks are handled very differently between the two.

是否可以告诉 Git 忽略符号链接?我正在使用混合的 Linux / Windows 环境,如您所知,符号链接在两者之间的处理方式非常不同。

回答by Tilo

Use git version >= 1.6

使用 git 版本 >= 1.6

Git used to treat sym-links the same as regular files, but newer git versions (>= 1.6) check if a file is beyond a symbolic link and will throw a fatal error.

Git 过去将符号链接视为与常规文件相同,但较新的 git 版本 (>= 1.6) 会检查文件是否超出符号链接并抛出致命错误。

e.g.:

例如:

# git init 
# mkdir newdir 
# touch newdir/foo 
# git add newdir/foo 
# git commit -m 'add foo' 
# mv newdir /tmp/ 
# ln -s /tmp/newdir 
# touch newdir/bar 
# git add newdir/bar 
fatal: 'newdir/bar' is beyond a symbolic link

# git add/tmp/newdir
fatal: '/tmp/newdir' is outside repository

# git --version
git version 1.7.3.4

回答by ColinM

No, it is not possible to do this globally. However, if you have lots of symlinks here is a bash script that you can use to easily add them to your repo's .gitignore file:

不,不可能在全球范围内执行此操作。但是,如果您有很多符号链接,这里有一个 bash 脚本,您可以使用它轻松地将它们添加到您的 repo 的 .gitignore 文件中:

for f in $(git status --porcelain | grep '^??' | sed 's/^?? //'); do
    test -L "$f" && echo $f >> .gitignore; # add symlinks
    test -d "$f" && echo $f\* >> .gitignore; # add new directories as well
done