我正在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
笔记:
git status --ignored
.gitignore
git clean -ndX
适用于较旧的gits,显示可以删除被忽略文件的预览(不删除任何内容)同样有趣(在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
.
Pen*_*eng 430
有一种更简单的方法(git 1.7.6+):
__CODE__
见有没有办法告诉git的状态忽视的.gitignore文件的影响?
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)
注意:此解决方案不会显示已删除的已忽略文件.
小智 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
不出现.
qwe*_*ymk 17
Git现在内置了这个功能
git check-ignore *
Run Code Online (Sandbox Code Playgroud)
当然,您可以将glob更改为类似于**/*.dll
您的情况
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
.
如果您只需要被忽略的文件的有效列表(无论它们如何被忽略),并且没有任何额外的通知和日志。
创建后,在任何地方(在 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)
示例用法, 假设这是初始提交并且您想要推送所有内容,请尝试:
Run Code Online (Sandbox Code Playgroud)git ignore-list | xargs git add -f
macOS
平台上运行Windows
!cd
,仅列出该目录(或其子目录)中忽略的文件。.git
dir,无论您cd
进入哪个目录)。另一个有用的东西是人类可读性,就像如果忽略整个目录,例如build
or node_modules
,这将仅记录目录名称。
虽然git ls-files --others -i --exclude-standard
命令记录每个文件(适用于xargs
)。
归档时间: |
|
查看次数: |
271183 次 |
最近记录: |