Ignore symbolic links in .gitignore

Deadly 提交于 2019-12-30 04:02:16

问题


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.


回答1:


Use git version >= 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.

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



回答2:


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:

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


来源:https://stackoverflow.com/questions/3511312/ignore-symbolic-links-in-gitignore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!