用于显示 .gitignore 忽略哪些特定文件的 Git 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/466764/
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
Git command to show which specific files are ignored by .gitignore
提问by Andrew Burns
I am getting my feet wet with Git and have the following issue:
我被 Git 弄湿了,有以下问题:
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.
我的供应商分支中有代码(当前是 MEF),我将在那里编译,然后将引用移到/src/refs
项目从中提取它们的位置。
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.
我的问题是我有我的.gitignore
设置忽略*.dll
和*.pdb
。我可以做一个git add -f bar.dll
强制添加被忽略的文件,这没问题,问题是我无法弄清楚列出哪些文件存在被忽略。
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?
我已经阅读了手册页git ls-files
,但无法使其工作。在我看来,git ls-files --exclude-standard -i
应该做我想做的。我错过了什么?
回答by VonC
Notes:
笔记:
- xiaobai's answeris simpler (git1.7.6+):
git status --ignored
(as detailed in "Is there a way to tell git-status to ignore the effects of.gitignore
files?") - MattDiPasquale's answer(to be upvoted)
git clean -ndX
works on older gits, displaying a previewof what ignored files could be removed (without removing anything)
- 小白的回答更简单(git1.7.6+):(
详见“有没有办法告诉git-status忽略文件的影响?”)
git status --ignored
.gitignore
- MattDiPasquale的答案(有待投票)
git clean -ndX
适用于较旧的 git,显示可以删除哪些忽略文件的预览(不删除任何内容)
Also interesting (mentioned in qwertymk's answer), you can also use the git check-ignore -v
command, at least on Unix (doesn't workin a CMD Windowssession)
同样有趣(在qwertymk的回答中提到),您也可以使用该git check-ignore -v
命令,至少在 Unix 上(在 CMD Windows会话中不起作用)
git check-ignore *
git check-ignore -v *
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+:
第二个显示.gitignore
git 存储库中忽略文件的实际规则。
在 Unix 上,使用“什么递归地扩展到当前目录中的所有文件?”和一个 bash4+:
git check-ignore **/*
(or a find -exec
command)
(或find -exec
命令)
Note: https://stackoverflow.com/users/351947/Rafi B.suggests in the commentsto avoid the (risky) globstar:
注:https://stackoverflow.com/users/351947/Rafi B.建议在评论以避免(风险)globstar:
git check-ignore -v $(find . -type f -print)
Make sure to exclude the files from the .git/
subfolder though.
不过,请确保从.git/
子文件夹中排除文件。
Original answer 42009)
原答案 42009)
git ls-files -i
should work, except its source codeindicates:
应该可以工作,除了它的源代码表明:
if (show_ignored && !exc_given) {
fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
argv[0]);
exc_given
?
exc_given
?
It turns out it need one more parameter after the -i
to actually list anything:
事实证明,在-i
实际列出任何内容之后还需要一个参数:
Try:
尝试:
git ls-files -i --exclude-from=[Path_To_Your_Global].gitignore
(but that would only list your cached(non-ignored) object, with a filter, so that is not quite what you want)
(但这只会列出您的缓存(非忽略)对象,并带有过滤器,因此这不是您想要的)
Example:
例子:
$ cat .git/ignore
# ignore objects and archives, anywhere in the tree.
*.[oa]
$ cat Documentation/.gitignore
# ignore generated html files,
*.html
# except foo.html which is maintained by hand
!foo.html
$ git ls-files --ignored \
--exclude='Documentation/*.[0-9]' \
--exclude-from=.git/ignore \
--exclude-per-directory=.gitignore
Actually, in my 'gitignore' file (called 'exclude'), I find a command line that could help you:
实际上,在我的“gitignore”文件(称为“exclude”)中,我找到了一个可以帮助您的命令行:
F:\prog\git\test\.git\info>type exclude
# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
So....
所以....
git ls-files --others --ignored --exclude-from=.git/info/exclude
git ls-files -o -i --exclude-from=.git/info/exclude
git ls-files --others --ignored --exclude-standard
git ls-files -o -i --exclude-standard
should do the trick.
应该做的伎俩。
As mentioned in the ls-files man page, --others
is the important part, in order to show you non-cached, non-committed, normally-ignored files.
正如ls-files 手册页中提到的,--others
是重要的部分,以便向您展示未缓存、未提交、通常被忽略的文件。
--exclude_standard
is not just a shortcut, but a way to include allstandard "ignored patterns" settings.
--exclude_standard
不仅仅是一种快捷方式,而是一种包含所有标准“忽略模式”设置的方法。
exclude-standard
Add the standard git exclusions:.git/info/exclude
,.gitignore
in each directory, and theuser's global exclusion file
.
exclude-standard
添加标准的git排除:.git/info/exclude
,.gitignore
在每个目录和user's global exclusion file
。
回答by Penghe Geng
There is a much simpler way to do it (git 1.7.6+):
有一种更简单的方法来做到这一点(git 1.7.6+):
git status --ignored
See Is there a way to tell git-status to ignore the effects of .gitignore files?
回答by ma11hew28
Another option that's pretty clean (No pun intended.):
另一个非常干净的选项(没有双关语。):
git clean -ndX
Explanation:
解释:
$ git help clean
git-clean - Remove untracked files from the working tree
-n, --dry-run - Don't actually remove anything, just show what would be done.
-d - Remove untracked directories in addition to untracked files.
-X - Remove only files ignored by Git.
Note: This solution will not show ignored files that have already been removed.
注意:此解决方案不会显示已被删除的忽略文件。
回答by riyad
While generally correct your solution does not work in all circumstances. Assume a repo dir like this:
虽然通常正确,但您的解决方案并非在所有情况下都有效。假设一个 repo 目录是这样的:
# ls **/*
doc/index.html README.txt tmp/dir0/file0 tmp/file1 tmp/file2
doc:
index.html
tmp:
dir0 file1 file2
tmp/dir0:
file0
and a .gitignore like this:
和这样的 .gitignore:
# cat .gitignore
doc
tmp/*
This ignores the doc
directory and all files below tmp
.
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:
这将忽略doc
目录和下面的所有文件tmp
。Git 按预期工作,但列出被忽略文件的给定命令没有。让我们来看看 git 怎么说:
# git ls-files --others --ignored --exclude-standard
tmp/file1
tmp/file2
Notice that doc
is missing from the listing.
You can get it with:
请注意,doc
列表中缺少它。您可以通过以下方式获取:
# git ls-files --others --ignored --exclude-standard --directory
doc/
Notice the additional --directory
option.
注意附加--directory
选项。
From my knowledge there is no onecommand to list all ignored files at once.
But I don't know why tmp/dir0
does not show up at all.
据我所知,没有一个命令可以一次列出所有被忽略的文件。但我不知道为什么tmp/dir0
根本不显示。
回答by qwertymk
Git now has this functionality built in
Git 现在内置了这个功能
git check-ignore *
Of course you can change the glob to something like **/*.dll
in your case
当然,您可以将 glob 更改为类似于**/*.dll
您的情况
回答by iconoclast
It should be sufficient to use
使用应该足够了
git ls-files --others -i --exclude-standard
as that covers everything covered by
因为这涵盖了所涵盖的所有内容
git ls-files --others -i --exclude-from=.git/info/exclude
therefore the latter is redundant.
因此后者是多余的。
您可以通过向
~/.gitconfig
~/.gitconfig
文件添加别名来简化此操作:git config --global alias.ignored "ls-files --others -i --exclude-standard"
Now you can just type git ignored
to see the list. Much easier to remember, and faster to type.
现在您只需键入git ignored
即可查看列表。更容易记住,打字也更快。
If you prefer the more succinct display of Jason Geng's solution, you can add an alias for that like this:
如果您更喜欢更简洁地展示 Jason Geng 的解决方案,您可以为它添加一个别名,如下所示:
git config --global alias.ignored "status --ignored -s"
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.
然而,更详细的输出对于解决 .gitignore 文件的问题更有用,因为它列出了每个被忽略的棉花采摘文件。您通常会通过管道grep
传输结果,以查看您希望被忽略的文件是否在其中,或者您不想被忽略的文件是否在其中。
git ignored | grep some-file-that-isnt-being-ignored-properly
Then, when you just want to see a short display, it's easy enough to remember and type
然后,当您只想看一个简短的显示时,很容易记住并输入
git status --ignored
(The -s
can normally be left off.)
(-s
通常可以关闭。)
回答by MarkDBlackwell
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
):
以下是在工作树中打印完整文件列表的方法,这些文件匹配位于 Git 的多个 gitignore 源中任何位置的模式(如果您使用的是 GNU find
):
$ cd {your project directory}
$ find . -path ./.git -prune -o -print \
| git check-ignore --no-index --stdin --verbose
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.
它还标识了特定的 gitignore 源代码行。
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.
Git 继续跟踪匹配 gitignore 模式的某些文件中的更改,仅仅是因为这些文件已经添加。有用的是,上面的命令也显示这些文件。
Negative gitignore patterns are also matched. However, these are easily distinguishable in the listing, because they begin with !
.
负 gitignore 模式也匹配。但是,这些在列表中很容易区分,因为它们以!
.
If you're using Windows, Git Bashincludes GNU find
(as revealed by find --version
).
如果您使用的是 Windows,则Git Bash包含 GNU find
(如 所示find --version
)。
If the list is long (and you have rev
), you can display them by extension (somewhat), too:
如果列表很长(并且您有rev
),您也可以按扩展名(有点)显示它们:
$ cd {your project directory}
$ find . -path ./.git -prune -o -print \
| git check-ignore --no-index --stdin --verbose \
| rev | sort | rev
For more details, see man find
, man git-check-ignore
, man rev
, and man sort
.
欲了解更多详情,请参阅man find
,man git-check-ignore
,man rev
,和man 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 extremelystable (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.
整个方法的重点是 Git(软件)正在快速变化并且非常复杂。相比之下,GNU的find
是非常稳定(至少,在这里使用其功能)。因此,任何希望通过展示他们对 Git 的深入了解来提高竞争力的人都会以不同的方式回答这个问题。
What's the best answer? Thisanswer 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 longtime.
最好的答案是什么?这个答案特意减少了对 Git 知识的依赖,通过模块化(信息隔离)来实现稳定性和简单性的目标,并且旨在持续很长时间。
回答by peterh - Reinstate Monica
(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 a git commit --amend
.
请注意,在您的工作树中git check-ignore
使用已提交的.gitignore
而不是已提交的!为了在不污染您的 git 历史记录的情况下使用它,您可以自由地尝试编辑它,然后使用git commit --amend
.
This problem happens mainly if you need a workaround of the problem, that git doesn't follow directories. Enter in the .gitignore
:
此问题主要发生在您需要解决该问题的方法时,即 git 不遵循目录。输入.gitignore
:
dirtokeep/**
!dirtokeep/.keep
.keep
should be a zero-length file in dirtokeep
.
.keep
应该是dirtokeep
.
The result will be that everythingin dirtokeep
will be ignored, exceptdirtokeep/.keep
, which will result that also the dirtokeep
directory will be constructed on clone/checkout.
其结果将是一切的dirtokeep
将被忽略,除外dirtokeep/.keep
,这将导致该还的dirtokeep
目录将在克隆/结帐构造。
回答by pikknz
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.:
假设有几个忽略目录,为什么不使用“git status node/logs/”来告诉你要添加哪些文件?在目录中,我有一个不属于状态输出的文本文件,例如:
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
(use "git add ..." to include in what will be committed)
在分支 master
您的分支是最新的“origin/master”。
未跟踪的文件:(
使用“git add ...”包含在将提交的内容中)
node/logs/.gitignore
.gitignore is:
.gitignore 是:
*
!.gitignore
*
!.gitignore