给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?
理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。
给定一个已经使用commit提交,然后使用revert恢复的更改,那么撤消该恢复的最佳方法是什么?
理想情况下,这应该通过一个新的提交来完成,这样就不会重写历史。
当前回答
要返回在提交后恢复的非暂存和暂存更改:
git reset HEAD@{1}
恢复所有未执行的删除操作:
git ls-files -d | xargs git checkout --
其他回答
如果你还没有按这个改变,git重置-硬头^
否则,还原还原完全没问题。
另一种方法是git结帐HEAD^^——。然后git add -A && git提交。
要返回在提交后恢复的非暂存和暂存更改:
git reset HEAD@{1}
恢复所有未执行的删除操作:
git ls-files -d | xargs git checkout --
在我的例子中,我需要在恢复后提交更改,然后才能在没有失败的情况下选择原始提交。
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 revert <commit-id>
你只需要运行:
git cherry-pick <commit-id>
我必须使用这个命令来提交我的更改。
你可以通过以下命令获取你的提交ID:
git log --pretty=format:"%h - %an, %ar : %s"
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.