git 忽略 .gitattributes 模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5524731/
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-19 05:17:02  来源:igfitidea点击:

git ignoring .gitattributes pattern

gitgitattributes

提问by iain

I've a directory structure like this:

我有一个这样的目录结构:

root/
  .git
  deploy/
  Site/
    blah/
    more_blah/
      something.local
      else.development
    Rakefile
    .gitattributes


Edit: to further clarify the above, directories have a trailing /and children are indented beneath a directory, so blahand more_blahare directories but Rakefileand .gitattributesare files, but all four are children of Site.

编辑:为了进一步澄清上述内容,目录有一个尾随/和子目录在目录下缩进,因此blahmore_blah是目录但Rakefile.gitattributes是文件,但所有四个都是Site.



I'm running git-archivefrom the Sitedirectory like so:

git-archiveSite目录中运行,如下所示:

git archive --format=tar --prefix=git-v0.0.1/ v0.0.1 | gzip > ../deploy/git-v0.0.1.tar.zip

but whatever pattern I put in .gitattributes, the resulting archive always contains Rakefile. I've tried:

但是无论我在 .gitattributes 中放入什么模式,生成的存档总是包含Rakefile. 我试过了:

  • Rakefile
  • Site/Rakefile
  • */Rakefile
  • ./Rakefile
  • Rakefile*
  • *
  • 文件
  • 站点/Rakefile
  • */Rakefile
  • ./Rakefile
  • Rakefile*
  • *

None of them work as I'd expect. Is anyone willing to point out the obvious yet non-obvious to me solution? Any help is much appreciated.

他们都没有像我期望的那样工作。有没有人愿意指出对我来说明显但不明显的解决方案?任何帮助深表感谢。



My apologies for not being clear.

我很抱歉没有说清楚。

  • I said the patternI was using didn't seem to work, but I am using "export-ignore" after the pattern.
  • Rakefileis not a directory, just a file
  • The .gitattributes file is successful in removing other patterns from the archive, Rakefileis not the only pattern used, but is the only one that doesn't work. It doesn't work whether I have it on its own or with other patterns, and at any place in the file.This is not true, due to having renamed certain files but not archiving the commit with the rename I was appearing to get some good results. My bad! :S
  • 我说我使用的模式似乎不起作用,但我在模式后使用了“export-ignore”。
  • Rakefile不是目录,只是文件
  • .gitattributes 文件成功地从存档中删除了其他模式,Rakefile它不是唯一使用的模式,而是唯一一个不起作用的模式。无论我是单独使用它还是使用其他模式,以及在文件中的任何位置,它都不起作用。这不是真的,因为重命名了某些文件但没有使用重命名归档提交,我似乎得到了一些不错的结果。我的错!:S

This is my .gitattributes(sitting in the directory Site)

这是我的.gitattributes(坐在目录中Site

Rakefile        export-ignore
*.local         export-ignore
*.development   export-ignore
*.staging       export-ignore

采纳答案by iain

I believe @Jefromi gave the information needed for me to resolve this with his comments, but is too humble to take the credit, and I'd like to keep my acceptance rating at 100% (quite rightly) so I'll give the answer here:

我相信@Jefromi 提供了我用他的评论解决这个问题所需的信息,但他太谦虚了,无法获得荣誉,我想将我的接受率保持在 100%(非常正确),所以我会给出答案这里:



Ok, two things were needed. --worktree-attributeson its own did not work, but when I moved the .gitattributesfile into the root dir from the Site dir, then it worked. Again, the Git book implies that the file doesn't need to be in the root for it to work"... (normally the root of your project)", so I feel a bit let down by those docs (for once). I also think it's not-what-you'd-think behaviour to have to opt in the file when .gitignore just works, IMO.

好的,需要两件事。--worktree-attributes本身不起作用,但是当我将.gitattributes文件从站点目录移动到根目录时,它起作用了。同样,Git 书暗示该文件不需要在根目录中才能工作“......(通常是您项目的根目录)”,所以我对这些文档感到有点失望(一次) . 我还认为,当 .gitignore 正常工作时,必须选择文件并不是你所想的行为,IMO。

回答by Alexander Pravdin

Not sure this is common case but I had trouble excluding folder testsfrom source tree which have many nested levels of folders. If I wrote only this line to .gitattributes

不确定这是常见情况,但我无法tests从具有许多嵌套文件夹级别的源树中排除文件夹。如果我只将这一行写到 .gitattributes

tests/* export-ignore

It didnt work and entire directory was remain in archive. The solution was to add wildcards for all subdirs levels:

它不起作用,整个目录都保留在存档中。解决方案是为所有子目录级别添加通配符:

tests/* export-ignore
tests/*/* export-ignore
tests/*/*/* export-ignore
tests/*/*/*/* export-ignore
tests/*/*/*/*/* export-ignore

With these lines the testsdirectory finally disappeared from archive.

有了这些行,tests目录终于从存档中消失了。

回答by VonC

Note: to ignore a directory, you needs to have a '/' at the end of said directory.

注意:要忽略目录,您需要/在该目录的末尾有一个“ ”。

Rakefile/

For archive, like Arrowmastermentions in his answer, and like the Pro Git book details, you need the export-ignoreoption:

对于存档,就像Arrowmaster他的回答中提到的那样,并且像Pro Git book details 一样,您需要以下export-ignore选项:

Rakefile/ export-ignore

回答by Holger B?hnke

With git version 1.7.2.5, which is the default on debian squeeze (hence this post), there must notbe a slash at the end to ignore a directory. So in order to ignore the deploy dir in the above question the following line must be used (no slash):

使用 git 版本 1.7.2.5,这是 debian Squeeze 的默认设置(因此是这篇文章),末尾不能有斜杠来忽略目录。因此,为了忽略上述问题中的部署目录,必须使用以下行(无斜杠):

deploy        export-ignore

This is in contradiction to the documentation (man gitattributes / man gitignore) and the git book. The documentation of gitattributes references gitignore. Interestingly dirs with a slash (deploy/) work in a .gitignore file.

这与文档 (man gitattributes / man gitignore) 和 git book 相矛盾。gitattributes 的文档参考了 gitignore。有趣的是,带有斜杠 (deploy/) 的目录在 .gitignore 文件中工作。

I did not test later versions of git.

我没有测试更高版本的 git。

回答by Tim Lum

For directories I had problems with different versions of git, so I had to include an entry bothwith and without a training slash:

对于目录我有不同版本的git的问题,所以我必须包括一个条目与不培训斜线:

  • foo/bar export-ignore
  • foo/bar/ export-ignore
  • foo/bar 导出忽略
  • foo/bar/ 导出忽略

Where foo/bar is relative to the where I ran git archive, but the actual .gitattributes was in the project root directory in addition to --worktree-attributesas noted above.

其中 foo/bar 与我运行git archive 的位置有关,但实际的 .gitattributes 除了上面提到的--worktree-attributes之外,还在项目根目录中。

回答by Arrowmaster

Are you trying to have the files included in the repository but not in the archive created from git archive? If so the syntax of your .gitattributesfiles wrong. If not then .gitattributesis not what you should be using.

您是否尝试将文件包含在存储库中但不包含在从创建的存档中git archive?如果是这样,您的.gitattributes文件的语法错误。如果不是,则.gitattributes不是您应该使用的。

To have files excluded from the archive produced by git archiveyou should put the following into the .gitattrubutes.

要从git archive您生成的存档中排除文件,应将以下内容放入.gitattrubutes.

Rakefile export-ignore

回答by JerodG

If you want git to ignore a file put it in the .gitignorefile notthe .attributesfile.

如果你想git的忽略文件将其放入的.gitignore文件中没有.attributes文件。

If you want to ignore your Rakefile put the following in the .gitignore file at the root of your project.

如果您想忽略您的 Rakefile,请将以下内容放入项目根目录的 .gitignore 文件中。

/**/Rakefile

Specify the full path if you have more than one and you only want to ignore one of them.

如果您有多个路径并且只想忽略其中之一,请指定完整路径。

A few examples of how to implement pattern matching for these files:

关于如何为这些文件实现模式匹配的几个示例:

对于所有文件夹中的所有文件
/**/*.ext
对于所有文件夹中的文件
/**/some_file.ext
根文件夹中的文件
/some_file.ext
文件夹中的文件
/some_folder/some_file.ext
文件夹中的所有文件
/some_folder/*