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

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

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

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


当前回答

如果你

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

那么你可以

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

其他回答

这是Jakub回答中提供的解决方案之一的扩展。

我面临的情况是,我需要回滚的提交有些复杂,其中几个提交是合并提交,我需要避免重写历史。我无法使用一系列git-restore命令,因为我最终在添加的反转更改之间遇到了冲突。我最终使用了以下步骤。

首先,检查目标提交的内容,同时将HEAD留在分支的顶端:

git checkout -f <target-commit> -- .

(--确保<targetcommit>被解释为提交而不是文件;.指的是当前目录。)

然后,确定在回滚的提交中添加了哪些文件,因此需要删除这些文件:

git diff --name-status --cached <target-commit>

添加的文件应在行的开头显示为“A”,并且不应有其他差异。现在,如果需要删除任何文件,请暂存这些文件以进行删除:

git rm <filespec>[ <filespec> ...]

最后,提交回复:

git commit -m 'revert to <target-commit>'

如果需要,请确保返回到所需状态:

git diff <target-commit> <current-commit>

应该没有区别。

可能比这里的其他方法不那么优雅,但我一直使用get-reset--hardHEAD~N来撤销多次提交,其中N是您想要返回的提交次数。

或者,如果不确定确切的提交次数,只需多次运行git-reset--hardHEAD^(返回一次提交),直到达到所需的状态。

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

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

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

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 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