如何让Git忽略除子目录之外的所有内容?

时间:2021-03-25 07:18:07

I want to ignore all files in my repository except those that occur in the bin subdirectory. I tried adding the following to my .gitignore:

我想忽略存储库中的所有文件,除了bin子目录中的文件。我试着给我的。gitignore添加了以下内容:

*!bin/*

This does not have the desired effect, however: I created a new file inside of bin/, but doing git status still shows nothing to commit (working directory clean).

但是,这并没有预期的效果:我在bin/中创建了一个新文件,但是做git状态仍然没有显示要提交的内容(工作目录清理)。

Any suggestions?

有什么建议吗?

9 个解决方案

#1


217  

This ignores root files & root directories, then un-ignores the root bin directory:

这就忽略了根文件和根目录,而忽略了根目录:

/*/*/!/bin/

This way you get all of the bin directory, including subdirectories and their files.

这样就可以获得所有bin目录,包括子目录和它们的文件。

#2


76  

Here how to ignore everything exept one directory "MY_SUPER_DUPER_TEMPLATE_directory" in some directory

在这里,如何忽略某个目录中的所有exept目录“MY_SUPER_DUPER_TEMPLATE_directory”

Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory

结构:/ / MY_SUPER_DUPER_TEMPLATE_directory bitrix /模板

/*!/bitrix/bitrix/*!/bitrix/templates/bitrix/templates/*!/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory*.DS_Store*.gitignore

#3


44  

You must exclude everything on the way to the destinations, but you must include the destinations:

你必须排除前往目的地途中的一切,但必须包括目的地:

Note: This is an exclusion file (i.e. .gitignore) so the logic is inverted. Ignore all, except the tsp directory, ignore all in tsp directory, except the src directory...

注意:这是一个排除文件(例如.gitignore),所以逻辑是反向的。忽略所有,除了tsp目录,忽略所有的tsp目录,除了src目录…

/*!/tsp/tsp/*!/tsp/src/tsp/src/*!/tsp/src/*.h!/tsp/src/*.cpp!/tsp/src/data.txt!/.gitignore

#4


36  

The only issue you have is that the bin directory itself is not matched by the bin/* pattern so git isn't even look in the bin directory.

惟一的问题是bin目录本身没有与bin/*模式匹配,因此git甚至没有在bin目录中查找。

There are two solutions that spring to mind.

有两种解决方案。

.gitignore :

.gitignore:

*!/bin/!bin/*

or

.gitignore :

.gitignore:

*!/bin/

bin/.gitignore :

bin /。gitignore:

!*

I prefer the second solution as the first solution won't stop ignoring files in the bin directory that are in subdirectories that aren't called bin. This may or may not matter in your situation.

我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略bin目录中的文件,这些文件位于不调用bin的子目录中。这可能对你的情况有影响,也可能无关紧要。

#5


10  

I think a better way would be to anchor each pattern to the top git directory by starting the pattern with a slash:

我认为更好的方法是将每个模式锚定到git目录的顶部,以斜线开始模式:

/*!/public_html!/.gitignore

Instead of ignoring all files it will only ignore top level files, not the ones in the directory you dont want to ignore.

它不会忽略所有文件,而是只会忽略*文件,而不会忽略目录中的文件。

#6


10  

From the official git doc, one of the examples says:

来自官方的git doc,其中一个例子是:

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

排除除特定目录foo/bar之外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):

$ cat .gitignore# exclude everything except directory foo/bar/*!/foo/foo/*!/foo/bar

For an explanation about the leading slash: When to use leading slash in gitignore.

关于前斜杠的解释:何时在gitignore中使用前斜杠。

#7


10  

Try following in latest GIT version.

尝试使用最新的GIT版本。

*!*/!/path/to/your/dir/**

#8


6  

This .gitignore works for me:

gitignore对我很有用:

*//*!bin/

#9


5  

try this obscure git bug.

试试这个模糊的git bug。

!bin**/**

Hope it helps :)

希望它能帮助:)

Supporting docs git bug sample

支持文档git错误示例

PS: try it first before commenting.

在评论之前先试一试。

#1


217  

This ignores root files & root directories, then un-ignores the root bin directory:

这就忽略了根文件和根目录,而忽略了根目录:

/*/*/!/bin/

This way you get all of the bin directory, including subdirectories and their files.

这样就可以获得所有bin目录,包括子目录和它们的文件。

#2


76  

Here how to ignore everything exept one directory "MY_SUPER_DUPER_TEMPLATE_directory" in some directory

在这里,如何忽略某个目录中的所有exept目录“MY_SUPER_DUPER_TEMPLATE_directory”

Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory

结构:/ / MY_SUPER_DUPER_TEMPLATE_directory bitrix /模板

/*!/bitrix/bitrix/*!/bitrix/templates/bitrix/templates/*!/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory*.DS_Store*.gitignore

#3


44  

You must exclude everything on the way to the destinations, but you must include the destinations:

你必须排除前往目的地途中的一切,但必须包括目的地:

Note: This is an exclusion file (i.e. .gitignore) so the logic is inverted. Ignore all, except the tsp directory, ignore all in tsp directory, except the src directory...

注意:这是一个排除文件(例如.gitignore),所以逻辑是反向的。忽略所有,除了tsp目录,忽略所有的tsp目录,除了src目录…

/*!/tsp/tsp/*!/tsp/src/tsp/src/*!/tsp/src/*.h!/tsp/src/*.cpp!/tsp/src/data.txt!/.gitignore

#4


36  

The only issue you have is that the bin directory itself is not matched by the bin/* pattern so git isn't even look in the bin directory.

惟一的问题是bin目录本身没有与bin/*模式匹配,因此git甚至没有在bin目录中查找。

There are two solutions that spring to mind.

有两种解决方案。

.gitignore :

.gitignore:

*!/bin/!bin/*

or

.gitignore :

.gitignore:

*!/bin/

bin/.gitignore :

bin /。gitignore:

!*

I prefer the second solution as the first solution won't stop ignoring files in the bin directory that are in subdirectories that aren't called bin. This may or may not matter in your situation.

我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略bin目录中的文件,这些文件位于不调用bin的子目录中。这可能对你的情况有影响,也可能无关紧要。

#5


10  

I think a better way would be to anchor each pattern to the top git directory by starting the pattern with a slash:

我认为更好的方法是将每个模式锚定到git目录的顶部,以斜线开始模式:

/*!/public_html!/.gitignore

Instead of ignoring all files it will only ignore top level files, not the ones in the directory you dont want to ignore.

它不会忽略所有文件,而是只会忽略*文件,而不会忽略目录中的文件。

#6


10  

From the official git doc, one of the examples says:

来自官方的git doc,其中一个例子是:

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

排除除特定目录foo/bar之外的所有内容(注意/* -没有斜杠,通配符也会排除foo/bar内的所有内容):

$ cat .gitignore# exclude everything except directory foo/bar/*!/foo/foo/*!/foo/bar

For an explanation about the leading slash: When to use leading slash in gitignore.

关于前斜杠的解释:何时在gitignore中使用前斜杠。

#7


10  

Try following in latest GIT version.

尝试使用最新的GIT版本。

*!*/!/path/to/your/dir/**

#8


6  

This .gitignore works for me:

gitignore对我很有用:

*//*!bin/

#9


5  

try this obscure git bug.

试试这个模糊的git bug。

!bin**/**

Hope it helps :)

希望它能帮助:)

Supporting docs git bug sample

支持文档git错误示例

PS: try it first before commenting.

在评论之前先试一试。