Git命令显示.gitignore忽略哪些特定文件

And*_*rns 596 git ignore

我正在git上弄湿我的脚并遇到以下问题:

我的项目源码树:

/
|
+--src/
+----refs/
+----...
|
+--vendor/
+----...
Run Code Online (Sandbox Code Playgroud)

我在我的供应商分支中有代码(目前是MEF),我将在那里编译,然后将引用移动到/src/refs项目从中获取的位置.

我的问题是我有我的.gitignore设置忽略*.dll*.pdb.我可以做一个git add -f bar.dll强制添加被忽略的文件,这是好的,问题是我无法弄清楚列出哪些文件被忽略.

我想列出被忽略的文件,以确保我不会忘记添加它们.

我已经阅读了手册页git ls-files,但无法使其正常工作.在我看来,git ls-files --exclude-standard -i应该做我想要的.我错过了什么?

Von*_*onC 606

笔记:


同样有趣(在qwertymk回答中提到),您也可以使用该git check-ignore -v命令,至少在Unix上(在CMD Windows会话中不起作用)

git check-ignore *
git check-ignore -v *
Run Code Online (Sandbox Code Playgroud)

第二个显示实际的规则,.gitignore它使git仓库中的文件被忽略.
在Unix上,使用" 什么扩展到当前目录中的所有文件递归? "和bash4 +:

git check-ignore **/*
Run Code Online (Sandbox Code Playgroud)

(或find -exec命令)


原始答案42009)

git ls-files -i
Run Code Online (Sandbox Code Playgroud)

应该工作,除了它的源代码表明:

if (show_ignored && !exc_given) {
                fprintf(stderr, "%s: --ignored needs some exclude pattern\n",
                        argv[0]);
Run Code Online (Sandbox Code Playgroud)

exc_given

事实证明,在-i实际列出任何内容后需要一个参数:

尝试:

git ls-files -i --exclude-from=[Path_To_Your_Global].gitignore
Run Code Online (Sandbox Code Playgroud)

(但这只列出你的缓存(非忽略)对象,带有过滤器,所以这不是你想要的)


例:

$ 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
Run Code Online (Sandbox Code Playgroud)

实际上,在我的'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]
# *~
Run Code Online (Sandbox Code Playgroud)

所以....

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
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩.

正如ls-files手册页中所提到的,--others是重要的部分,以便向您展示非缓存的,未提交的,通常被忽略的文件.

--exclude_standard不仅仅是一种捷径,而是一种包含所有标准"忽略模式"设置的方法.

exclude-standard
添加标准的git排除:.git/info/exclude,.gitignore在每个目录和user's global exclusion file.

  • 由于 Git v2.13.2 发布:`git status --ignored` 似乎也显示未跟踪的文件:https://github.com/git/git/blob/master/Documentation/RelNotes/2.13.2.txt#L26-L27 (3认同)
  • @CervEd 感谢您的反馈,完成了拉菲的建议。我已将您的命令包含在答案中以获得更多可见性。 (2认同)

Pen*_*eng 430

有一种更简单的方法(git 1.7.6+):

__CODE__

有没有办法告诉git的状态忽视的.gitignore文件的影响?

  • 这肯定比接受的答案要好得多.它也比`git clean -ndX`解决方案更安全,因为当一个错误地忘记标志的罕见情况将对存储库产生不可逆转的影响,因为未删除的文件被删除.所以这很危险.相反,`git status --ignored`总是安全的,即使错误输入也是很自然的. (11认同)
  • 或者 `git status --ignored --untracked-files=all` 显示被忽略目录中的单个文件。 (5认同)
  • 我试过这个页面上的每个解决方案.这是最好的一个.它显示文件和目录.最初询问此问题时,此功能可能无法恢复.(顺便说一句,在您至少进行了更改之前,所有解决方案都无法使用全新的"git init"正常工作.) (4认同)
  • 你使用的是什么版本的git?我的(1.7.0.4)说'错误:未知选项'忽略'.即使在链接帖子中建议添加`-s`也行不通. (3认同)
  • 我的版本是1.7.6.另一个版本1.7.5.1是需要`-s`的版本.您可以尝试`git status -h`来查看是否支持`--ignored` (3认同)

ma1*_*w28 397

另一个非常干净的选择(没有双关语.):

git clean -ndX
Run Code Online (Sandbox Code Playgroud)

说明:

$ 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.
Run Code Online (Sandbox Code Playgroud)

注意:此解决方案不会显示已删除的已忽略文件.

  • 一件小事 - 首先键入`n`可能是个好主意,不太可能意外删除那个方式; `git clean -ndX` (3认同)
  • 我正在使用git版本1.7.0.4,两个命令('git ls-files -o -i --exclude-standard','git clean -dXn')不等效.第一个显示4个文件,第二个显示只有两个.(.gitignore~,index.php~,sql/create_users.sql~,www/index.php~)(会删除.gitignore~,会删除index.php~).我在这里想点什么吗? (2认同)

小智 39

虽然通常正确,但您的解决方案并不适用于所有情况.假设一个repo dir是这样的:

# ls **/*                                                                                                       
doc/index.html  README.txt  tmp/dir0/file0  tmp/file1  tmp/file2

doc:
index.html

tmp:
dir0  file1  file2

tmp/dir0:
file0
Run Code Online (Sandbox Code Playgroud)

和.gitignore这样:

# cat .gitignore
doc
tmp/*
Run Code Online (Sandbox Code Playgroud)

这会忽略doc下面的目录和所有文件tmp.Git按预期工作,但列出被忽略文件的给定命令却没有.让我们来看看git有什么话要说:

# git ls-files --others --ignored --exclude-standard                                                            
tmp/file1
tmp/file2
Run Code Online (Sandbox Code Playgroud)

请注意,doc列表中缺少该内容.你可以得到它:

# git ls-files --others --ignored --exclude-standard --directory                                                
doc/
Run Code Online (Sandbox Code Playgroud)

请注意附加--directory选项.

据我所知,没有一个命令可以一次列出所有被忽略的文件.但我不知道为什么tmp/dir0不出现.

  • 这让我得到了我想要的东西,而其他人却没有(对于我的特殊情况)......谢谢!必须运行两个命令是令人沮丧的,但是对于一个被忽略的目录, - 目录选项至少找到了我,并且我可以将它传递给find命令来查找文件.谢谢! (2认同)

qwe*_*ymk 17

Git现在内置了这个功能

git check-ignore *
Run Code Online (Sandbox Code Playgroud)

当然,您可以将glob更改为类似于**/*.dll您的情况

Git参考

  • `git check-ignore**/*`包含子目录中的文件 (7认同)

ico*_*ast 13

它应该足够使用

git ls-files --others -i --exclude-standard
Run Code Online (Sandbox Code Playgroud)

因为它涵盖了所涵盖的一切

git ls-files --others -i --exclude-from=.git/info/exclude
Run Code Online (Sandbox Code Playgroud)

因此后者是多余的.


您可以通过向~/.gitconfig文件添加别名来简化此操作:

git config --global alias.ignored "ls-files --others -i --exclude-standard"
Run Code Online (Sandbox Code Playgroud)

现在您只需输入git ignored即可查看列表.更容易记住,更快打字.

如果您更喜欢Jason Geng解决方案的更简洁的显示,您可以为此添加别名:

git config --global alias.ignored "status --ignored -s"
Run Code Online (Sandbox Code Playgroud)

但是,更详细的输出对于解决.gitignore文件的问题更有用,因为它列出了被忽略的每个棉花挑选文件.您通常会通过管道输出结果grep来查看您希望被忽略的文件是否在那里,或者是否存在您不想忽略的文件.

git ignored | grep some-file-that-isnt-being-ignored-properly
Run Code Online (Sandbox Code Playgroud)

然后,当您只想看一个简短的显示时,它很容易记住并输入

git status --ignored
Run Code Online (Sandbox Code Playgroud)

(-s通常可以不用了.)


Mar*_*ell 11

以下是如何在工作树中打印完整的文件列表,这些文件匹配位于Git的多个gitignore源中的模式(如果您使用的是GNU find):

$ cd {your project directory}
$ find . -path ./.git -prune -o -print \
| git check-ignore --no-index --stdin --verbose
Run Code Online (Sandbox Code Playgroud)

它将检查存储库当前分支中的所有文件(除非您已在本地删除它们).

它还标识了特定的gitignore源代码行.

Git继续跟踪一些与gitignore模式匹配的文件的变化,因为这些文件已经添加了.有用的是,上面的命令也会显示这些文件.

如果您使用的是Windows,Git Bash包含GNU !(如图所示find).

如果列表很长(并且你有find --version),你也可以通过扩展(稍微)显示它们:

$ cd {your project directory}
$ find . -path ./.git -prune -o -print \
| git check-ignore --no-index --stdin --verbose \
| rev | sort | rev
Run Code Online (Sandbox Code Playgroud)

欲了解更多详情,请参阅rev,man find,man git-check-ignore,和man rev.


Top*_*ter 6

如果您只需要被忽略的文件的有效列表(无论它们如何被忽略),并且没有任何额外的通知和日志。

创建后,在任何地方(在 Git-bash 中)运行:

git ignore-list
Run Code Online (Sandbox Code Playgroud)

通过执行以下命令来创建它:

git config --global alias.ignore-list "! cd -- \"\${GIT_PREFIX:-.}\" && git ls-files -v \${1:-.} | sed -n -e \"s,^[a-z] \(.*\)$,\${GIT_PREFIX:-./}\1,p\" && git status --ignored --porcelain \${1:-.} 2>/dev/null | sed -n -e \"s/^\(\\!\\! \)\(.*\)$/\2/p\" #"
Run Code Online (Sandbox Code Playgroud)

示例用法, 假设这是初始提交并且您想要推送所有内容,请尝试:

git ignore-list | xargs git add -f
Run Code Online (Sandbox Code Playgroud)

笔记:

  1. 它已经过测试并且可以在两个macOS平台上运行Windows
  2. 进入目录后cd,仅列出该目录(或其子目录)中忽略的文件。
  3. 最后,始终记录相对于 root-dir 的路径(其中包含.gitdir,无论您cd进入哪个目录)。

另一个有用的东西是人类可读性,就像如果忽略整个目录,例如buildor node_modules,这将仅记录目录名称。
虽然git ls-files --others -i --exclude-standard命令记录每个文件(适用于xargs)。