我有一个Git存储库,看起来像这样:
A <- B <- C <- D <- HEAD
我希望分支的头部指向A,即,我希望B、C、D和head消失,我希望头部与A同义。
听起来我可以尝试重新设置基础(不适用,因为我已经在两者之间进行了更改),也可以恢复。但如何恢复多次提交?我一次回复一个吗?订单重要吗?
我有一个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
其他回答
在我看来,一个非常简单和干净的方法可以是:
回到A
git checkout -f A
将master的头指向当前状态
git symbolic-ref HEAD refs/heads/master
save
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
与Jakub的回答类似,这允许您轻松选择要还原的连续提交。
# Revert all commits from and including B to HEAD, inclusively
git revert --no-commit B^..HEAD
git commit -m 'message'
首先确保您的工作副本未被修改。
然后:
git diff --binary HEAD commit_sha_you_want_to_revert_to | git apply
然后就承诺吧。不要忘记记录还原的原因。