Answers:
有很多方法可以做到这一点,例如:
如果你还没有推动公开承诺尚未:
git reset HEAD~1 --soft
就是这样,您的提交更改将在您的工作目录中,而LAST提交将从当前分支中删除。见git reset man
如果您确实公开(在名为“ master”的分支上)推送:
git checkout -b MyCommit //save your commit in a separate branch just in case (so you don't have to dig it from reflog in case you screw up :) )
正常还原提交并推送
git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this introduces a new commit (say, it's hash is 86b48ba) which removes changes, introduced in the commit in question (but those changes are still visible in the history)
git push origin master
现在,如果您想在工作副本中进行本地更改时进行这些更改(“以便您的本地副本保留在该提交中所做的更改”)-只需使用以下--no-commit
选项还原还原提交:
git revert --no-commit 86b48ba (hash of the revert commit).
我制作了一个小例子:https://github.com/Isantipov/git-revert/commits/master
reset
提到的第一个命令是说在磁头之前“软”重置为1 rev,这将保留所有本地更改。对我来说,在SourceTree中使用它并不是立即显而易见的。只需确保您已将软重置为先前的版本,而不是您尝试重置的版本
"Already up to date"
进行合并时。
如果您推送了更改,则可以使用undo
它,而无需使用另一个分支即可将文件移回舞台。
git show HEAD > patch
git revert HEAD
git apply patch
它将创建一个包含最后一个分支更改的补丁文件。然后,它还原更改。最后,将补丁文件应用于工作树。
rm patch
太多
对于这种情况:“尚未推送,仅已提交。” -如果您使用IntelliJ(或其他JetBrains IDE),但尚未推送更改,则可以继续。
做完了
这将“取消提交”您的更改,并将您的git状态返回到上一次本地提交之前的状态。您将不会丢失所做的任何更改。
git reset --soft "HEAD^"
在Windows上,顺便说一句。:)
对于我来说,大多数情况发生在我将更改推送到错误的分支并稍后实现时。在大多数情况下,后续工作都是如此。
git revert commit-hash
git push
git checkout my-other-branch
git revert revert-commit-hash
git push
请确保在运行这些命令之前在一个单独的文件夹中备份您的更改
git checkout branch_name
在您的分支结帐
git merge --abort
中止合并
git状态
中止合并后检查代码的状态
git reset --hard origin / branch_name
这些命令将重置您的更改,并使您的代码与branch_name(分支)代码对齐。