告诉 git 忽略符号链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8650871/
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
Telling git to ignore symlinks
提问by user225643
This question has appeared in similar forms hereand here, but they don't seem to match up with what I'm looking for.
这个问题在这里和这里以类似的形式出现,但它们似乎与我正在寻找的不匹配。
I'm making a project in StaticMatic, a Ruby static site generator. Basically, it's just a src/ directory with Haml templates, Sass, and CoffeeScript. StaticMatic provides a development server to keep compiling these into a static site, as well as a build command that generates the static site in build/.
我正在用 Ruby 静态站点生成器 StaticMatic 创建一个项目。基本上,它只是一个包含 Haml 模板、Sass 和 CoffeeScript 的 src/ 目录。StaticMatic 提供了一个开发服务器来不断地将这些编译成静态站点,以及在 build/ 中生成静态站点的构建命令。
My modification to StaticMatic is to allow the addition of src/_modules/foo/, which might contain src/_modules/foo/bar.haml. When running the server or building the site, a symlink would be created at src/bar.haml which points to the file in foo/.
我对 StaticMatic 的修改是允许添加 src/_modules/foo/,其中可能包含 src/_modules/foo/bar.haml。在运行服务器或构建站点时,将在 src/bar.haml 中创建一个符号链接,指向 foo/ 中的文件。
So far so good. (Conflicts are handled, etc.)
到现在为止还挺好。(处理冲突等)
The reasoning behind separate directories in _modules/ is that they could be tracked as git submodules and checked out independently by other teams. Essentially, this allows multiple teams to work on different pages (actually JS apps) in one static site without duplicating the main layout and such.
_modules/ 中的单独目录背后的原因是它们可以作为 git 子模块进行跟踪并由其他团队独立检出。从本质上讲,这允许多个团队在一个静态站点中处理不同的页面(实际上是 JS 应用程序),而无需复制主布局等。
The hitch is that git wants to think of these symlinks as files. For instance, git status
shows:
问题在于 git 想要将这些符号链接视为文件。例如,git status
显示:
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be commited)
#
# src/_modules/bar/foo.haml
# src/foo.haml
...when I really just want it to show src/_modules/bar/foo.haml
and to ignore src/foo.haml
...当我真的只是想让它显示src/_modules/bar/foo.haml
和忽略时src/foo.haml
One approach would be to have my link-generating code append the links to .gitignore, but messing with .gitignore programmatically strikes me as prone to error. (Perhaps this concern isn't reasonable?)
一种方法是让我的链接生成代码将链接附加到 .gitignore,但以编程方式弄乱 .gitignore 会让我觉得容易出错。(也许这种担心不合理?)
My ideal fix would be something like:
我的理想修复是这样的:
[filetype = link]
...in .gitignore. As far as I know nothing like this is possible, or is it?
...在.gitignore 中。据我所知,这样的事情是不可能的,或者是吗?
回答by Biswajit_86
This seems to be a better idea
这似乎是一个更好的主意
find . -type l | sed -e s'/^\.\///g' >> .gitignore
Find outputs a "./" prefix for all files. Unless you trim it off, gitignore is unable to act on them . Once you trim the ".\" at the beginning , it works like a charm
Find 为所有文件输出一个“./”前缀。除非你修剪它,否则 gitignore 无法对它们采取行动。一旦你在开头修剪了“.\”,它就像一个魅力
回答by edwardsharp
Depending on what version of git you are using it should follow symlinks. There's a config setting core.symlinks, that may be set to false and thus not letting git follow them as directories (assuming git >= 1.6). It seems completely reasonable to have your symlinking script also append those links to the .gitignore file or just add them yourself. You could also do something like find . -type l >> .gitignore
根据您使用的 git 版本,它应该遵循符号链接。有一个配置设置 core.symlinks,它可能被设置为 false,因此不会让 git 作为目录跟随它们(假设 git >= 1.6)。让您的符号链接脚本也将这些链接附加到 .gitignore 文件或只是自己添加它们似乎是完全合理的。你也可以做类似的事情find . -type l >> .gitignore
回答by Chris Rasys
We had a similar issue where we needed to ignore a symlinked directory ora real directory, but nota subdirectory of the same name ora file starting with the same letters. For example:
我们有我们需要忽略符号链接目录类似的问题或者一个真正的目录,但不是相同的名称的子目录或开始用相同的字母的文件。例如:
- Ignore - (symlink) ./media
- Ignore - (directory) ./media
- Do not ignore - ./something/media
- Do not ignore - ./media.bak
- Do not ignore - ./media-somethingelse
- 忽略 - (符号链接) ./media
- 忽略 - (目录) ./media
- 不要忽略 - ./something/media
- 不要忽略 - ./media.bak
- 不要忽略 - ./media-somethingelse
I ended up using this in the .gitignore:
我最终在 .gitignore 中使用了它:
media
!*/media
Which looks a little strange but is basically saying (line 1) ignore everything "media" but (line 2) do notignore anything "media" that isn't in the root directory. This method does require a little specificity (in regards to not ignoring things in subdirectories) but should be pretty easy to adapt/extend.
这看起来有点奇怪,但基本上是说(第 1 行)忽略所有“媒体”,但(第 2 行)不要忽略根目录中没有的任何“媒体”。这种方法确实需要一些特殊性(关于不忽略子目录中的内容),但应该很容易适应/扩展。
回答by ColinM
My answer from another questionis relevant:
我在另一个问题中的回答是相关的:
for f in $(git status --porcelain | grep '^??' | sed 's/^?? //'); do
test -L "$f" && echo $f >> .gitignore;
test -d "$f" && echo $f\* >> .gitignore;
done
回答by Dmitry Astafyev
I used the most suitable solution to me - naming agreement.
我用了最适合我的解决方案——命名协议。
With filesEach symlink to file in my solution had a prefix in a name like "bla.outsidemodule.ts". In .gitignore file, I had just:
使用文件我的解决方案中文件的每个符号链接在名称中都有一个前缀,例如“bla.outsidemodule.ts”。在 .gitignore 文件中,我刚刚:
**/*.outsidemodule.*
With foldersAlso on the root of the solution, I had folder "platform", which had common libs/modules for other parts of the solution. The structure was like it:
使用文件夹同样在解决方案的根目录上,我有文件夹“平台”,其中包含用于解决方案其他部分的通用库/模块。结构是这样的:
- ./platform <- sources
- ./client/src/platform <- symlink to ./platform
- ./server/src/platform <- symlink to ./platform
- ./exchange/src/platform <- symlink to ./platform
- ./service/src/platform <- symlink to ./platform
And in .gitignore just:
而在 .gitignore 中:
**/platform <- exclude all "platform" folders
!./platform <- do not exclude folder "platform" if it's on a root
回答by L33tCh
My solution might seem silly, but I'd rather do this than update the .gitignore file every time a new file is added.
我的解决方案可能看起来很愚蠢,但我宁愿这样做,也不愿在每次添加新文件时更新 .gitignore 文件。
I merely leave the links with a default name like "link to xxx" and then add the following line to my .gitignore file:
我只是将链接保留为默认名称,例如“链接到 xxx”,然后将以下行添加到我的 .gitignore 文件中:
link to *
Then you just ensure you do not name any other files/folders with a name starting with "link to " and you're sorted.
然后,您只需确保没有以“链接到”开头的名称命名任何其他文件/文件夹,并且您已排序。
回答by owford
If you really want to ignore a filename if it's a symlink or file but not if it is a directory, you can add to .gitignore :
如果你真的想忽略一个文件名,如果它是一个符号链接或文件,而不是一个目录,你可以添加到 .gitignore :
/media
!/media/
To formulate it like @Chris_Rasys, this will :
要像@Chris_Rasys 一样制定它,这将:
- Ignore - (symlink) ./media
- Ignore- (file) ./media
- Do not ignore- (directory) ./media
- Do not ignore - ./something/media
- Do not ignore - ./media.bak
- Do not ignore - ./media-somethingelse
- 忽略 - (符号链接) ./media
- 忽略- (文件) ./media
- 不要忽略- (目录) ./media
- 不要忽略 - ./something/media
- 不要忽略 - ./media.bak
- 不要忽略 - ./media-somethingelse
回答by Larry Helms
I don't keep the executables, and symlinks in my repository... so I want to omit them as well when I do a "git status".
我没有在我的存储库中保留可执行文件和符号链接......所以当我执行“git status”时我也想省略它们。
If, like me, you want to ignore symlinks, directories and executables (ala some of the files in $NAG_HOME/libexec you can add those files into .gitignore as follows:
如果像我一样,您想忽略符号链接、目录和可执行文件(例如 $NAG_HOME/libexec 中的一些文件,您可以将这些文件添加到 .gitignore 中,如下所示:
file * | egrep 'ELF|symbolic|directory' | awk -F\: '{ print }' >> .gitignore
echo ".gitignore" >> .gitignore
Then, when you do a git status, it won't spit back the list of those files if you have not added them to your repository.
然后,当您执行 git status 时,如果您尚未将它们添加到您的存储库中,它不会吐回这些文件的列表。