给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?
理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。
给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?
理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。
当前回答
如果你不喜欢“还原还原”这个想法(特别是当这意味着丢失多次提交的历史信息时),你可以随时查看git文档中关于“还原错误合并”的内容。
给定下面的起始情况
P---o---o---M---x---x---W---x
\ /
A---B---C----------------D---E <-- fixed-up topic branch
(W是归并M的初始还原;D和E修复了你最初损坏的特性分支/提交)
你现在可以简单地重放提交A到E,这样它们都不“属于”被还原的合并:
$ git checkout E
$ git rebase --no-ff P
分支的新副本现在可以再次合并到主分支:
A'---B'---C'------------D'---E' <-- recreated topic branch
/
P---o---o---M---x---x---W---x
\ /
A---B---C----------------D---E
其他回答
我是这样做的: 如果分支my_branchname包含在被还原的合并中。我想取消my_branchname:
我首先从my_branchname执行git checkout -b my_new_branchname。 然后我做一个git重置——软$COMMIT_HASH,其中$COMMIT_HASH是my_branchname第一次提交之前的提交哈希(见git日志) 然后我新建一个commit git commit -m "Add back reverted changes" 然后我向上推新的分支git推origin new_branchname 然后我对新分支提出了拉请求。
在我的例子中,我需要在恢复后提交更改,然后才能在没有失败的情况下选择原始提交。
git commit -m "Make changes (original commit)."
git revert <original commit hash>
git commit -m "Revert original commit."
git cherry-pick <original commit hash>
如果你不喜欢“还原还原”这个想法(特别是当这意味着丢失多次提交的历史信息时),你可以随时查看git文档中关于“还原错误合并”的内容。
给定下面的起始情况
P---o---o---M---x---x---W---x
\ /
A---B---C----------------D---E <-- fixed-up topic branch
(W是归并M的初始还原;D和E修复了你最初损坏的特性分支/提交)
你现在可以简单地重放提交A到E,这样它们都不“属于”被还原的合并:
$ git checkout E
$ git rebase --no-ff P
分支的新副本现在可以再次合并到主分支:
A'---B'---C'------------D'---E' <-- recreated topic branch
/
P---o---o---M---x---x---W---x
\ /
A---B---C----------------D---E
I saw responses include the command git reset --hard HEAD without any caution. You should be careful with that command because of the option --hard. It resets your index and your remote repo but mostly, it also resets your local repo and all commits that were not pushed to the remote yet will be lost, both from your local repo and index. Never use that flag --hard unless you are sure you also want to reset all your local work from the current commit till the hash you chose. If anyway you did it by mistake, run git reflog to retrieve your ~hash then git reset --hard ~hash to recover your files.
恢复还原程序就可以了
例如,
如果abcdef是你的提交,ghijkl是你还原abcdef时的提交,那么运行:
git revert ghijkl
这将还原还原