是否有方法恢复提交,以便我的本地副本保留该提交中所做的更改,但它们在我的工作副本中成为未提交的更改?回滚提交会将您带到上一次提交—我想保留所做的更改,但我将它们提交到了错误的分支。

这并没有被推动,只是被承诺了。


当前回答

如果您推动了更改,则可以撤消更改并将文件移回后台,而无需使用其他分支。

git show HEAD > patch
git revert HEAD
git apply patch

它将创建包含最后分支更改的修补程序文件。然后恢复更改。最后,将补丁文件应用于工作树。

其他回答

2020简单方法:

git reset <commit_hash>

要保留的上次提交的提交哈希。

对我来说,大多数情况下,当我将更改推到错误的分支并在稍后实现时,就会发生这种情况。大多数情况下,以下内容都有效。

git revert commit-hash
git push

git checkout my-other-branch
git revert revert-commit-hash
git push

恢复提交(创建并)签出其他分支还原还原

如果您推动了更改,则可以撤消更改并将文件移回后台,而无需使用其他分支。

git show HEAD > patch
git revert HEAD
git apply patch

它将创建包含最后分支更改的修补程序文件。然后恢复更改。最后,将补丁文件应用于工作树。

撤消上次Git提交的最简单方法是使用以下选项之一执行Git reset命令

软的坚固的混合的

假设您添加了两次提交,并且希望撤消最后一次提交

$ git log --oneline

45e6e13 (HEAD -> master) Second commit
eb14168 Initial commit

–soft选项撤消上次提交并保留对文件所做的更改

$ git reset --soft HEAD~1


$ git status

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   file.html


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

–hard选项撤消上次提交并放弃工作目录和索引中的所有更改

$ git reset --hard HEAD~1


$ git status

nothing to commit, working tree clean


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

--混合选项撤消最后一次提交并将更改保留在工作目录中,但不保留在索引中

$ git reset --mixed HEAD~1


$ git status

On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   file.html

no changes added to commit (use "git add" and/or "git commit -a")


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

请确保在单独的文件夹中运行这些命令之前备份您的更改

git签出分支名称

在分支机构结账

git merge—中止

中止合并

git状态

中止合并后检查代码的状态

git reset--硬原点/分支名称

这些命令将重置您的更改,并将代码与branchname(branch)代码对齐。