如何使用 git 进行 hg revert --all?

How to do hg revert --all with git?

使用 Mercurial 时,可以发出以下命令: hg revert --all --rev <revision> 工作目录的内容设置为指定的 <revision>,但您留在树中的位置(相同的父级、分支等)。

如何在 git 中做到这一点?
在不移动分支指针的情况下,它应该表现得像 git reset --hard <commit>

长话短说;博士 git config alias.revert-all 'read-tree -um HEAD' git revert-all <commit>

我发现的所有变体的 comparison/test 可以在这里找到:http://git.io/vk9it

The content of the working directory is set to the specified but you stay where you are in the tree (same parent, branch, etc).

在 git 中,您的 HEAD 可以指向单个提交。你不能同时拥有多个 HEADS(除非你使用 git new-workdir 然后你有 n 个存储库副本)

唯一可能与此类似(但又不是您想要的)的是使用分支(与 hg 中的相同)。

一旦你有多个分支,每个分支就可以有不同的 HEAD。

您不必使用重置,您可以在任何给定点简单地 branch 退出:

git checkout -b <branch_name> <SHA-1>

你将有一个指向所需父级的新分支。


在 git 中,当您执行 git revert 时,您仍然在当前分支上,但您的父级是当前提交之前的提交(历史正在移动到树中的给定点)

git read-tree -um @ $thatcommit

会做的。那是 "transition the index and worktree from the HEAD aka @ commit to $thatcommit, as for checkout (but without touching HEAD)".

当您正在做的事情与任何便捷命令都不匹配时,核心命令会支持您:-)

如果您需要清除未提交的更改,git reset --hard 可能会选择一些 git clean 选项来首先清除完全未跟踪的文件,git 真的很讨厌在没有提交的情况下踩踏未提交的工作明确的命令。

为了完整起见,另一个(较慢的)替代方案: git diff --cached --full-index --binary <commit> | git apply --reverse --cached

只更新索引,强制到工作目录下发: git checkout-index -fa && git clean -dxf