bash 获取 Git 控制文件夹中所有未版本控制文件的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/13263369/
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
Getting a list of all unversioned files in a Git-controlled folder
提问by Ryan Lester
Getting a list of unversioned files in a Git-controlled folder is way more annoying than it needs to be. Unless I really suck at reading man pages, it doesn't look like Git provides a facility to perform this operation on its own.
在 Git 控制的文件夹中获取未版本控制的文件列表比它需要的更烦人。除非我真的不擅长阅读手册页,否则 Git 似乎并没有提供一种工具来自行执行此操作。
There may be a more elegant way of performing this, but here's a one-liner I threw together for this task, in case anyone else ever needs to use it.
可能有一种更优雅的方式来执行此操作,但这是我为此任务拼凑的单行代码,以防其他人需要使用它。
Edit: turns out there's a standard way to do this that already works well.
编辑:事实证明,有一种标准的方法可以很好地做到这一点。
git ls-files --other [--exclude-standard]
采纳答案by Dietrich Epp
Or...
或者...
git clean -dnx | cut -c 14-
If you don't want to see ignored files,
如果您不想看到被忽略的文件,
git clean -dn | cut -c 14-
回答by platforms
Actually, if you use git cleanwith the -nor --dry-runoption, it will print out a list untracked files that it would have removed had you run it with the -for --forceoption. Adding the -dflag includes directories that are either empty or contain only untracked files.
其实,如果你使用git clean与-n或--dry-run选项,它会打印出清单未跟踪文件,它会删除你已经与运行-f或--force选项。添加该-d标志包括空目录或仅包含未跟踪文件的目录。
So you can run this command from within a git repository:
因此,您可以从 git 存储库中运行此命令:
$ git clean -dn
And get output like this:
并得到这样的输出:
Would remove dir/untracked_file_1.txt
Would remove untracked_file_2.txt
Here's a bonus: It respects your .gitignore file as well, though if you add the -Xflag, it will list only ignored files.
这是一个奖励:它也尊重您的 .gitignore 文件,但如果您添加该-X标志,它只会列出被忽略的文件。
回答by Ryan Lester
diff <(echo "`git ls-tree -r master | sed 's/.*\t//g'`") <(echo "`find . -type f`") | grep '> .*' | cut -c 3-

