git gitignore,如何排除特定目录

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

gitignore, how to exclude a specific directory

gitrepositorygitignoreignore

提问by Whitewind617

I'm trying to utilize a .gitignorefile to exclude a specific directory, but I'm having some trouble. I looked up how to do this previously on a few forums, including stackoverflow, and while I found it a bit confusing, I had thought I understood it, but it seems to not be working as I thought.

我正在尝试利用.gitignore文件来排除特定目录,但遇到了一些麻烦。我以前在几个论坛上查过如何做到这一点,包括 stackoverflow,虽然我发现它有点混乱,但我以为我理解它,但它似乎不像我想象的那样工作。

So I have a repo here, and there's a specific directory in the top level called "images" that I want to exclude. Here's what I put in my .gitignorefile:

所以我在这里有一个 repo,在顶层有一个名为“images”的特定目录,我想排除它。这是我放在我的.gitignore文件中的内容:

images/

I added and committed everything, all well and good, it ignored the images director in the top level. But then I realized it also ignored forums/images. So what's the deal? How do I handle ignoring a specific directory, but not any others with the same name?

我添加并提交了所有内容,一切都很好,它忽略了顶层的图像总监。但后来我意识到它也忽略了论坛/图片。那么有什么关系呢?如何处理忽略特定目录,但不忽略任何其他同名目录?

回答by gview

You want to set the root to / which will be relative to the location of your gitignore file.

您想将根设置为 / ,这将相对于您的 gitignore 文件的位置。

So you want:

所以你要:

/images

That will ignore any file OR directory named images in the same directory as the gitignore file you are using.

这将忽略与您正在使用的 gitignore 文件位于同一目录中的任何名为 images 的文件或目录。

If you want to specify that it should only match the images directory then add the trailing slash:

如果你想指定它应该只匹配图像目录然后添加尾部斜杠:

/images/

回答by scniro

Try changing images/to /images/per the .gitignore docs

尝试根据.gitignore 文档更改images//images/

A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".

前导斜杠与路径名的开头匹配。例如,“/*.c”匹配“cat-file.c”但不匹配“mozilla-sha1/sha1.c”。