简体   繁体   English

如何在不丢失更改的情况下取消提交最后未推送的 git 提交

[英]How to un-commit last un-pushed git commit without losing the changes

Is there a way to revert a commit so that my local copy keeps the changes made in that commit, but they become non-committed changes in my working copy?有没有办法恢复提交,以便我的本地副本保留在该提交中所做的更改,但它们在我的工作副本中变成未提交的更改? Rolling back a commit takes you to the previous commit - I want to keep the changes made but I committed them to the wrong branch.回滚提交会将您带到之前的提交 - 我想保留所做的更改,但我将它们提交到错误的分支。

This has not been pushed, only committed.这还没有被推动,只是被承诺了。

There are a lot of ways to do so, for example:有很多方法可以做到这一点,例如:

in case you have not pushed the commit publicly yet:如果你还没有公开推送提交:

git reset HEAD~1 --soft   

That's it, your commit changes will be in your working directory, whereas the LAST commit will be removed from your current branch.就是这样,您的提交更改将在您的工作目录中,而 LAST 提交将从您当前的分支中删除。 See git reset man参见git reset man


In case you did push publicly (on a branch called 'master'):如果您确实公开推送(在名为“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 :) )

revert commit normally and push恢复正常提交并推送

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

now if you want to have those changes as you local changes in your working copy ("so that your local copy keeps the changes made in that commit") - just revert the revert commit with --no-commit option:现在,如果您想在工作副本中进行本地更改时进行这些更改(“以便您的本地副本保留在该提交中所做的更改”) - 只需使用--no-commit选项还原还原提交:

git revert --no-commit 86b48ba (hash of the revert commit).

I've crafted a small example: https://github.com/Isantipov/git-revert/commits/master我制作了一个小例子: https : //github.com/Isantipov/git-revert/commits/master

The easiest way to undo the last Git commit is to execute the git reset command with one of the below options撤消最后一次 Git 提交的最简单方法是使用以下选项之一执行git reset命令

  • soft柔软的
  • hard难的
  • mixed混合的

Let's assume you have added two commits and you want to undo the last commit假设您添加了两个提交并且您想要撤消最后一个提交

$ git log --oneline

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

–soft option undo the last commit and preserve changes done to your files –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 option undo the last commit and discard all changes in the working directory and index –hard选项 撤消最后一次提交并丢弃工作目录和索引中的所有更改

$ git reset --hard HEAD~1


$ git status

nothing to commit, working tree clean


$ git log --oneline

eb14168 (HEAD -> master) Initial commit

--mixed option undo the last commit and keep changes in the working directory but NOT in the index --mixed选项撤消最后一次提交并将更改保留在工作目录中但不在索引中

$ 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

If you pushed the changes, you can undo it and move the files back to stage without using another branch.如果您推送更改,您可以undo它并将文件移回舞台,而无需使用其他分支。

git show HEAD > patch
git revert HEAD
git apply patch

It will create a patch file that contain the last branch changes.它将创建一个包含最后分支更改的补丁文件。 Then it revert the changes.然后它恢复更改。 And finally, apply the patch files to the working tree.最后,将补丁文件应用到工作树。

For the case: "This has not been pushed, only committed ."对于案例:“这还没有被推送,只有被提交。” - if you use IntelliJ (or another JetBrains IDE) and you haven't pushed changes yet you can do next. - 如果您使用IntelliJ (或其他 JetBrains IDE)并且您还没有推送更改,您可以执行下一步操作。

  1. Go to Version control window ( Alt + 9/Command + 9 ) - "Log" tab.转到版本控制窗口( Alt + 9/Command + 9 )-“日志”选项卡。
  2. Right-click on a commit before your last one.在最后一次提交之前右键单击提交。
  3. Reset current branch to here将当前分支重置到这里
  4. pick Soft (!!!)选择(!!!)
  5. push the Reset button in the bottom of the dialog window.按下对话窗口底部的重置按钮。

Done.完成。

This will "uncommit" your changes and return your git status to the point before your last local commit.这将“取消提交”您的更改并将您的 git 状态返回到您上次本地提交之前的点。 You will not lose any changes you made.您不会丢失所做的任何更改。

With me mostly it happens when I push changes to the wrong branch and realize later.对我来说,这种情况主要发生在我将更改推送到错误的分支并稍后意识到时。 And following works in most of the time.并且在大多数情况下跟随工作。

git revert commit-hash
git push

git checkout my-other-branch
git revert revert-commit-hash
git push
  1. revert the commit还原提交
  2. (create and) checkout other branch (创建和)结帐其他分支
  3. revert the revert还原还原

2020 simple way : 2020 简单方法:

git reset <commit_hash>

Commit hash of the last commit you want to keep.提交您要保留的最后一次提交的哈希值。

PLease make sure to backup your changes before running these commmand in a separate folder请确保在单独的文件夹中运行这些命令之前备份您的更改

git checkout branch_name git checkout 分支名称

Checkout on your branch在您的分行结帐

git merge --abort git merge --abort

Abort the merge中止合并

git status状态

Check status of the code after aborting the merge中止合并后检查代码的状态

git reset --hard origin/branch_name git reset --hard origin/branch_name

these command will reset your changes and align your code with the branch_name (branch) code.这些命令将重置您的更改并将您的代码与 branch_name (分支)代码对齐。

Adding steps I followed hoping that it's helpful for a beginner like me.添加我遵循的步骤,希望对我这样的初学者有所帮助。

Following picture shows the commits I have already pushed to the remote branch ' A ' in bitbucket.下图显示了我已经推送到 bitbucket 中的远程分支“ A ”的提交。 在此处输入图片说明

From these 5 commits, I want to keep the last 2 as it is, but the first 3 commits I want them pushed to another branch ' B '.从这 5 次提交中,我想保留最后 2 次提交,但前 3 次提交我希望它们推送到另一个分支“ B ”。

These are the steps I followed:这些是我遵循的步骤:

Inside branch ' A ':内部分支“ A ”:

  1. git revert <commit-hash> for each of the 3 commits. git revert <commit-hash>为 3 个提交中的每一个提交。 As an example, d4a3734 is the commit hash of the last commit in the picture.例如, d4a3734是图片中最后一次提交的提交哈希。 (If you want you can revert multiple commits at once - refer How to revert multiple git commits? ) (如果您愿意,可以一次还原多个提交 - 请参阅如何还原多个 git 提交?
  2. git push

After the push, this is how it looked like:-推送后,这是它的样子:-

在此处输入图片说明

Now, I only have the first 2 commits in my branch ' A ', which is what I wanted.现在,我的分支“ A ”中只有前 2 个提交,这正是我想要的。 Next, checkout to the branch wanted.接下来,结帐到想要的分行。 If it's a new branch, use git checkout -b <branchname> .如果是新分支,请使用git checkout -b <branchname> In my case, I did git checkout B .就我而言,我做了git checkout B

Inside branch ' B ':内部分支“ B ”:

I simply cherry-picked the commits I wanted to branch ' B '.我只是挑选了我想要分支“ B ”的提交。 In my case I did:就我而言,我做了:

git cherry-pick <commit-hash> 

for those 3 commits I reverted.对于我恢复的那 3 个提交。

(Again, as an example, git cherry-pick d4a3734 where d4a3734 is the commit hash of the last commit in the picture) (再次举个例子, git cherry-pick d4a3734其中d4a3734是图中最后一次提交的提交哈希)

New Update for fellow googlers from 2021 onwards.从 2021 年起面向谷歌员工的新更新。

Here is a modern way to undo your last commit if you are a VSCode user.如果您是 VSCode 用户,这里有一种现代方法可以撤消上次提交。

  1. Go to your Source Control tab on the left (shortcut: Ctrl + Shift + GG ).转到左侧的源代码管理选项卡(快捷键: Ctrl + Shift + GG )。
  2. press ... on the left of circlish update icon.按圆形更新图标左侧的... Refer to the screenshot below:请参考以下屏幕截图: 源代码控制截图
  3. Navigate to Commit, then to Undo Last Commit.导航到 Commit,然后导航到 Undo Last Commit。 Here is a screenshot:这是一个屏幕截图: 在此处输入图片说明

All it does is it restores your repository just as it was before you made the commit, with your changes untouched.它所做的只是恢复您的存储库,就像您提交之前一样,而您的更改不受影响。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM