给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?

理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。


当前回答

在我的例子中,我需要在恢复后提交更改,然后才能在没有失败的情况下选择原始提交。

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 cherry-pick <原始提交sha> 将复制原始提交,本质上是重新应用提交

还原将做同样的事情,与一个更混乱的提交消息: Git revert <commit sha of revert>

这两种方式都允许您在不覆盖历史记录的情况下进行git推送,因为它会在还原后创建一个新的提交。 当输入commit sha时,通常只需要前5或6个字符: Git选择6bfabc

在最初意外删除所有文件的恐慌之后,我使用以下方法来取回我的数据

git reset HEAD@{1}         
git fsck --lost-found      
git show 
git revert <sha that deleted the files>

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

这将还原还原

如果你还没有按这个改变,git重置-硬头^

否则,还原还原完全没问题。

另一种方法是git结帐HEAD^^——。然后git add -A && git提交。