我有一个Git存储库,看起来像这样:

A <- B <- C <- D <- HEAD

我希望分支的头部指向A,即,我希望B、C、D和head消失,我希望头部与A同义。

听起来我可以尝试重新设置基础(不适用,因为我已经在两者之间进行了更改),也可以恢复。但如何恢复多次提交?我一次回复一个吗?订单重要吗?


当前回答

我真的想避免硬重置,这就是我想到的。

A -> B -> C -> D -> HEAD

返回A(后退4步):

git pull                  # Get latest changes
git reset --soft HEAD~4   # Set back 4 steps
git stash                 # Stash the reset
git pull                  # Go back to head
git stash pop             # Pop the reset 
git commit -m "Revert"    # Commit the changes

其他回答

首先确保您的工作副本未被修改。

然后:

git diff --binary HEAD commit_sha_you_want_to_revert_to | git apply

然后就承诺吧。不要忘记记录还原的原因。

git reset --hard a
git reset --mixed d
git commit

这将同时对所有人起到恢复作用。给出一个好的承诺信息。

如果你

合并提交和您无法恢复,并且你不介意粉碎你要还原的历史,

那么你可以

git reset --soft HEAD~(number of commits you'd like to revert)
git commit -m "The stuff you didn't like."
git log
# copy the hash of your last commit
git revert <hash of your last (squashed) commit>

然后,当您想要推送更改时,请记住使用-f标志,因为您修改了历史记录

git push <your fork> <your branch> -f

我觉得有用的干净方式

git revert --no-commit HEAD~3..
git commit -m "your message regarding reverting the multiple commits"

此命令仅使用一次提交来还原最后3次提交。

也不重写历史,因此不需要强制推送。

这个帮助创建范围。意思是HEAD~3..与HEAD~3..HEAD相同

为此,您只需使用revert命令,指定要恢复的提交范围。

考虑到您的示例,您必须这样做(假设您在分支“master”上):

git revert master~3..master

或git revert B…D或git return D C B

这将使用B、C和D的反向提交在本地创建一个新的提交(这意味着它将撤消这些提交带来的更改):

A <- B <- C <- D <- BCD' <- HEAD