如何删除我在本地 git 存储库中的最后一次提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2618989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:13:29  来源:igfitidea点击:

How can I remove my last commit in my local git repository

git

提问by michael

When I do a 'git pull', I have conflict with the head commit. So I did a 'git rebase --abort'

当我执行 'git pull' 时,我与头部提交发生冲突。所以我做了一个'git rebase --abort'

Can I 'save' my commit to a "patch" and then do a git pull?

我可以“保存”我对“补丁”的承诺,然后执行 git pull 吗?

What I want to emulate is:

我想效仿的是:

  • I did not commit, but I did a 'git stash' instead
  • Do a git pull to avoid any merge error
  • 我没有提交,但我做了一个“git stash”
  • 执行 git pull 以避免任何合并错误

So I need somehow to 'turn back the clock'. Is that possible with git?

所以我需要以某种方式“倒转时钟”。用git可以吗?

回答by Alexander Gro?

Your 7826b2patch will still cause a conflict when it's applied after pulling, but you can do the following:

7826b2拉取后应用您的补丁仍然会导致冲突,但您可以执行以下操作:

git reset --soft HEAD^
git stash
git pull
git stash pop # Will cause a conflict
git commit    # Re-commit 7826b2

Another workflow is also possible:

另一个工作流程也是可能的:

git reset --hard HEAD^
git pull
git cherry-pick 7826b2 # Will cause a conflict

The second workflow relies on the fact that Git keeps the 7826b2commit in the reflog (you can think of it as the recycle bin) even though you reset the changes it introduced with the first line.

第二个工作流程依赖于这样一个事实,即 Git 将7826b2提交保留在引用日志中(您可以将其视为回收站),即使您重置了它在第一行中引入的更改。

回答by Jamey Hicks

If you did commit your changes in your local repository, you could do a straight rebase:

如果您确实在本地存储库中提交了更改,则可以直接进行 rebase:

git fetch origin

git rebase origin/master

git 获取来源

git rebase 原点/主

This unwinds your local commits from the local master, applies new commits from origin/master, and then replays your local commits.

这会从本地 master 展开您的本地提交,应用来自 origin/master 的新提交,然后重放您的本地提交。

In your situation, this will result in a conflict and git will tell you which files need to be edited to resolve the conflict. After you do so, git addthose files and then git commitwith no arguments to recommit the changes.

在您的情况下,这将导致冲突,git 会告诉您需要编辑哪些文件来解决冲突。执行此操作后,git add这些文件将不git commit带任何参数重新提交更改。

回答by Thermech

First I suggest to do a backup of your branch:

首先我建议备份你的分支:

git branch your_branch_backup

Then the easiest way is:

那么最简单的方法是:

git reset --soft <commit> #Go back in time but without loosing your changes
git stash                 #Stash your changes
git pull                  #Refresh your branch with origin
git stash pop             #Re-apply your changes on the refreshed branch 

Then you can commit as usual

然后你可以像往常一样提交

回答by Pran

There is a way, in fact in git, I'm sure there's a multitude of ways of doing it...

有一种方法,实际上在 git 中,我敢肯定有很多方法可以做到...

You could do:

你可以这样做:

git stash     # Stash your changes
git pull      # Pull the changes
git stash pop # Pop your stash

You could do:

你可以这样做:

git pull --rebase # Bring in the changes and apply yours on top of it.

You could do:

你可以这样做:

git stash     # Stash your changes
git checkout <commit> # Pull a specific commit.
git stash pop # Pop your stash

Hope it helps.

希望能帮助到你。