如何删除未推送的 git 提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3197413/
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
How do I delete unpushed git commits?
提问by NullVoxPopuli
I accidentally committed to the wrong branch. How do I delete that commit?
我不小心提交到错误的分支。如何删除该提交?
回答by dbyrne
Delete the most recent commit, keeping the work you've done:
删除最近的提交,保留您所做的工作:
git reset --soft HEAD~1
Delete the most recent commit, destroying the workyou've done:
删除最近的提交,破坏你所做的工作:
git reset --hard HEAD~1
回答by Ashkan Sirous
I wonder why the best answer that I've found is only in the comments! (by Daenyth with 86 up votes)
我想知道为什么我找到的最佳答案只在评论中!(由 Daenyth 以 86 票赞成)
git reset --hard origin
This command will sync the local repository with the remote repository getting rid of every change you have made on your local. You can also do the following to fetch the exact branch that you have in the origin.
此命令会将本地存储库与远程存储库同步,从而消除您在本地所做的所有更改。您还可以执行以下操作来获取源中的确切分支。
git reset --hard origin/<branch>
回答by VonC
Don't delete it: for just one commit git cherry-pick
is enough.
不要删除它:只需一次提交git cherry-pick
就足够了。
But if you had severalcommits on the wrong branch, that is where git rebase --onto
shines:
但是如果你在错误的分支上有几次提交,那就是git rebase --onto
亮点:
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
回答by Hank Gay
Do a git rebase -i FAR_ENOUGH_BACK
and drop the line for the commit you don't want.
执行 agit rebase -i FAR_ENOUGH_BACK
并删除您不想要的提交的行。
回答by Alexander Gro?
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
并cherry-pick
承诺other-branch
git cherry-pick <sha-of-the-commit>
回答by Xennex81
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 reset --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".
您现在有一个应用了更改的分支,我们称之为“主题”。
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":
现在,您将创建主题分支的副本,然后将其变基到位于分支“转储”中的源代码转储:
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".
现在,您的更改将根据“转储”的起点重新应用到分支 topic_duplicate 中,但仅适用于自“主”以来发生的提交。因此,您自 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.
我想说的是,如果您想根据不同的祖先更新当前的“重复”分支,您必须首先通过执行git reset --hard <last-commit-to-retain>
或git branch -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:
樱桃采摘要容易得多:
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.
当您的主题重复分支被检出时。这将从当前副本中删除先前选择的提交,并且只需将“主题”中发生的所有更改重新应用到当前“转储”(不同的祖先)之上。在使用不同的“下游”主机来检查您的本地更改是否仍然适用于该主机的同时,将开发基于“真实”上游主机似乎是一种相当方便的方法。或者,您可以只生成一个差异,然后将其应用到任何 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
- reset 将使您的分支指向不同的提交(--hard 还会检查先前的提交,--soft 将添加的文件保留在索引中(如果您再次提交,则会提交)并且默认值 (--mixed) 不会检查之前的提交(擦除您的本地更改)但它会清除索引(尚未添加任何提交)
- 你可以强制一个分支指向不同的提交
- 您也可以在立即检查该提交的同时这样做
- 对当前分支中存在的提交进行 rebase
- 樱桃采摘意味着从不同的分支复制
Hope this helps someone. I was meaning to rewrite this, but I cannot manage now. Regards.
希望这可以帮助某人。我本来想重写这个,但我现在无法管理。问候。
回答by Atul Sureka
Following command worked for me, all the local committed changes are dropped & local is reset to the same as remote origin/master branch.
以下命令对我有用,所有本地提交的更改都被删除,本地被重置为与远程源/主分支相同。
git reset --hard origin
git reset --hard 原点