Git 命令显示 .gitignore 忽略了哪些特定文件
我正在接触 Git,并遇到以下问题:
我的项目源树:
/
|
+--src/
+----refs/
+----...
|
+--vendor/
+----...
我的供应商分支中有代码(当前为 MEF),我将在那里进行编译,然后将引用移至 /src/refs 这是项目从中获取它们的地方。
我的问题是我将 .gitignore
设置为忽略 *.dll
和 *.pdb
。 我可以执行 git add -f bar.dll 来强制添加被忽略的文件,这是可以的,问题是我无法弄清楚列出哪些文件存在被忽略。
我想列出被忽略的文件,以确保我不会忘记添加它们。
我已阅读 git ls-files 上的手册页,但无法使其工作。 在我看来, git ls-files --exclude-standard -i 应该做我想做的事。 我缺少什么?
I am getting my feet wet with Git and have the following issue:
My project source tree:
/
|
+--src/
+----refs/
+----...
|
+--vendor/
+----...
I have code (currently MEF) in my vendor branch that I will compile there and then move the references into /src/refs
which is where the project picks them up from.
My issue is that I have my .gitignore
set to ignore *.dll
and *.pdb
. I can do a git add -f bar.dll
to force the addition of the ignored file which is ok, the problem is I can not figure out to list what files exist that are ignored.
I want to list the ignored files to make sure that I don't forget to add them.
I have read the man page on git ls-files
and can not make it work. It seems to me that git ls-files --exclude-standard -i
should do what I want. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
注:
git status --ignored
(详见“有没有办法告诉 git-status 忽略
.gitignore
文件的影响?”)也很有趣(在 qwertymk的答案),您也可以使用
git check-ignore -v
命令,至少在 Unix 上(在 CMD Windows 会话中不起作用,但在 Windows CMD git bash 会话中起作用)第二个显示
.gitignore
的实际规则使文件在 git 存储库中被忽略。在 Unix 上,使用“什么递归地扩展到当前目录中的所有文件?" 和 bash4+:
(或
find -exec
命令)注意:https:// /stackoverflow.com/users/351947/Rafi B. 建议 在评论中避免(有风险的)globstar:
确保从
.git 中排除文件/
子文件夹。CervEd 在 注释,以避免
.git/
:原始答案(2009)
应该有效,除了其源代码表示:
exc_given?
事实证明,在
-i
之后还需要一个参数才能实际列出任何内容。尝试:(
但这只会列出您的缓存(非忽略)对象,并带有过滤器,因此这并不完全是您想要的)
示例:
实际上,在我的“gitignore”文件中(称为“排除'),我找到一个可以帮助您的命令行:
所以......
应该可以解决问题。
(感谢honzajde指出在注释中
git ls-files -o -i --exclude -from...
不不包含缓存文件:仅git ls-files -i --exclude-from...
(不包含< /em>-o
) 确实如此。)正如 ls-files 手册页,
--others
是重要部分,向您展示非缓存、非提交、通常被忽略的文件。--exclude_standard
不仅仅是一个快捷方式,而且是一种包含所有标准“忽略模式”设置的方法。Git 2.44(2024 年第 1 季度),第 13 批,澄清了文档,其中
- -exclude-per-directory
选项被标记为已弃用,这让读者误以为将来可能有计划删除它,但这不是我们的意图。请参阅 提交 0009542(2024 年 1 月 24 日),作者:Junio C Hamano (
gitster
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 bbc8c05,2024 年 2 月 2 日)git ls-files
现在包含在其 手册页:git ls-files
现在包含在其 手册页:Notes:
git status --ignored
(as detailed in "Is there a way to tell git-status to ignore the effects of
.gitignore
files?")Also interesting (mentioned in qwertymk's answer), you can also use the
git check-ignore -v
command, at least on Unix (doesn't work in a CMD Windows session, but works in a Windows CMD git bash session)The second one displays the actual rule of the
.gitignore
which makes a file to be ignored in your git repo.On Unix, using "What expands to all files in current directory recursively?" and a bash4+:
(or a
find -exec
command)Note: https://stackoverflow.com/users/351947/Rafi B. suggests in the comments to avoid the (risky) globstar:
Make sure to exclude the files from the
.git/
subfolder though.CervEd suggests in the comments, to avoid
.git/
:Original answer (2009)
should work, except its source code indicates:
exc_given
?It turns out it needs one more parameter after the
-i
to actually list anything.Try:
(but that would only list your cached (non-ignored) object, with a filter, so that is not quite what you want)
Example:
Actually, in my 'gitignore' file (called 'exclude'), I find a command line that could help you:
So....
Should do the trick.
(Thanks to honzajde pointing out in the comments that
git ls-files -o -i --exclude-from...
does not include cached files: onlygit ls-files -i --exclude-from...
(without-o
) does.)As mentioned in the ls-files man page,
--others
is the important part, to show you non-cached, non-committed, normally ignored files.--exclude_standard
is not just a shortcut, but a way to include all standard "ignored patterns" settings.Git 2.44 (Q1 2024), batch 13, clarifies the documentation where the
--exclude-per-directory
option was marked as deprecated, which confused readers into thinking there may be a plan to remove it in the future, which was not our intention.See commit 0009542 (24 Jan 2024) by Junio C Hamano (
gitster
).(Merged by Junio C Hamano --
gitster
-- in commit bbc8c05, 02 Feb 2024)git ls-files
now includes in its man page:git ls-files
now includes in its man page:有一个更简单的方法来做到这一点(git 1.7.6+):
请参阅 有没有办法告诉 git-status 忽略.gitignore 文件的影响?
There is a much simpler way to do it (git 1.7.6+):
See Is there a way to tell git-status to ignore the effects of .gitignore files?
另一个非常干净的选项(没有双关语。):
说明:
注意:此解决方案不会显示已被删除的忽略文件。
Another option that's pretty clean (No pun intended.):
Explanation:
Note: This solution will not show ignored files that have already been removed.
虽然通常是正确的,但您的解决方案并不在所有情况下都有效。
假设 repo 目录像这样:
和 .gitignore 像这样:
这会忽略
doc
目录和tmp
下面的所有文件。Git 按预期工作,但列出被忽略文件的给定命令却没有。
让我们看看 git 怎么说:
请注意,列表中缺少
doc
。您可以通过以下方式获取它:
注意附加的
--directory
选项。据我所知,没有一个命令可以一次性列出所有被忽略的文件。
但我不知道为什么
tmp/dir0
根本不显示。While generally correct your solution does not work in all circumstances.
Assume a repo dir like this:
and a .gitignore like this:
This ignores the
doc
directory and all files belowtmp
.Git works as expected, but the given command for listing the ignored files does not.
Lets have a look at what git has to say:
Notice that
doc
is missing from the listing.You can get it with:
Notice the additional
--directory
option.From my knowledge there is no one command to list all ignored files at once.
But I don't know why
tmp/dir0
does not show up at all.Git 现在内置了此功能
当然,您可以在您的情况下将 glob 更改为
**/*.dll
Git 参考
Git now has this functionality built in
Of course you can change the glob to something like
**/*.dll
in your caseGit Reference
以下是如何打印工作树中与 Git 的多个 gitignore 源中任意位置的模式相匹配的文件的完整列表(如果您使用的是 GNU
find
):它将检查当前分支中的所有文件存储库的(除非您已在本地删除它们)。
它还标识了特定的 gitignore 源代码行。
Git 继续跟踪某些与 gitignore 模式匹配的文件中的更改,只是因为这些文件已被添加。 有用的是,上面的命令也显示这些文件。
负的 gitignore 模式也被匹配。 但是,这些在列表中很容易区分,因为它们以
!
开头。如果您使用的是 Windows,Git Bash 包含 GNU
find
(正如find --version
所揭示的那样)。如果列表很长(并且您有
rev
),您也可以通过扩展名(某种程度上)显示它们:有关更多详细信息,请参阅
man find
、man git-check-ignore、
man rev
和man sort
。整个方法的要点是 Git(软件)正在快速变化并且非常复杂。 相比之下,GNU 的
find
极其 稳定(至少在此处使用的功能方面是如此)。 因此,任何希望通过展示自己对 Git 的深入了解来提高竞争力的人都会以不同的方式回答这个问题。最好的答案是什么? 这个答案故意最小化对 Git 知识的依赖,通过模块化(信息隔离)来实现稳定和简单的目标,并且被设计为可以持续很长时间。
Here's how to print the complete list of files in the working tree which match patterns located anywhere in Git's multiple gitignore sources (if you're using GNU
find
):It will check all the files in the current branch of the repository (unless you've deleted them locally).
And it identifies the particular gitignore source lines, as well.
Git continues to track changes in some files which match gitignore patterns, simply because those files were added already. Usefully, the above command displays those files, too.
Negative gitignore patterns are also matched. However, these are easily distinguishable in the listing, because they begin with
!
.If you're using Windows, Git Bash includes GNU
find
(as revealed byfind --version
).If the list is long (and you have
rev
), you can display them by extension (somewhat), too:For more details, see
man find
,man git-check-ignore
,man rev
, andman sort
.The point of this whole approach is that Git (the software) is changing rapidly and is highly complex. By contrast, GNU's
find
is extremely stable (at least, in its features used here). So, anyone who desires to be competitive by displaying their in-depth knowledge of Git will answer the question in a different way.What's the best answer? This answer deliberately minimizes its reliance on Git knowledge, toward achieving the goal of stability and simplicity through modularity (information isolation), and is designed to last a long time.
使用它应该足够了,
因为它涵盖了所涵盖的所有内容,
因此后者是多余的。
You can make this easier by adding an alias to your
~/.gitconfig
file:现在您只需输入
gitignoreed
即可查看列表。 更容易记住,打字也更快。如果您更喜欢 Jason Geng 的解决方案的更简洁显示,您可以为其添加一个别名,如下所示:
然而,更详细的输出对于解决 .gitignore 文件的问题更有用,因为它列出了每个单独的棉花采摘文件被忽略。 您通常会通过
grep
传送结果,以查看其中是否存在您希望忽略的文件,或者是否存在您不想忽略的文件。然后,当您只想看到简短的显示时,很容易记住和键入
(通常可以省略
-s
。)It should be sufficient to use
as that covers everything covered by
therefore the latter is redundant.
You can make this easier by adding an alias to your
~/.gitconfig
file:Now you can just type
git ignored
to see the list. Much easier to remember, and faster to type.If you prefer the more succinct display of Jason Geng's solution, you can add an alias for that like this:
However the more verbose output is more useful for troubleshooting problems with your .gitignore files, as it lists every single cotton-pickin' file that is ignored. You would normally pipe the results through
grep
to see if a file you expect to be ignored is in there, or if a file you don't want to be ignore is in there.Then, when you just want to see a short display, it's easy enough to remember and type
(The
-s
can normally be left off.)如果您只需要有效的文件列表忽略(无论它们如何被忽略),并且没有任何额外的通知和日志。
一旦创建,任何地方(在Git-bash中) ) run:
通过执行以下命令创建它:
注意:
macOS
和Windows
平台上运行!cd
进入某个目录,仅列出该目录(或其子目录)中忽略的文件。.git
目录,无论您cd
进入哪个目录)。另一个有用的东西是人类可读性,就像如果忽略整个目录,例如
build
或node_modules
,这将仅记录目录名称。而 git ls-files --others -i --exclude-standard 命令会记录每个文件(适用于 xargs )。
If you just need a valid list of files ignored (no matter how they got ignored), and without any extra notice and logs.
Once created, anywhere (in Git-bash) run:
Create it by executing:
Notes:
macOS
andWindows
platforms!cd
into a directory, lists only files ignored from that dir (or its sub-dirs)..git
dir, no matter what dir youcd
into).Another useful thing is Human-readability, like if an entire directory is ignored, something like
build
ornode_modules
, this will log dir-name only.While
git ls-files --others -i --exclude-standard
command logs each and every file (good forxargs
).我只想
列出被忽略的文件
I would just do
and to only list the ignored files
您可以使用 fd。
运行的命令是:
请注意,如果没有
.git
目录,只有.gitignore
文件(例如裸存储库),您必须具体说明位置.gitignore 文件是。更具体地说,请使用
--no-ignore-vcs
而不是-I
有关更多详细信息,请查看 此处。
You can use fd.
The command to run is:
Please note that in case there is no
.git
directory and only a.gitignore
file (ex. bare repo), you have to specifically tell where the .gitignore file is.To be more specific, use
--no-ignore-vcs
instead of-I
For more details please check here.
假设有一些忽略目录,为什么不使用“git status node/logs/”来告诉您要添加哪些文件? 在目录中,我有一个不属于状态输出的文本文件,例如:
Onbranchmaster
您的分支已更新为“origin/master”。
未跟踪的文件:
(使用“git add ...”包含在将提交的内容中)
.gitignore 是:
*
!.gitignore
Assuming there are a few ignore directories, why not use "git status node/logs/" which will tell you what files are to be added? In the directory I have a text file that is not part of status output, e.g.:
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add ..." to include in what will be committed)
.gitignore is:
*
!.gitignore
(扩展其他答案)
注意,
git check-ignore
使用提交的.gitignore
而不是工作树中的! 要在不污染 git 历史记录的情况下使用它,您可以自由地尝试编辑它,然后使用 git commit --amend 进行提交。这个问题主要发生在您需要解决问题的方法时,即 git 不跟踪目录。 在
.gitignore
中输入:.keep
应该是dirtokeep
中的零长度文件。结果将是
dirtokeep
中的所有内容都将被忽略,除了dirtokeep/.keep
,这将导致dirtokeep
目录也将在克隆/签出时构建。(extending the other answers)
Note,
git check-ignore
uses the committed.gitignore
and not the one in your working tree! To play with it without polluting your git history, you might freely try to edit it, and then commit with agit commit --amend
.This problem happens mainly if you need a workaround of the problem, that git doesn't follow directories. Enter in the
.gitignore
:.keep
should be a zero-length file indirtokeep
.The result will be that everything in
dirtokeep
will be ignored, exceptdirtokeep/.keep
, which will result that also thedirtokeep
directory will be constructed on clone/checkout.