Git:仅列出“未跟踪”的文件(还有自定义命令)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3801321/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:09:56  来源:igfitidea点击:

Git: list only "untracked" files (also, custom commands)

gitgit-statusgit-rmgit-stagegit-ls-files

提问by We Are All Monica

Is there a way to use a command like git ls-filesto show only untracked files?

有没有办法使用像git ls-files只显示未跟踪文件的命令?

The reason I'm asking is because I use the following command to process all deleted files:

我问的原因是因为我使用以下命令来处理所有已删除的文件:

git ls-files -d | xargs git rm

I'd like something similar for untracked files:

对于未跟踪的文件,我想要类似的东西:

git some-command --some-options | xargs git add

I was able to find the -ooption to git ls-files, but this isn't what I want because it also shows ignored files. I was also able to come up with the following long and ugly command:

我能够找到-o选项git ls-files,但这不是我想要的,因为它还显示被忽略的文件。我还想出了以下又长又丑的命令:

git status --porcelain | grep '^??' | cut -c4- | xargs git add

It seems like there's got to be a better command I can use here. And if there isn't, how do I create custom git commands?

似乎必须有一个更好的命令我可以在这里使用。如果没有,我该如何创建自定义 git 命令?

回答by takeshin

To list untracked files try:

要列出未跟踪的文件,请尝试:

git ls-files --others --exclude-standard

If you need to pipe the output to xargs, it is wise to mind white spaces using git ls-files -zand xargs -0:

如果您需要将输出通过管道传输到xargs,那么使用git ls-files -zand介意空格是明智的xargs -0

git ls-files -z -o --exclude-standard | xargs -0 git add

Nice alias for adding untracked files:

添加未跟踪文件的好别名:

au = !git add $(git ls-files -o --exclude-standard)

Edit:For reference: git-ls-files

编辑:供参考:git-ls-files

回答by Dustin

If you just want to remove untracked files, do this:

如果您只想删除未跟踪的文件,请执行以下操作:

git clean -df

add xto that if you want to also include specifically ignored files. I use git clean -dfxa lotthroughout the day.

添加x到,如果你想也包括专门忽略的文件。我用git clean -dfx一个很多全天。

You can create custom git by just writing a script called git-whateverand having it in your path.

您可以通过编写一个名为的脚本git-whatever并将其放在您的路径中来创建自定义 git 。

回答by Mike Lococo

git add -A -nwill do what you want. -Aadds all untracked files to the repo, -nmakes it a dry-runwhere the add isn't performed but the status output is given listing each file that wouldhave been added.

git add -A -n会做你想做的。-A将所有未跟踪文件到回购,-n使得它dry-run不执行附加,但被赋予状态输出列出每个文件被添加。

回答by youurayy

The accepted answer crashes on filenames with space. I'm at this point not sure how to update the alias command, so I'll put the improved version here:

接受的答案在带有空格的文件名上崩溃。我现在不确定如何更新别名命令,所以我将改进的版本放在这里:

git ls-files -z -o --exclude-standard | xargs -0 git add

回答by Valentyna

Everything is very simple

一切都很简单

To get list of all untracked files use command git statuswith option -u(--untracked-files)

要获取所有未跟踪文件的列表,请使用命令git status选项-u(--untracked-files)

git status -u

回答by cmac

When looking for files to potentially add. The output from git showdoes that but it also includes a lot of other stuff. The following command is useful to get the same list of files but without all of the other stuff.

在寻找可能添加的文件时。来自的输出git show做到了这一点,但它还包括很多其他的东西。以下命令可用于获取相同的文件列表但不包含所有其他内容。

 git status --porcelain | grep "^?? " | sed -e 's/^[?]* //'

This is useful when combined in a pipeline to find files matching a specific pattern and then piping that to git add.

当在管道中组合以查找匹配特定模式的文件,然后将其传送到git add.

git status --porcelain | grep "^?? "  | sed -e 's/^[?]* //' | \
egrep "\.project$|\.settings$\.classfile$" | xargs -n1 git add

回答by code_fodder

I know its an old question, but in terms of listing untracked files I thought I would add another one which also lists untracked folders:

我知道这是一个老问题,但在列出未跟踪的文件方面,我想我会添加另一个也列出未跟踪文件夹的文件:

You can used the git clean operation with -n (dry run) to show you which files it will remove (including the .gitignore files) by:

您可以将 git clean 操作与 -n (dry run) 一起使用,以通过以下方式向您显示它将删除的文件(包括 .gitignore 文件):

git clean -xdn

git clean -xdn

This has the advantage of showing all filesand all foldersthat are not tracked. Parameters:

这具有显示所有文件所有未跟踪的文件夹的优点。参数:

  • x- Shows all untracked files (including ignored by git and others, like build output etc...)
  • d- show untracked directories
  • n- and most importantly!- dryrun, i.e. don't actually delete anything, just use the clean mechanism to display the results.
  • x- 显示所有未跟踪的文件(包括被 git 和其他人忽略的文件,如构建输出等...)
  • d- 显示未跟踪的目录
  • n- 最重要的是!- 空运行,即实际上不删除任何内容,只需使用清理机制来显示结果。

It can be a little bit unsafe to do it like this incase you forget the -n. So I usually alias it in git config.

这样做可能有点不安全,以防您忘记-n. 所以我通常在 git config 中给它起别名。

回答by Frank-Rene Sch?fer

All previous answers which I checked would list the files to be committed, too. Here is a simple and easy solution that only lists files which are not yet in the repo and not subject to .gitignore.

我之前检查过的所有答案也会列出要提交的文件。这是一个简单易行的解决方案,它只列出尚未在 repo 中且不受.gitignore.

git status --porcelain | awk '/^\?\?/ { print ; }'

or

或者

git status --porcelain | grep -v '\?\?'

回答by Tim Fulmer

I think this will so the same thing as the original poster intended:

我认为这与原始海报的意图相同:

git add .

git add .