如何删除未推的git提交?

[英]How do I delete unpushed git commits?


I accidentally committed to the wrong branch. How do I delete that commit?

我不小心犯了错误。如何删除提交?

6 个解决方案

#1


1071  

Delete the most recent commit, keeping the work you've done:

删除最近的提交,保留你所做的工作:

git reset --soft HEAD~1

Delete the most recent commit, destroying the work you've done:

删除最近的承诺,毁掉你的工作:

git reset --hard HEAD~1

#2


46  

Don't delete it: for just one commit git cherry-pick is enough.

不要删除它:只要提交一次git就足够了。

But if you had several commits on the wrong branch, that is where git rebase --onto shines:

但是如果你在错误的分支上犯了几个错误,那就是git rebase的所在:

Suppose you have this:

假设你有:

 x--x--x--x <-- master
           \
            -y--y--m--m <- y branch, with commits which should have been on master

, then you can mark master and move it where you would want to be:

,然后你可以标记master移动到你想要的位置:

 git checkout master
 git branch tmp
 git checkout y
 git branch -f master

 x--x--x--x <-- tmp
           \
            -y--y--m--m <- y branch, master branch

, reset y branch where it should have been:

,复位y支,应在:

 git checkout y
 git reset --hard HEAD~2 # ~1 in your case, 
                         # or ~n, n = number of commits to cancel

 x--x--x--x <-- tmp
           \
            -y--y--m--m <- master branch
                ^
                |
                -- y branch

, and finally move your commits (reapply them, making actually new commits)

,最后移动您的提交(重新应用它们,进行实际的新提交)

 git rebase --onto tmp y master
 git branch -D tmp


 x--x--x--x--m'--m' <-- master
           \
            -y--y <- y branch

#3


9  

Do a git rebase -i FAR_ENOUGH_BACK and drop the line for the commit you don't want.

执行git rebase -i FAR_ENOUGH_BACK并删除您不想要的提交的行。

#4


6  

If you want to move that commit to another branch, get the SHA of the commit in question

如果您想要将该提交移动到另一个分支,请获取所涉及的提交的SHA

git rev-parse HEAD

Then switch the current branch

然后切换当前分支

git checkout other-branch

And cherry-pick the commit to other-branch

然后选择提交给其他分支

git cherry-pick <sha-of-the-commit>

#5


2  

For your reference, I believe you can "hard cut" commits out of your current branch not only with git reset --hard, but also with the following command:

作为参考,我相信您不仅可以通过git重置(hard)从当前分支中“硬切”提交,而且还可以通过以下命令:

git checkout -B <branch-name> <SHA>

In fact, if you don't care about checking out, you can set the branch to whatever you want with:

事实上,如果你不关心退房,你可以将分支设置为:

git branch -f <branch-name> <SHA>

This would be a programmatic way to remove commits from a branch, for instance, in order to copy new commits to it (using rebase).

这是一种从分支中删除提交的编程方式,例如,为了将新的提交复制到分支中(使用rebase)。

Suppose you have a branch that is disconnected from master because you have taken sources from some other location and dumped it into the branch.

假设您有一个与master断开连接的分支,因为您从其他位置获取了源并将其转储到分支中。

You now have a branch in which you have applied changes, let's call it "topic".

现在您有了一个应用了更改的分支,我们将其称为“topic”。

You will now create a duplicate of your topic branch and then rebase it onto the source code dump that is sitting in branch "dump":

现在,您将创建主题分支的副本,然后将其重新设置为位于分支“dump”中的源代码转储:

git branch topic_duplicate topic
git rebase --onto dump master topic_duplicate

Now your changes are reapplied in branch topic_duplicate based on the starting point of "dump" but only the commits that have happened since "master". So your changes since master are now reapplied on top of "dump" but the result ends up in "topic_duplicate".

现在,根据“dump”的起始点,您的更改被重新应用到branch topic_duplicate中,但只在“master”之后发生的提交。因此,由于master的更改现在在“dump”的顶部重新应用,但结果以“topic_duplicate”结束。

You could then replace "dump" with "topic_duplicate" by doing:

然后可以将“dump”替换为“topic_duplicate”,方法如下:

git branch -f dump topic_duplicate
git branch -D topic_duplicate

Or with

或与

git branch -M topic_duplicate dump

Or just by discarding the dump

或者直接丢弃转储

git branch -D dump

Perhaps you could also just cherry-pick after clearing the current "topic_duplicate".

也许您也可以在清除当前的“topic_duplicate”之后进行挑选。

What I am trying to say is that if you want to update the current "duplicate" branch based off of a different ancestor you must first delete the previously "cherrypicked" commits by doing a git reset --hard <last-commit-to-retain> or git branch -f topic_duplicate <last-commit-to-retain> and then copying the other commits over (from the main topic branch) by either rebasing or cherry-picking.

我想说的是,如果你想更新当前“复制”分支基于不同的祖先必须先删除以前“cherrypicked”提交通过git重置——< last-commit-to-retain >或git分支- f topic_duplicate < last-commit-to-retain >,然后复制其他提交(从主话题分支)垫底术或挑选。

Rebasing only works on a branch that already has the commits, so you need to duplicate your topic branch each time you want to do that.

rebase只在已经有提交的分支上工作,所以每次您想要这样做时,您需要重复您的主题分支。

Cherrypicking is much easier:

Cherrypicking要容易得多:

git cherry-pick master..topic

So the entire sequence will come down to:

所以整个序列会归结为

git reset --hard <latest-commit-to-keep>
git cherry-pick master..topic

When your topic-duplicate branch has been checked out. That would remove previously-cherry-picked commits from the current duplicate, and just re-apply all of the changes happening in "topic" on top of your current "dump" (different ancestor). It seems a reasonably convenient way to base your development on the "real" upstream master while using a different "downstream" master to check whether your local changes also still apply to that. Alternatively you could just generate a diff and then apply it outside of any Git source tree. But in this way you can keep an up-to-date modified (patched) version that is based on your distribution's version while your actual development is against the real upstream master.

当您的主题复制分支被检出时。这将从当前副本中删除以前选中的提交,并重新应用当前“dump”(不同的祖先)之上的“topic”中发生的所有更改。在使用不同的“下游”主机来检查您的本地更改是否仍然适用于此时,这似乎是将您的开发建立在“真正的”上游主机上的一种相当方便的方法。或者,您也可以生成一个diff,然后将其应用到任何Git源代码树之外。但通过这种方式,您可以保留基于发行版版本的最新修改(补丁)版本,而实际开发是针对真正的上游主版本的。

So just to demonstrate:

为了演示:

  • reset will make your branch point to a different commit (--hard also checks out the previous commit, --soft keeps added files in the index (that would be committed if you commit again) and the default (--mixed) will not check out the previous commit (wiping your local changes) but it will clear the index (nothing has been added for commit yet)
  • 重置将使您的分枝点到一个不同的提交(——硬还检查了之前的提交,软不断添加文件索引(如果你再次提交将提交)和默认的(混合)不会查看之前的提交(擦拭你的本地更改)但它会清除指数(没有添加提交)
  • you can just force a branch to point to a different commit
  • 您可以强制一个分支指向另一个提交
  • you can do so while immediately checking out that commit as well
  • 您可以在立即检出该提交的同时这样做
  • rebasing works on commits present in your current branch
  • 基于当前分支中的提交重新设置工作基础
  • cherry-picking means to copy over from a different branch
  • cherry-pick的意思是从另一个分支复制过来

Hope this helps someone. I was meaning to rewrite this, but I cannot manage now. Regards.

希望这可以帮助别人。我本来想重写这个,但现在不能。的问候。

#6


0  

If you have a backup of your code (including .git folder), delete the .git folder from your latest codebase. Copy old .git folder to latest codebase. Execute git pull.

如果您有代码的备份(包括.git文件夹),请从最新的代码基中删除.git文件夹。将老的.git文件夹复制到最新的代码基中。执行git拉。

Note: Please use this solution only if you are running out of time and are confused a lot.

注意:只有当你没有足够的时间并且很困惑的时候,请使用这个解决方案。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2010/07/07/bdbdf8c560331fb13079fb7b5810c577.html



 
粤ICP备14056181号  © 2014-2020 ITdaan.com