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

发布于 2024-07-30 19:29:22 字数 260 浏览 4 评论 0原文

我想忽略存储库中的所有文件,除了 bin 子目录中出现的文件。 我尝试将以下内容添加到我的 .gitignore 中:

*
!bin/*

但是,这没有达到预期的效果:我在 bin/ 内部创建了一个新文件,但执行 git状态仍然显示没有任何可提交的内容(工作目录干净)

有什么建议么?

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/*

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).

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

旧瑾黎汐 2024-08-06 19:29:22

这会忽略根文件和 root 目录,然后取消忽略 root bin 目录:

/*
/*/
!/bin/

这样您就可以获得所有 bin 目录,包括子目录及其文件。

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.

诗笺 2024-08-06 19:29:22

您遇到的唯一问题是 bin 目录本身与 bin/* 模式不匹配,因此 git 甚至不会在 bin 中查找> 目录。

我想到了两种解决方案。

.gitignore

*
!/bin/
!bin/*

.gitignore

*
!/bin/

bin/.gitignore

!*

我更喜欢第二个解决方案,因为第一个解决方案不会停止忽略bin 目录位于不名为 bin 的子目录中。 这对于您的情况可能很重要,也可能无关紧要。

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.

There are two solutions that spring to mind.

.gitignore :

*
!/bin/
!bin/*

or

.gitignore :

*
!/bin/

bin/.gitignore :

!*

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

临风闻羌笛 2024-08-06 19:29:22

您必须排除前往目的地途中的所有内容,但必须包括目的地:

注意:这是一个排除文件(即 .gitignore),因此逻辑是相反的。 忽略除 tsp 目录之外的所有内容,忽略 tsp 目录中除 src 目录之外的所有内容...

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

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...

/*
!/tsp
/tsp/*
!/tsp/src
/tsp/src/*
!/tsp/src/*.h
!/tsp/src/*.cpp
!/tsp/src/data.txt
!/.gitignore
我喜欢麦丽素 2024-08-06 19:29:22

这里如何忽略某个目录

结构中除一个目录“MY_SUPER_DUPER_TEMPLATE_directory”之外的所有内容:/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory

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

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

Structure: /bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory

/*
!/bitrix
/bitrix/*
!/bitrix/templates
/bitrix/templates/*
!/bitrix/templates/MY_SUPER_DUPER_TEMPLATE_directory
*.DS_Store
*.gitignore
盗心人 2024-08-06 19:29:22

从官方 git doc 中,其中一个示例表示:

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

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

有关前导斜杠的说明: 何时在 gitignore 中使用前导斜杠

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

Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within 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.

不打扰别人 2024-08-06 19:29:22

在最新的 GIT 版本中尝试以下操作。

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

Try following in latest GIT version.

*
!*/
!/path/to/your/dir/**
日久见人心 2024-08-06 19:29:22

这是我的解决方案。 在我的场景中,我有一个包含子文件夹的文件夹,如下所示:

  • 数据库(主文件夹)
    • conf(子文件夹)
    • 驱动程序(子文件夹)
    • jar(子文件夹)
    • sql(子文件夹)

并且从database文件夹中我只想推送sql文件夹,所以我的.gitignore 文件如下:

database/*
!database/sql/

第一行只是说忽略 database 文件夹的所有子文件夹,第二行的意思是因为您忽略 database 文件夹的所有子文件夹>database 文件夹排除(不要忽略)sql 子文件夹

Here is my solution. In my scenario I had a folder with subfolders as follow:

  • database (main folder)
    • conf (subfolder)
    • drivers (subfolder)
    • jars (subfolder)
    • sql (subfolder)

And from the database folder I wanted to push the sql folder only so my .gitignore file was as follow:

database/*
!database/sql/

Where the first line simply say ignore all subfolder(s) of the database folder and the second line meaning since you are ignoring all subfolder(s) of the database folder exclude(don't ignore) the sql subfolder

居里长安 2024-08-06 19:29:22

对我来说,事实证明我不应该忽略父文件夹,而是使用 /* 忽略其中的文件和文件

-- **/node_modules

++ **/node_modules/*

++ !node_modules/@my-package/

To me it turns out I should not ignore the parent folder but files and folders in it using /*

-- **/node_modules

++ **/node_modules/*

then

++ !node_modules/@my-package/
友欢 2024-08-06 19:29:22

我正在开发一个 WordPress 插件,这是我的 .gitignore 仅跟踪插件文件

/*
!/.gitignore
!/wp-content/
/wp-content/*
!/wp-content/plugins/
/wp-content/plugins/*
!/wp-content/plugins/my-plugin/

I'm developing a wordpress plugin, Here is my .gitignore to only track the plugin files

/*
!/.gitignore
!/wp-content/
/wp-content/*
!/wp-content/plugins/
/wp-content/plugins/*
!/wp-content/plugins/my-plugin/
没有你我更好 2024-08-06 19:29:22

这个 .gitignore 对我有用:

*/
/*
!bin/

This .gitignore works for me:

*/
/*
!bin/
拥抱没勇气 2024-08-06 19:29:22

试试这个不起眼的 git bug。

!bin**/**

希望它有帮助:)

支持文档 git bug 示例

PS:在发表评论之前先尝试一下。

try this obscure git bug.

!bin**/**

Hope it helps :)

Supporting docs git bug sample

PS: try it first before commenting.

絕版丫頭 2024-08-06 19:29:22

我最近不得不这样做:

*
!sub-dir/
!sub-dir/*

我不确定为什么我需要两行来排除一个文件夹,但这对我和 macos 来说都是有效的。

I had to do this recently:

*
!sub-dir/
!sub-dir/*

I'm not sure why I need two lines for to exclude one folder, but this is what worked for me and macos.

离去的眼神 2024-08-06 19:29:22

我认为更好的方法是通过以斜杠开头的模式将每个模式锚定到顶部 git 目录:

/*
!/public_html
!/.gitignore

它不会忽略所有文件,它只会忽略顶级文件,而不是您不想忽略的目录中的文件。

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

/*
!/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.

垂暮老矣 2024-08-06 19:29:22

聚会迟到了,但没有一个答案对我来说“开箱即用”。 这个的作用是:

* 
!bin
!bin/**
  • 第一行忽略所有内容
  • 第二行包含“bin”目录本身
  • 最后一行包含“bin”目录的全部内容,无论它是如何嵌套的(注意双星号),

Late to the party, but none of the answers worked for me "out of the box". This one does:

* 
!bin
!bin/**
  • the first line ignores everything
  • the second line includes back "bin" directory itself
  • the last line includes back entire content of the "bin" directory no matter how nested it is (note double asterisk),
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文