git 使用 xargs 撤消别名

发布于 2024-08-31 10:24:28 字数 421 浏览 6 评论 0原文

我有一个 git 别名 (git undo),可以撤消工作目录中的所有内容,包括新文件、更改的文件和删除的文件:

!git reset --hard && git ls-files -d | xargs -0 git rm --ignore-unmatch && git clean -fq

在 OS X 上,这非常有效。然而,在 Linux 上,我遇到了以下问题:如果没有从存储库中删除任何文件,则 git ls-files -d | xargs -0 git rm --ignore-unmatch 命令将失败(xargs 不会传递任何内容)。

如果 xargs 没有从 git ls-files 接收到任何内容,是否有办法让 xargs 默默地继续前进?

I have a git alias (git undo) that undoes everything in the working directory, including new files, changed files, and deleted files:

!git reset --hard && git ls-files -d | xargs -0 git rm --ignore-unmatch && git clean -fq

On OS X, this works great. On Linux, however, I run into the following issue: if no files have been deleted from the repository, the git ls-files -d | xargs -0 git rm --ignore-unmatch command will fail (xargs will be passed nothing).

Is there a way to have xargs silently move on if it receives nothing from git ls-files?

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

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

发布评论

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

评论(4

彡翼 2024-09-07 10:24:28

在其之前使用 git reset --hard , git ls-files -d 永远不会生成任何输出(如果确实如此,您将需要使用git ls-files -d -z 让它为 xargs -0 生成以 NUL 结尾的输出。

完成 git reset --hard 后,工作树的跟踪部分和整个索引将与 HEAD 提交匹配。 git ls-files -d 将仅显示索引中但不在工作树中的文件。由于工作树将拥有索引所拥有的所有内容,因此在硬重置后不应有任何已删除的文件。

git clean 位对于删除未跟踪的文件很有用(git reset --hard 不会触及),但您可能希望将其更改为 git clean - dfq 也可以删除完全未跟踪的目录)。

With a git reset --hard before it, the git ls-files -d should never generate any output (and if it did, you would want to use git ls-files -d -z to have it produce NUL-terminated output for xargs -0).

Once you have done git reset --hard, the tracked portion of the working tree and the whole index will match the HEAD commit. git ls-files -d will only show files that are in the index but not in the working tree. Since the working tree will have everthing that the index has, there should never be any deleted files after a hard reset.

The git clean bit is useful to delete untracked files (which git reset --hard will not touch), but you might want to change it to git clean -dfq to also delete wholly untracked directories).

千年*琉璃梦 2024-09-07 10:24:28

手册页

--如果为空则不运行,-r
如果标准输入不包含任何非空白,则不运行
命令。通常,即使有
,该命令也会运行一次
没有输入。此选项是 GNU 扩展。

确保您的 xargs 版本具有该选项 (man xargs)

From the man page:

--no-run-if-empty, -r
If the standard input does not contain any nonblanks, do not run
the command. Normally, the command is run once even if there is
no input. This option is a GNU extension.

Make sure your version of xargs has that option (man xargs)

风苍溪 2024-09-07 10:24:28

也许您想使用 xargs 的“-r”选项:

xargs -r -0 git rm --ignore-unmatch

这样,如果 ls-files 没有显示任何内容,xargs 根本不会调用 git rm 。

Maybe you want to use xargs' "-r" option:

xargs -r -0 git rm --ignore-unmatch

That way, if ls-files shows nothing, xargs won't call git rm at all.

不气馁 2024-09-07 10:24:28

有“git clean”(man git-clean):从工作树中删除未跟踪的文件

There is "git clean" (man git-clean): Remove untracked files from the working tree

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