我有一个Git存储库,看起来像这样:
A <- B <- C <- D <- HEAD
我希望分支的头部指向A,即,我希望B、C、D和head消失,我希望头部与A同义。
听起来我可以尝试重新设置基础(不适用,因为我已经在两者之间进行了更改),也可以恢复。但如何恢复多次提交?我一次回复一个吗?订单重要吗?
我有一个Git存储库,看起来像这样:
A <- B <- C <- D <- HEAD
我希望分支的头部指向A,即,我希望B、C、D和head消失,我希望头部与A同义。
听起来我可以尝试重新设置基础(不适用,因为我已经在两者之间进行了更改),也可以恢复。但如何恢复多次提交?我一次回复一个吗?订单重要吗?
当前回答
为此,您只需使用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
其他回答
我发现自己需要恢复一系列的提交,然后重新恢复它们,以帮助团队提出明确的拉取请求,而不必强制推送他们的目标分支(直接提交给)
# checkout the branch that should be targeted
git checkout $branch_target
# revert the commits in $branch_target to some $count where
# $count is the number of commits to revert
# cut is used to slice just the commit hash field from each line of output
# xargs runs the command once for each line of input, reversing the commits!
git log --oneline -n $count | cut -d' ' -f1 | xargs git revert
# check out the branch which should be the source of the pull request
git checkout -b $branch_for_pull
# revert the revert commits
# $count is that same number of commits being reverted (again)
git log --oneline -n $count | cut -d' ' -f1 | xargs git revert
# push branches up and go off to create PR in whatever web UI
git push --set-upstream origin $branch_for_pull # it's new!
git checkout $branch_target
git push # if this branch wasn't pushed, just fix the issue locally instead..
因为这会以相反的顺序将所有提交从HEAD还原为git-log-n$count,所以它可以很好地、干净地处理任何数量的提交
在此状态下从$branch_target查看
% git log --oneline origin/$branch_target
ffff006 (origin/$branch_target, $branch_target) Revert "first commit"
ffff005 Revert "second commit"
ffff004 Revert "third commit"
ffff003 third commit
ffff002 second commit
ffff001 first commit
在此状态下从$branch_for_pull查看
% git log --oneline origin/$branch_for_pull
ffff009 (origin/$branch_for_pull, $branch_for_pull) Revert "Revert "third commit""
ffff008 Revert "Revert "second commit""
ffff007 Revert "Revert "first commit""
ffff006 (origin/$branch_target, $branch_target) Revert "first commit"
ffff005 Revert "second commit"
ffff004 Revert "third commit"
ffff003 third commit
ffff002 second commit
ffff001 first commit
如果意图是用变更集创建N个分支,但它们都被提交到同一个分支,您仍然可以将它们全部还原为基本提交,然后只还原所需的还原,因为变更集应该按逻辑排序(试着说5倍快)
使用像HEAD~7..HEAD~5这样的语法可能有助于描述范围以精确地分割还原-还原分支
在这里,当恢复最后7次提交(gitlog-n7),但在一个分支中恢复5次(gitlog-n 5),然后在另一个gitlogHEAD~12..HEAD~10(12是7次提交+5次提交,假设新的PR分支基于“之前”分支,或FF(未压缩)的结果,将分支“之前”合并到原始目标分支中时,这是有意义的
还原共享存储库上的一组提交的简单方法是将git-rever与git-rev列表结合使用。后者将为您提供一个提交列表,前者将自己进行还原。
有两种方法可以做到这一点。如果要在一次提交中还原多次提交,请使用:
for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-commit $i; done
这将恢复您需要的一组提交,但将所有更改保留在工作树上,之后应照常提交。
另一种选择是对每个还原的更改进行一次提交:
for i in `git rev-list <first-commit-sha>^..<last-commit-sha>`; do git revert --no-edit -s $i; done
例如,如果您有一个提交树
o---o---o---o---o---o--->
fff eee ddd ccc bbb aaa
要将更改从eee还原为bbb,请运行
for i in `git rev-list eee^..bbb`; do git revert --no-edit -s $i; done
在我看来,一个非常简单和干净的方法可以是:
回到A
git checkout -f A
将master的头指向当前状态
git symbolic-ref HEAD refs/heads/master
save
git commit
我很沮丧,这个问题无法得到回答。其他所有问题都与如何正确还原和保存历史有关。这个问题说:“我希望分支的头部指向A,即我希望B、C、D和head消失,我希望头部与A同义。”
git checkout <branch_name>
git reset --hard <commit Hash for A>
git push -f
我在阅读Jakub的帖子时学到了很多东西,但公司里的一些人(可以在没有Pull Request的情况下推送到我们的“测试”分支)推送了五次错误的提交,试图修复他在五次提交之前犯的错误。不仅如此,还接受了一到两个拉取请求,但现在情况很糟糕。所以忘掉它吧;我找到了最后一个好的提交(abc1234),并运行了基本脚本:
git checkout testing
git reset --hard abc1234
git push -f
我告诉在这个仓库工作的其他五个人,他们最好记录下过去几个小时的变化,并从最新的测试中删除/重新广播。故事结束。
我觉得有用的干净方式
git revert --no-commit HEAD~3..
git commit -m "your message regarding reverting the multiple commits"
此命令仅使用一次提交来还原最后3次提交。
也不重写历史,因此不需要强制推送。
这个帮助创建范围。意思是HEAD~3..与HEAD~3..HEAD相同