git 在被忽略的目录中跟踪文件

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

Track file inside ignored directory

gitgit-commit

提问by Gabriel

Some time ago I set up my .gitignorefile to nottrack a folder my_folderwith:

前段时间我设置我的.gitignore文件跟踪文件夹my_folder

my_folder/

Now I want to track only a given file inside said folder, named my_file.md. After making .gitignorelook like this:

现在我只想跟踪所述文件夹中的给定文件,名为my_file.md. 制作后.gitignore看起来像这样:

my_folder/
!my_folder/my_file.md

and checking:

并检查:

git status

the file does not appear as a change to be committed.

该文件不会显示为要提交的更改。

What am I doing wrong?

我究竟做错了什么?



Add

添加

I tried changing my .gitignorefile to:

我尝试将我的.gitignore文件更改为:

my_folder/*
!my_folder/my_file.md

as advised but the file is stillnot showing up as a change to commit after a git status. Do I need to reset something?

按照建议,但文件仍然没有显示为git status. 我需要重置一些东西吗?



Add 2

加2

Attempting to add the file with git add my_folder/my_file.mdreturns:

尝试添加带有git add my_folder/my_file.md返回的文件:

The following paths are ignored by one of your .gitignore files:
my_folder/my_file.md
Use -f if you really want to add them.
fatal: no files added

The command git check-ignore -v my_folder/my_file.mdgives:

该命令git check-ignore -v my_folder/my_file.md给出:

.gitignore:1:my_folder/*    my_folder/my_file.md

回答by VonC

To add to ".gitignoreexclude folder but include specific subfolder", one good way to debug those .gitignore file is to use git check-ignore(Git 1.8.4+):

要添加到“.gitignore排除文件夹但包含特定子文件夹”,调试那些 .gitignore 文件的一种好方法是使用git check-ignore(Git 1.8.4+):

git check-ignore -v my_folder/my_file.md

You would see it is still ignored because of the my_folder/rule.

你会看到它仍然因为my_folder/规则而被忽略。

That is because it is not possible to re-include a file if a parent directory of that file is excluded.(*)
(*: unless certain conditions are met in git 2.?+, see below)

这是因为如果排除了该文件的父目录,则不可能重新包含该文件。( *)
( *: 除非在 git 2.?+ 中满足某些条件,请参见下文)

That is why ignoring the fileswithin that folder (my_folder/*, instead of the folder itself) allows you to exclude one.

这就是为什么忽略该文件夹中的文件(my_folder/*,而不是文件夹本身)允许您排除一个。

Of course, you can force adding a file ignored (git add -f my_folder/my_file.md), but that is not the point of this answer.
The point is to explain why adding !my_folder/my_file.mdin .gitignoredoesn't work with git 2.6 or less.

当然,您可以强制添加忽略的文件 ( git add -f my_folder/my_file.md),但这不是本答案的重点。
重点是解释为什么!my_folder/my_file.md.gitignoregit 2.6 或更低版本中添加不起作用。



Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

请注意,对于 git 2.9.x/2.10(2016 年中期?),如果路径中没有通配符 re-included,则如果排除该文件的父目录,则可能会重新包含该文件。

Nguy?n Thái Ng?c Duy (pclouds)is trying to add this feature:

Nguy?n Thái Ng?c Duy ( pclouds)正在尝试添加此功能:

So here, with git 2.8+, this would work:

所以在这里,使用 git 2.8+,这将起作用:

/my_folder
!my_folder/my_file.md

回答by knittl

A file will not show up as "change to be committed" if it is not yet tracked (IOW was never added before). Simply add the file with git add, and Git will track the file only, but still ignore the folder.

如果尚未跟踪文件(以前从未添加 IOW),则该文件将不会显示为“要提交的更改”。只需添加文件git add,Git 将仅跟踪文件,但仍会忽略该文件夹。

git add -f ignored_folder/my_file

The -fflag is important, since git will refuse adding files from ignored paths.

-f标志很重要,因为 git 将拒绝从忽略的路径添加文件。

回答by BarzX ___

when I need track files like this, or only directory structures, I add something like this to my .gitignore file

当我需要跟踪这样的文件或仅需要目录结构时,我将这样的内容添加到我的 .gitignore 文件中

resources/**/*.*

which means, "ignore all files with extension inside the resources directory including subdirectories".

这意味着,“忽略包含子目录在内的资源目录中所有带有扩展名的文件”。

Then I put an "empty" file inside those directories I want to keep tracked.

然后我将一个“空”文件放在我想要跟踪的那些目录中。

Note that only extensionless files are going to be tracked, and that's why this works.

请注意,只会跟踪无扩展名的文件,这就是为什么这样做有效。

with the option listed above, you can now add this:

使用上面列出的选项,您现在可以添加以下内容:

!resources/my_needed_file.md

and that's another solution that not includes forced files, but is only an option if you are using files with extensions.

这是另一种不包含强制文件的解决方案,但仅当您使用带有扩展名的文件时才可以选择。

回答by Mihkel L.

My Solution:

我的解决方案:

resources/**/*.*

resources/**/*.*

then added afterthis line the negation:

然后此行之后添加否定:

!resources/my_needed_file.md

!resources/my_needed_file.md