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

发布于 2024-09-25 16:02:27 字数 479 浏览 10 评论 0原文

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

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

git ls-files -d | xargs git rm

我想要对未跟踪的文件执行类似的操作:

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

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

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

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

Is there a way to use a command like git ls-files to show only untracked 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 -o option 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:

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

隔纱相望 2024-10-02 16:02:27

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

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-文件

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 -z and 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

少年亿悲伤 2024-10-02 16:02:27

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

警告这将删除所有未跟踪的文件,包括目录

git clean -df

如果您还想包括专门忽略的文件,请添加 x 。我一整天都使用git clean -dfx很多

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

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

warning this will remove all of you untracked files including directories

git clean -df

add x to that if you want to also include specifically ignored files. I use git clean -dfx a lot throughout the day.

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

携君以终年 2024-10-02 16:02:27

git add -A -n
会做你想做的事。 -A 将所有未跟踪的和修改文件添加到存储库,-n 使其成为空运行,其中未执行添加,但会给出状态输出,列出添加的每个文件。

git add -A -n
will do what you want. -A adds all untracked and modified files to the repo, -n makes it a dry-run where the add isn't performed but the status output is given listing each file that would have been added.

花开半夏魅人心 2024-10-02 16:02:27
git status -u

将列出所有未跟踪的文件。长形式:

git status --untracked-files
git status -u

will list all untracked files. Long form:

git status --untracked-files
送你一个梦 2024-10-02 16:02:27

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

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

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
秋意浓 2024-10-02 16:02:27

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

您可以使用 git clean 操作和 -n (试运行)来显示它将删除哪些文件(包括 .gitignore 文件):

git clean -xdn

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

  • x - 显示所有未跟踪的文件(包括被 git 和其他文件忽略,如构建输出等...)
  • d - 显示未跟踪的目录
  • n - 最重要的是! - dryrun,即实际上不删除任何内容,只是使用清理机制来显示结果。

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

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 -xdn

This has the advantage of showing all files and all folders that 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.

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.

安静 2024-10-02 16:02:27

当寻找可能添加的文件时。 git show 的输出做到了这一点,但它还包含许多其他内容。以下命令对于获取相同的文件列表非常有用,但不包含所有其他内容。

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

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

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

When looking for files to potentially add. The output from git show does 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 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 status --porcelain | grep "^?? "  | sed -e 's/^[?]* //' | \
egrep "\.project$|\.settings$\.classfile$" | xargs -n1 git add
昨迟人 2024-10-02 16:02:27

我之前检查过的所有答案都会列出要提交的文件。
这是一个简单易用的解决方案,仅列出尚未包含在文件中的文件
repo 且不受 .gitignore 约束。

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

或者

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

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.

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

or

git status --porcelain | grep -v '\?\?'
迷途知返 2024-10-02 16:02:27

我想我发现了一个功能/问题:

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

只想列出未跟踪的文件:
如果未跟踪的文件位于已修改的目录中,则不会列出该文件

I think I found a feature/problem with:

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

wanting to list only untracked files:
if untracked file is in a modified directory, file will not be listed

夜光 2024-10-02 16:02:27

我认为这将执行与原始海报预期相同的操作:

git add 。

添加一些警告:

  • 您已运行 git status 并确认您的本地目录是干净的
  • 您已运行对 git status 中报告的每个文件进行 git diff,并确认您的更改是干净的
  • 您的更改已包含在自动单元测试中
  • 您的更改已包含在自动集成测试中
  • 您已运行通过调试器进行集成测试,通过白盒观察正在运行的新代码来验证新行为
  • 您已经运行了所有 linting/代码约定规则并且它们通过了
  • 您已经运行了所有单元测试并且它们通过了
  • 您已经运行了所有本地集成测试并且它们通过了通过
  • 您已将更改部署到应用程序审查环境,并与其他更改隔离地手动端到端测试它们
  • 您已从主分支合并最新内容,并重新运行所有自动化单元和集成测试,修复任何合并冲突和测试失败

现在,我的朋友,你已经准备好git add .而不受惩罚了。

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

git add .

Adding some caveats:

  • 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

Now, my friend, you are ready to git add . with impunity.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文