是否有一种方法可以使用像git ls-files这样的命令来只显示未跟踪的文件?

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

git ls-files -d | xargs git rm

我想要一些类似的未跟踪文件:

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

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

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

看来这里还有更好的命令可以用。如果没有,我如何创建自定义git命令?


当前回答

我认为这和最初海报的意图是一样的:

转到添加 。

补充一些注意事项:

You have run git status and confirmed your local directories are clean You have run git diff on each file reported in git status, and confirmed your changes are clean Your changes are covered with automated unit testing Your changes are covered with automated integration testing You have run the integration tests through a debugger, verifying the new behavior by white box observing the new code in action You have run all linting / code convention rules and they pass You have run all unit tests and they pass You have run all local integration tests, and they pass You have deployed your changes to an app review environment and manually tested them end to end in isolation from other changes You have merged latest from main branch and re-run all automated unit and integration testing, fixing any merge conflicts and test failures

现在,我的朋友,你准备好做加法了。而不受惩罚。

其他回答

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

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

如果你需要将输出管道到xargs,明智的做法是使用git ls-files -z和xargs -0来注意空格:

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

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

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

编辑:参考:git-ls-files

一切都很简单

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

git status -u

我认为这和最初海报的意图是一样的:

转到添加 。

补充一些注意事项:

You have run git status and confirmed your local directories are clean You have run git diff on each file reported in git status, and confirmed your changes are clean Your changes are covered with automated unit testing Your changes are covered with automated integration testing You have run the integration tests through a debugger, verifying the new behavior by white box observing the new code in action You have run all linting / code convention rules and they pass You have run all unit tests and they pass You have run all local integration tests, and they pass You have deployed your changes to an app review environment and manually tested them end to end in isolation from other changes You have merged latest from main branch and re-run all automated unit and integration testing, fixing any merge conflicts and test failures

现在,我的朋友,你准备好做加法了。而不受惩罚。

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

你可以使用git clean操作-n(演练)来显示它将删除哪些文件(包括.gitignore文件):

git clean -xdn

这样做的好处是可以显示未被跟踪的所有文件和文件夹。参数:

x -显示所有未跟踪的文件(包括被git忽略的文件和其他文件,如构建输出等…) 显示未跟踪的目录 N -最重要的是!-干式运行,即实际上不删除任何东西,只是使用清洁机制显示结果。

这样做可能有点不安全万一你忘了-n。所以我通常在git配置中使用别名。

当寻找可能添加的文件时。git的输出显示了这一点,但它还包括许多其他东西。下面的命令对于获得相同的文件列表很有用,但不需要其他所有东西。

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

这在管道中组合时非常有用,可以找到匹配特定模式的文件,然后将其输送到git add中。

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