Git 重置为之前的提交

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

Git reset to previous commit

git

提问by Steven

I have three commits I made which I attempted to clean up some code. Anyways I managed to completely destroy what I was working on. And I want to delete the past three commits and return to a specific commit SHA1.

我做了三个提交,试图清理一些代码。无论如何,我设法完全摧毁了我正在做的事情。我想删除过去的三个提交并返回到特定的提交 SHA1。

How can I restore to that previous commit and delete the history of those 3 commits? (The history deleting part is not a big deal). These commits are pushed already, so I am a little lost.

如何恢复到之前的提交并删除这 3 次提交的历史记录?(历史删除部分没什么大不了的)。这些提交已经被推送了,所以我有点迷茫。

Thanks!

谢谢!

采纳答案by Pablo Fernandez

Since your commits are pushed remotely you need to remove them. I'll assume your branch is masterand it's pushed over origin.

由于您的提交是远程推送的,因此您需要删除它们。我会假设你的分支是master并且它被推过去了origin

You first need to remove masterfrom origin:

您首先需要masterorigin以下位置删除:

git push origin :master(note the colon)

git push origin :master(注意冒号)

Then you need to get masterto the status you want, I'll assume the commit hash is ABCDE:

然后你需要达到master你想要的状态,我假设提交哈希是ABCDE

git reset --hard ABCDE

git reset --hard ABCDE

Lastly, push masteragain:

最后,master再次推:

git push origin master

git push origin master

That's it! Note that if someone already downloaded your changes from originthis will screw them pretty much leaving their local repos unstable.

就是这样!请注意,如果有人已经从origin这里下载了您的更改,这将使他们的本地存储库变得不稳定。

回答by triad

Find the commit you want to reset to:

找到您要重置为的提交:

git log

Once you have the hash:

一旦你有了哈希:

git reset --hard <hash>

And to push onto the remote:

并推到遥控器上:

git push -f <remote> <branch>

回答by torek

The general idea in git is that you never delete a commit. You just leave them without a name. Commits that are neither named nor referenced by some other named commit, eventually go away on their own.

git 中的一般思想是永远不要删除提交。你只是让他们没有名字。既没有命名也没有被其他命名提交引用的提交,最终会自行消失。

For example, suppose you started with:

例如,假设您开始于:

$ git checkout my_work_branch
<do some editing>
$ git commit -m "attempt #1, so far so good"
<do more editing>
$ git commit -m "attempt #2, getting a little hazier"
<do more editing>
$ git commit -m "attempt #3, looking bleak now"

At this point git log --graph --decorate --pretty=oneline --abbrev-commitmight produce something like:

此时git log --graph --decorate --pretty=oneline --abbrev-commit可能会产生类似的东西:

* d97de0e (HEAD, my_work_branch) attempt #3, looking bleak now
* 9a3efe3 attempt #2, getting a little hazier
* 9e80936 attempt #1, so far so good
* a1d6424 here's where you started

What you have now is that the branch named my_work_branch(the name you gave earlier) "points to" commit d97de0e, which in turn points to 9a3efe3, which points to 9e80936, which points to a1d6424. (This is also where the special name HEAD points.)

您现在拥有的是名为my_work_branch(您之前给出的名称)的分支“指向”提交 d97de0e,它又指向 9a3efe3,它指向 9e80936,它指向 a1d6424。(这也是特殊名称 HEAD 所指的地方。)

You can move HEAD elsewhere with any old git checkout. But, here's the thing: you can alsomove the name my_work_branchto point to a1d6424 too:

您可以使用任何旧的git checkout. 但是,事情是这样的:您也可以将名称my_work_branch移至指向 a1d6424:

$ git reset --hard a1d6424

or

或者

$ git reset --hard HEAD~3  # use at most one of these

If you do this, you find that the name my_work_branchhas also been moved:

如果这样做,您会发现名称my_work_branch也被移动了:

$ git rev-parse my_work_branch
a1d6424e5afcda475910084720c9aa26e3528618

The commits you added are still there:

您添加的提交仍然存在:

$ git log d97de0e

will show it them to you:

将向您展示它们:

$ git log --graph --decorate --pretty=oneline --abbrev-commit d79de0e
* d97de0e attempt #3, looking bleak now
* 9a3efe3 attempt #2, getting a little hazier
* 9e80936 attempt #1, so far so good
* a1d6424 (HEAD, my_work_branch) here's where you started

It's just that they no longer have any names, and if you do some work and git addand git commitit, that will be on a new branch named my_work_branch. The old one, that has the three extra commits, is now a "junk" branch. (If you decide that despite the bleakness you want them back, you need only give them a name before they expire in roughly 3 months. You'll have to find or remember that number, d97de0e in the above example, somehow.)

这只是他们不再有任何名字,如果你做了一些工作,并git addgit commit它,那将是一个名为新的分支my_work_branch。旧的,有三个额外的提交,现在是一个“垃圾”分支。(如果你决定,尽管你希望他们回来,但你只需要在他们大约 3 个月后到期之前给他们一个名字。你必须找到或记住这个数字,在上面的例子中是 d97de0e,不知何故。)



更新:啊,您已经推送了它们,并且希望它们从远程存储库中消失。

You don't have to delete the remote branch. You can use git push -fafter doing the rewind (git reset --hard) above. Just remember that anyone else who has fetched your pushed changes has them, and will continue to have them and can easily get confused by their presence. You'll have to alert any such people to beware of your "revoked" commits.

您不必删除远程分支。您可以git push -f在执行上述倒带 ( git reset --hard)后使用。请记住,获取您推送的更改的任何其他人都拥有它们,并且将继续拥有它们,并且很容易被它们的存在弄糊涂。你必须提醒任何这样的人提防你的“撤销”提交。

回答by Michael Cole

Ok, here are two solutions.

好的,这里有两个解决方案。

For either solution, git logand find the hash of the commit you want to go back to.

对于任一解决方案,git log并找到您要返回的提交的哈希值。

1) Revert your changes by automatically creating a patch to undo them.

1) 通过自动创建补丁来撤销您的更改。

What?Automatically create a reverse patch to undo your changes. Commit and push the patch. Your original changes are still in the git log, but they are "undone" with this reverse patch.

什么?自动创建反向补丁以撤消您的更改。提交并推送补丁。您的原始更改仍在 git 日志中,但通过此反向补丁它们已“撤消”。

How?git revert [hash] && git push

如何?git revert [hash] && git push

Why?Because you are a nice dev working with other nice devs and don't want to wreck their local repos.

为什么?因为你是一个优秀的开发者,与其他优秀的开发者一起工作,不想破坏他们的本地存储库。

Why not?Because it's harder to "merge" your changes later. The revert patch is "newer" than your changes, so it's harder than "git merge" to re-apply your changes.

为什么不?因为以后更难“合并”您的更改。恢复补丁比您的更改“更新”,因此重新应用您的更改比“git merge”更难。

2) Force push to a previous commit

2) 强制推送到之前的提交

What?A branch is a pointer to a commit. Hard reset that pointer to a previous commit, then force push that pointer to the server. Your changes (and anyone elses) are removed from the git history.

什么?分支是指向提交的指针。将该指针硬重置为之前的提交,然后将该指针强制推送到服务器。您的更改(以及其他任何人)将从 git 历史记录中删除。

How?git reset --hard [hash] && git push -f

如何?git reset --hard [hash] && git push -f

Why?Because you're a wild cowboy/cowgirl and your changes are in a feature branch you'd like to keep working on to easily merge later.

为什么?因为您是一个狂野的牛仔/女牛仔,并且您的更改位于一个功能分支中,您希望继续工作以便以后轻松合并。

Why not?Your reset and push will mess up any local repos for teammates that already pulled your changes (e.g. losing their commits, having to recommit work). If you haven't pushed (or they haven't pulled), not a problem.

为什么不?您的重置和推送将弄乱已经拉取您的更改的队友的任何本地存储库(例如丢失他们的提交,不得不重新提交工作)。如果你没有推(或者他们没有拉),那不是问题。

回答by Jayen Chondigara

use git revert

使用 git 恢复

-- you can revert to one, two or range of commit

-- 您可以恢复到一次、两次或提交范围

-- it will delete the commit history also

-- 它也会删除提交历史记录

1) git revert 175a25

1) git 回复 175a25

2) git status / git log (to confirm you have reverted)

2) git status / git log (确认你已经恢复了)

3) git push

3) git 推送

回答by Jaime Montoya

For example, if you want to get rid of the last two commits, you can do this:

例如,如果你想摆脱最后两次提交,你可以这样做:

git log -3
commit 7e83c9fa5dc1a1914847bfccfe9d2da14f845070
Author: Jaime Montoya <[email protected]>
Date:   Fri Sep 28 05:58:58 2018 -0600

    Add new background image to the header of the website.

commit b84b0c38df2f876d1c1f5657e6e452b5689c2d80
Author: Shannon Hall <[email protected]>
Date:   Thu Sep 27 21:35:48 2018 -0400

    Include code for Google Maps.

commit 40e82f46c1d523cb07abb6abe9f8c64f2b4fe0f7
Author: Jaime Montoya <[email protected]>
Date:   Thu Sep 27 18:57:45 2018 -0600

    Update links to copyright messages.

In this case, we are trying to get rid of these two commits: 7e83c9fa5dc1a1914847bfccfe9d2da13f845070and b84b0c38df2f876d1c1f5657e6e255b5689c2d80. We want to have this as our latest commit: 40e82f46c1d523cb07abb6abe9f8c64f2b4fe0f7. We need to do this:

在这种情况下,我们试图摆脱这两个提交:7e83c9fa5dc1a1914847bfccfe9d2da13f845070b84b0c38df2f876d1c1f5657e6e255b5689c2d80。我们希望有这个作为我们的最新承诺:40e82f46c1d523cb07abb6abe9f8c64f2b4fe0f7。我们需要这样做:

git reset --hard 40e82f46c1d523cb07abb6abe9f8c64f2b4fe0f7

This is one way to undo commits from your local repository.

这是从本地存储库撤消提交的一种方法。

After the above you push to your remote repository by using git push <remote> <branch>.

完成上述操作后,您可以使用git push <remote> <branch>.