git 使用 xargs 撤消别名
我有一个 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技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在其之前使用 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, thegit ls-files -d
should never generate any output (and if it did, you would want to usegit ls-files -d -z
to have it produce NUL-terminated output forxargs -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 (whichgit reset --hard
will not touch), but you might want to change it togit clean -dfq
to also delete wholly untracked directories).从 手册页:
确保您的 xargs 版本具有该选项 (
man xargs
)From the man page:
Make sure your version of xargs has that option (
man xargs
)也许您想使用 xargs 的“-r”选项:
这样,如果 ls-files 没有显示任何内容,xargs 根本不会调用 git rm 。
Maybe you want to use xargs' "-r" option:
That way, if
ls-files
shows nothing, xargs won't callgit rm
at all.有“git clean”(man git-clean):从工作树中删除未跟踪的文件
There is "git clean" (man git-clean): Remove untracked files from the working tree