有没有办法恢复提交,以便我的本地副本保留在该提交中所做的更改,但它们在我的工作副本中变为未提交的更改?回滚提交会将您带到上一次提交 - 我想保留所做的更改,但我将它们提交到了错误的分支.
这没有被推动,只是承诺.
Isa*_*pov 797
有很多方法可以做到这一点,例如:
如果您还没有公开推送提交:
git reset HEAD~1 --soft
Run Code Online (Sandbox Code Playgroud)
就是这样,您的提交更改将在您的工作目录中,而LAST提交将从您当前的分支中删除.见git reset man
如果你做了推公开(在一个叫"主"分支):
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 :) )
Run Code Online (Sandbox Code Playgroud)
正常恢复提交并推送
git checkout master
git revert a8172f36 #hash of the commit you want to destroy
# this intorduces a new coomit (say, it's hash is 86b48ba) which removes changes, introduced in the coomit in question (but those changes are still visible in the history)
git push origin master
Run Code Online (Sandbox Code Playgroud)
现在,如果您希望在工作副本中进行本地更改时进行这些更改("以便本地副本保留在该提交中进行的更改") - 只需使用以下-no-commit选项恢复还原提交:
git revert --no-commit 86b48ba (hash of the revert commit).
Run Code Online (Sandbox Code Playgroud)
我精心设计了一个小例子:https://github.com/Isantipov/git-revert/commits/master
San*_*eva 43
撤消上次 Git 提交的最简单方法是git reset使用以下选项之一执行命令
假设您添加了两次提交,并且想要撤消最后一次提交
\n$ git log --oneline\n\n45e6e13 (HEAD -> master) Second commit\neb14168 Initial commit\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x93soft选项撤消最后一次提交并保留对文件所做的更改
$ git reset --soft HEAD~1\n\n\n$ git status\n\nOn branch master\nChanges to be committed:\n (use "git restore --staged <file>..." to unstage)\n modified: file.html\n\n\n$ git log --oneline\n\neb14168 (HEAD -> master) Initial commit\nRun Code Online (Sandbox Code Playgroud)\n\xe2\x80\x93hard选项撤消最后一次提交并放弃工作目录和索引中的所有更改
$ git reset --hard HEAD~1\n\n\n$ git status\n\nnothing to commit, working tree clean\n\n\n$ git log --oneline\n\neb14168 (HEAD -> master) Initial commit\nRun Code Online (Sandbox Code Playgroud)\n--mixed选项撤消最后一次提交并将更改保留在工作目录中但不保留在索引中
$ git reset --mixed HEAD~1\n\n\n$ git status\n\nOn branch master\nChanges not staged for commit:\n (use "git add <file>..." to update what will be committed)\n (use "git restore <file>..." to discard changes in working directory)\n modified: file.html\n\nno changes added to commit (use "git add" and/or "git commit -a")\n\n\n$ git log --oneline\n\neb14168 (HEAD -> master) Initial commit\nRun Code Online (Sandbox Code Playgroud)\n
Bek*_*eki 19
如果您是 VSCode 用户,这里有一种撤消上次提交的现代方法。
它所做的只是将您的存储库恢复为提交之前的状态,并且您的更改保持不变。
Ami*_*ein 11
如果您按下更改,则可以undo将文件移回舞台而不使用其他分支.
git show HEAD > patch
git revert HEAD
git apply patch
Run Code Online (Sandbox Code Playgroud)
它将创建一个包含最后一个分支更改的补丁文件.然后它还原更改.最后,将补丁文件应用于工作树.
Xys*_*Xys 11
2020年简单方法:
git reset <commit_hash>
Run Code Online (Sandbox Code Playgroud)
提交您要保留的最后一次提交的哈希值。
Edu*_*sov 10
对于案例:“这还没有被推送,只有被提交。” - 如果您使用IntelliJ(或其他 JetBrains IDE)并且您还没有推送更改,您可以执行下一步操作。
完毕。
这将“取消提交”您的更改并将您的 git 状态返回到您上次本地提交之前的点。您不会丢失所做的任何更改。
对我来说,大多数情况下,当我将更改推送到错误的分支并稍后意识到时,就会发生这种情况。并且在大多数情况下都可以进行以下操作。
git revert commit-hash
git push
git checkout my-other-branch
git revert revert-commit-hash
git push
Run Code Online (Sandbox Code Playgroud)