告诉 git 忽略符号链接
这个问题以类似的形式出现在此处和此处,但它们似乎与我正在寻找的内容不匹配。
我正在 StaticMatic 中制作一个项目,这是一个 Ruby 静态站点生成器。基本上,它只是一个包含 Haml 模板、Sass 和 CoffeeScript 的 src/ 目录。 StaticMatic 提供了一个开发服务器来持续将它们编译成静态站点,以及一个在 build/ 中生成静态站点的构建命令。
我对 StaticMatic 的修改是允许添加 src/_modules/foo/,其中可能包含 src/_modules/foo/bar.haml。当运行服务器或构建站点时,将在 src/bar.haml 处创建一个符号链接,该符号链接指向 foo/ 中的文件。
到目前为止,一切都很好。 (处理冲突等)
_modules/ 中单独目录背后的原因是它们可以作为 git 子模块进行跟踪并由其他团队独立检出。从本质上讲,这允许多个团队在一个静态站点中的不同页面(实际上是 JS 应用程序)上工作,而无需复制主布局等。
问题是 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
...当我真的只想显示 src/_modules/bar/foo.haml
并忽略 src/foo .haml
一种方法是让我的链接生成代码将链接附加到 .gitignore,但以编程方式弄乱 .gitignore 让我觉得容易出错。 (也许这种担心不合理?)
我理想的解决方案是:
[filetype = link]
...in .gitignore。据我所知,这是不可能的,是吗?
This question has appeared in similar forms here and 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/.
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/.
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.
The hitch is that git wants to think of these symlinks as files. For instance, git status
shows:
# 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
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?)
My ideal fix would be something like:
[filetype = link]
...in .gitignore. As far as I know nothing like this is possible, or is it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这似乎是一个更好的主意
Find 为所有文件输出一个“./”前缀。除非你将其修剪掉,否则 gitignore 无法对它们起作用。一旦你修剪开头的“.\”,它就像一个魅力
This seems to be a better idea
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
根据您使用的 git 版本,它应该遵循符号链接。有一个配置设置 core.symlinks,可以设置为 false,从而不允许 git 将它们作为目录跟踪(假设 git >= 1.6)。让您的符号链接脚本将这些链接附加到 .gitignore 文件或自己添加它们似乎是完全合理的。您还可以执行诸如
find 之类的操作。 -类型l>> .gitignore
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
我们遇到了类似的问题,需要忽略符号链接目录或真实目录,但不是同名子目录或文件以相同的字母开头。例如:
我最终使用.gitignore 中的这个:
看起来有点奇怪,但基本上是说(第 1 行)忽略所有“媒体”,但(第 2 行)不要忽略任何不是“媒体”的内容在根目录中。这种方法确实需要一点特殊性(关于不忽略子目录中的内容),但应该很容易适应/扩展。
We had a similar issue where we needed to ignore a symlinked directory or a real directory, but not a subdirectory of the same name or a file starting with the same letters. For example:
I ended up using this in the .gitignore:
Which looks a little strange but is basically saying (line 1) ignore everything "media" but (line 2) do not ignore 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.
我在另一个问题中的回答是相关的。
此方法仅添加未跟踪的符号链接,因此可以重复使用,而无需添加重复的条目、子模块中的符号链接或已被忽略的符号链接,或有意跟踪的符号链接。
My answer from another question is relevant.
This method only adds untracked symlinks so can be repeated without adding duplicate entries, symlinks that are in submodules or are otherwise already ignored, or intentionally tracked symlinks.
如果您确实想忽略文件名(如果它是符号链接或文件,但不是目录),则可以添加到 .gitignore :
要像 @Chris_Rasys 那样表述它,这将:
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 :
To formulate it like @Chris_Rasys, this will :
我使用了最适合我的解决方案——命名协议。
带有文件
我的解决方案中的每个文件符号链接都有一个名称前缀,例如“bla.outsidemodule.ts”。
在 .gitignore 文件中,我只有:
带有文件夹
另外,在解决方案的根目录中,我有文件夹“platform”,其中包含解决方案其他部分的通用库/模块。结构就像这样:
在 .gitignore 中:
I used the most suitable solution to me - naming agreement.
With files
Each symlink to file in my solution had a prefix in a name like "bla.outsidemodule.ts".
In .gitignore file, I had just:
With folders
Also 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:
And in .gitignore just:
我的解决方案可能看起来很愚蠢,但我宁愿这样做,也不愿在每次添加新文件时更新 .gitignore 文件。
我只是将链接保留为默认名称,例如“link to xxx”,然后将以下行添加到我的 .gitignore 文件中:
然后,您只需确保不命名任何其他文件/文件夹,其名称以“link to”开头,并且你已经排序了。
My solution might seem silly, but I'd rather do this than update the .gitignore file every time a new file is added.
I merely leave the links with a default name like "link to xxx" and then add the following line to my .gitignore file:
Then you just ensure you do not name any other files/folders with a name starting with "link to " and you're sorted.
我不保留可执行文件和符号链接在我的存储库中......所以当我执行“git status”时我也想忽略它们。
如果像我一样,您想忽略符号链接、目录和可执行文件(例如 $NAG_HOME/libexec 中的一些文件,您可以将这些文件添加到 .gitignore 中,如下所示:
然后,当您执行 git status 时,它不会吐出如果您尚未将这些文件添加到存储库,请返回这些文件的列表。
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".
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:
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.