忽略 git 项目上的任何“bin”目录

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

ignoring any 'bin' directory on a git project

gitgitignore

提问by Ben Hymers

I have a directory structure like this:

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

.git/
.gitignore
main/
  ...
tools/
  ...
...

Inside main and tools, and any other directory, at any level, there can be a 'bin' directory, which I want to ignore (and I want to ignore everything under it too). I've tried each of these patterns in .gitignore but none of them work:

在 main 和 tools 以及任何其他目录中,在任何级别,都可以有一个“bin”目录,我想忽略它(我也想忽略它下面的所有内容)。我已经在 .gitignore 中尝试了这些模式中的每一个,但它们都不起作用:

/**/bin/**/*
/./**/bin/**/*
./**/bin/**/*
**/bin/**/*
*/bin/**/*
bin/**/*
/**/bin/* #and the others with just * at the end too

Can anyone help me out? The first pattern (the one I think should be working) works just fine if I do this:

谁能帮我吗?如果我这样做,第一个模式(我认为应该工作的模式)工作得很好:

/main/**/bin/**/*

But I don't want to have an entry for every top-level directory and I don't want to have to modify .gitignore every time I add a new one.

但我不想为每个顶级目录都有一个条目,我不想每次添加新目录时都必须修改 .gitignore。

This is on Windows using the latest msysgit.

这是在 Windows 上使用最新的 msysgit。

EDIT: one more thing, there are files and directories that have the substring 'bin' in their names, I don't want those to be ignored :)

编辑:还有一件事,有些文件和目录的名称中包含子字符串“bin”,我不希望它们被忽略:)

回答by CB Bailey

Before version 1.8.2, **didn't have any special meaning in the .gitignore. As of 1.8.2 git supports **to mean zero or more sub-directories (see release notes).

在 1.8.2 版本之前,**.gitignore. 从 1.8.2 开始,git 支持**表示零个或多个子目录(请参阅发行说明)。

The way to ignore all directories called bin anywhere below the current level in a directory tree is with a .gitignorefile with the pattern:

忽略目录树中当前级别以下任何位置的所有名为 bin 的目录的方法是使用.gitignore具有以下模式的文件:

bin/

In the manpage, there an example of ignoring a directory called foousing an analogous pattern.

man页面中,有一个foo使用类似模式调用的忽略目录的示例。

Edit:If you already have any bin folders in your git index which you no longer wish to track then you need to remove them explicitly. Git won't stop tracking paths that are already being tracked just because they now match a new .gitignorepattern. Execute a folder remove (rm) from index only (--cached) recursivelly (-r). Command line example for root bin folder:

编辑:如果您的 git 索引中已经有任何您不再希望跟踪的 bin 文件夹,那么您需要明确删除它们。Git 不会仅仅因为它们现在匹配新.gitignore模式而停止跟踪已经被跟踪的路径。仅从索引(--cached)递归(-r)执行文件夹删除(rm)。根 bin 文件夹的命令行示例:

git rm -r --cached bin

回答by Michael Krelin - hacker

The .gitignoreof your dream seems to be:

.gitignore你的梦想似乎是:

bin/

on the top level.

在顶层。

回答by plancys

I think it is worth to mention for git beginners:

我认为对于 git 初学者来说值得一提的是:

If you already have a file checked in, and you want to ignore it, Git will not ignore the file if you add a rule later. In those cases, you must untrack the file first, by running the following command in your terminal:

git rm --cached

如果您已经签入了一个文件,并且您想忽略它,那么如果您稍后添加规则Git 将不会忽略该文件。在这些情况下,您必须首先通过在终端中运行以下命令来取消跟踪文件:

git rm --cached

So if you want add to ignore some directories in your local repository (which already exist) after editing .gitignore you want to run this on your root dir

因此,如果您想在编辑 .gitignore 后添加忽略本地存储库(已存在)中的某些目录,则您希望在根目录上运行它

git rm --cached -r .
git add .

It will basically 'refresh' your local repo and unstage ignored files.

它基本上会“刷新”您的本地存储库并取消暂存忽略的文件。

See:

看:

http://git-scm.com/docs/git-rm,

http://git-scm.com/docs/git-rm

https://help.github.com/articles/ignoring-files/

https://help.github.com/articles/ignoring-files/

回答by VonC

The **never properly worked before, but since git 1.8.2 (March, 8th 2013), it seems to be explicitly mentioned and supported:

**始终无法正常工作之前,但因为git的1.8.2(三月8日2013) ,它似乎被明确提及,并支持:

The patterns in .gitignoreand .gitattributesfiles can have **/, as a pattern that matches 0 or more levels of subdirectory.

E.g. "foo/**/bar" matches "bar" in "foo" itself or in a subdirectory of "foo".

.gitignore.gitattributes文件中的模式可以**/作为匹配 0 级或更多级子目录的模式

例如,“ foo/**/bar”匹配“ barfoo本身或“ foo”子目录中的“ ”。

In your case, that means this line might now be supported:

在您的情况下,这意味着现在可能支持此行:

/main/**/bin/

回答by wisbucky

[Bb]in/

matches both upper and lower case

匹配大小写

回答by Cory

I didn't see it mentioned here, but this appears to be case sensitive. Once I changed to /Bin the files were ignored as expected.

我没有看到这里提到它,但这似乎是区分大小写的。一旦我更改为 /Bin,文件就会按预期被忽略。

回答by Jaider

[Bb]inwill solve the problem, but... Here a more extensive list of things you should ignore (sample list by GitExtension):

[Bb]in将解决问题,但是......这里有一个更广泛的你应该忽略的事情列表(GitExtension的示例列表):

#ignore thumbnails created by windows
Thumbs.db
#Ignore files build by Visual Studio
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.sbr
obj/
[Rr]elease*/
_ReSharper*/

回答by Jochen van Wylick

If you're looking for a great global .gitignorefile for any Visual Studio ( .NET ) solution - I recommend you to use this one: https://github.com/github/gitignore/blob/master/VisualStudio.gitignore

如果您正在.gitignore为任何 Visual Studio ( .NET ) 解决方案寻找出色的全局文件 - 我建议您使用此文件:https: //github.com/github/gitignore/blob/master/VisualStudio.gitignore

AFAIK it has the most comprehensive .gitignorefor .NET projects.

AFAIK 它拥有最全面.gitignore的 .NET 项目。

回答by Bravo Yeung

Step 1: Add following content to the file .gitignore.

步骤 1:将以下内容添加到文件 .gitignore 中。

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/

# Visual Studio 2015 cache/options directory
.vs/

Step 2: Make sure take effect

第2步:确认生效

If the issue still exists, that's because settings in .gitignorecan only ignore files that were originally not tracked. If some files have already been included in the version control system, then modifying .gitignoreis invalid. To solve this issue completely, you need to open Git Bashor Package Manager Console(see screenshot below) to run following commands in the repository root folder.

如果问题仍然存在,那是因为.gitignore中的设置只能忽略最初未跟踪的文件。如果版本控制系统中已经包含了一些文件,那么修改.gitignore是无效的。要彻底解决这个问题,您需要打开Git BashPackage Manager Console(见下面的截图)在存储库根文件夹中运行以下命令。

$ git rm -r --cached .
$ git add .
$ git commit -m 'Update .gitignore'

PM consoleThen the issue will be completely solved.

下午控制台那么问题就彻底解决了。

回答by AnthonyC

Literally none of the answers actually worked for me; the only one that worked for me was (on Linux):

从字面上看,没有一个答案对我有用。唯一对我有用的是(在 Linux 上):

**/bin
(yes without the / in the end)

git version 2.18.0