Git:仅列出“未跟踪”的文件(还有自定义命令)
有没有办法使用像 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
要列出未跟踪的文件,请尝试:
如果您需要将输出通过管道传输到 xargs,明智的做法是使用 git ls-files -z 和 xargs 来注意空格 - 0:
用于添加未跟踪文件的好别名:
编辑:供参考:git-ls-文件
To list untracked files try:
If you need to pipe the output to
xargs
, it is wise to mind white spaces usinggit ls-files -z
andxargs -0
:Nice alias for adding untracked files:
Edit: For reference: git-ls-files
如果您只想删除未跟踪的文件,请执行以下操作:
警告这将删除所有未跟踪的文件,包括目录
如果您还想包括专门忽略的文件,请添加
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
add
x
to that if you want to also include specifically ignored files. I usegit 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.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 adry-run
where the add isn't performed but the status output is given listing each file that would have been added.将列出所有未跟踪的文件。长形式:
will list all untracked files. Long form:
接受的答案在带有空格的文件名上崩溃。我现在不确定如何更新别名命令,所以我将改进的版本放在这里:
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 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 directoriesn
- 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.当寻找可能添加的文件时。 git show 的输出做到了这一点,但它还包含许多其他内容。以下命令对于获取相同的文件列表非常有用,但不包含所有其他内容。
当组合在管道中以查找与特定模式匹配的文件,然后通过管道将其传输到 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.This is useful when combined in a pipeline to find files matching a specific pattern and then piping that to
git add
.我之前检查过的所有答案都会列出要提交的文件。
这是一个简单易用的解决方案,仅列出尚未包含在文件中的文件
repo 且不受
.gitignore
约束。或者
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
.or
我想我发现了一个功能/问题:
只想列出未跟踪的文件:
如果未跟踪的文件位于已修改的目录中,则不会列出该文件
I think I found a feature/problem with:
wanting to list only untracked files:
if untracked file is in a modified directory, file will not be listed
我认为这将执行与原始海报预期相同的操作:
git add 。
添加一些警告:
git status
并确认您的本地目录是干净的git status
中报告的每个文件进行git diff
,并确认您的更改是干净的现在,我的朋友,你已经准备好
git add .
而不受惩罚了。I think this will do the same thing as the original poster intended:
git add .
Adding some caveats:
git status
and confirmed your local directories are cleangit diff
on each file reported ingit status
, and confirmed your changes are cleanNow, my friend, you are ready to
git add .
with impunity.