在 Git 中丢弃本地提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5097456/
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 10:11:38  来源:igfitidea点击:

Throw away local commits in Git

git

提问by David Moles

Due to some bad cherry-picking, my local Git repository is currently five commits ahead of the origin, and not in a good state. I want to get rid of all these commits and start over again.

由于一些糟糕的挑选,我的本地 Git 存储库目前比原点提前 5 次提交,并且状态不佳。我想摆脱所有这些提交并重新开始。

Obviously, deleting my working directory and re-cloning would do it, but downloading everything from GitHub again seems like overkill, and not a good use of my time.

显然,删除我的工作目录并重新克隆就可以了,但是再次从 GitHub 下载所有内容似乎有点过分,而且不能很好地利用我的时间。

Maybe git revertis what I need, but I don't want to end up 10commits ahead of the origin (or even six), even if it does get the code itself back to the right state. I just want to pretend the last half-hour never happened.

也许git revert这就是我所需要的,但我不想在原点之前(甚至 6 个)提交10 次,即使它确实让代码本身恢复到正确的状态。我只想假装最后的半小时从未发生过。

Is there a simple command that will do this? It seems like an obvious use case, but I'm not finding any examples of it.

有没有一个简单的命令可以做到这一点?这似乎是一个明显的用例,但我没有找到任何例子。



Note that this question is specifically about commits, notabout:

请注意,这个问题专门针对commits而不是关于:

  • untracked files
  • unstaged changes
  • staged, but uncommitted changes
  • 未跟踪的文件
  • 未分阶段的变化
  • 分阶段但未提交的更改

回答by Ben Hymanson

If your excess commits are only visible to you, you can just do git reset --hard origin/<branch_name>to move back to where the origin is. This will reset the state of the repository to the previous commit, and it will discard all local changes.

如果您的多余提交仅对您可见,您只需 git reset --hard origin/<branch_name>移回原点即可。这会将存储库的状态重置为之前的提交,并且会丢弃所有本地更改。

Doing a git revertmakes newcommits to remove oldcommits in a way that keeps everyone's history sane.

做一个git revert使得提交删除的,保持每个人的历史理智的方式提交。

回答by Ramon Zarazua B.

Simply delete your local master branch and recreate it like so:

只需删除您的本地主分支并像这样重新创建它:

git branch -D master
git checkout origin/master -b master

回答by Anders Zommarin

Try:

尝试:

git reset --hard <the sha1 hash>

to reset your head to wherever you want to be. Use gitk to see which commit you want to be at. You can do reset within gitk as well.

将您的头重新调整到您想去的任何地方。使用 gitk 来查看您想要进行的提交。您也可以在 gitk 中进行重置。

回答by James L.

Delete the most recent commit:

删除最近的提交:

git reset --hard HEAD~1

git reset --hard HEAD~1

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

删除最近的提交,而不会破坏您所做的工作:

git reset --soft HEAD~1

git reset --soft HEAD~1

回答by muruge

If you are using Atlassian SourceTreeapp, you could use the reset option in the context menu.

如果您使用的是Atlassian SourceTree应用程序,则可以使用上下文菜单中的重置选项。

enter image description here

在此处输入图片说明

回答by parasrish

On your branch attempt:

在您的分支尝试中:

git reset --hard origin/<branch_name>

Validate the reversal (to the state, with no local commits), using "git log" or "git status" hence.

验证反转(到状态,没有本地提交),因此使用“ git log”或“ git status”。

回答by Kevin Chen

git reset --hard @{u}* deletes all your local changes on the current branch, including commits. I'm surprised no one has posted this yet considering you won't have to look up what commit to revert to or play with branches.

git reset --hard @{u}* 删除当前分支上的所有本地更改,包括提交。我很惊讶还没有人发布这个,因为您不必查找要恢复的承诺或使用分支。

* That is, reset to the current branch at @{upstream}—commonly origin/<branchname>, but not always

* 也就是说,重置到当前分支在@{upstream}—commonly origin/<branchname>,但不总是

回答by Nicholas

To see/get the SHA-1 id of the commit you want to come back too

要查看/获取您也想返回的提交的 SHA-1 id

gitk --all

To roll back to that commit

回滚到那个提交

git reset --hard sha1_id

!Note. All the commits that were made after that commit will be deleted (and all your modification to the project). So first better clone the project to another branch or copy to another directory.

!笔记。在该提交之后进行的所有提交都将被删除(以及您对项目的所有修改)。所以首先最好将项目克隆到另一个分支或复制到另一个目录。

回答by ElasticCode

Remove untracked files (uncommitted local changes)

删除未跟踪的文件(未提交的本地更改)

git clean -df

Permanently deleting all local commits and get latest remote commit

永久删除所有本地提交并获取最新的远程提交

git reset --hard origin/<branch_name>

回答by Robert

I had a situation where I wanted to remove a commit that wasn't pushed, but the commit was before another one. To do so, I've used the following command

我有一种情况,我想删除一个未推送的提交,但该提交在另一个提交之前。为此,我使用了以下命令

git rebase -i HEAD~2-> it will rebase the last two commits

git rebase -i HEAD~2-> 它将重新设置最后两次提交

And I used 'drop' for the commit signature that I wanted to remove.

我对要删除的提交签名使用了“drop”。