回滚到公共存储库中的旧 Git 提交

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

Rollback to an old Git commit in a public repo

gitgit-checkoutgit-revert

提问by David

How can I go about rolling back to a specific commit in git?

我怎样才能回滚到git 中的特定提交?

The best answer someone could give me was to use git revertX times until I reach the desired commit.

有人能给我的最佳答案是使用git revertX 次,直到我达到所需的提交。

So let's say I want to revert back to a commit that's 20 commits old, I'd have to run it 20 times.

所以假设我想恢复到 20 次提交之前的提交,我必须运行它 20 次。

Is there an easier way to do this?

有没有更简单的方法来做到这一点?

I can't use reset because this repository is public.

我无法使用重置,因为此存储库是公开的。

回答by Alex Reisner

Try this:

尝试这个:

git checkout [revision] .

where [revision]is the commit hash (for example: 12345678901234567890123456789012345678ab).

[revision]提交哈希在哪里(例如:)12345678901234567890123456789012345678ab

Don't forget the .at the end, very important. This will apply changes to the whole tree. You should execute this command in the git project root. If you are in any sub directory, then this command only changes the files in the current directory. Then commit and you should be good.

不要忘记.最后,非常重要。这将对整个树应用更改。您应该在 git 项目根目录中执行此命令。如果您在任何子目录中,则此命令仅更改当前目录中的文件。然后提交,你应该很好。

You can undo this by

您可以通过以下方式撤消此操作

git reset --hard 

that will delete all modifications from the working directory and staging area.

这将从工作目录和暂存区中删除所有修改。

回答by Naga Kiran

To rollback to a specific commit:

回滚到特定提交:

git reset --hard commit_sha

To rollback 10 commits back:

回滚 10 次提交:

git reset --hard HEAD~10

You can use "git revert" as in the following post if you don't want to rewrite the history

如果您不想重写历史记录,可以使用“git revert”,如下面的帖子所示

How to revert Git repository to a previous commit?

如何将 Git 存储库恢复到以前的提交?

回答by Ben

Well, I guess the question is, what do you mean by 'roll back'? If you can't resetbecause it's public and you want to keep the commit history intact, do you mean you just want your working copy to reflect a specific commit? Use git checkoutand the commit hash.

好吧,我想问题是,“回滚”是什么意思?如果你不能reset因为它是公开的并且你想保持提交历史完整,你的意思是你只是想让你的工作副本反映一个特定的提交?使用git checkout和提交哈希。

Edit: As was pointed out in the comments, using git checkoutwithout specifying a branch will leave you in a "no branch" state. Use git checkout <commit> -b <branchname>to checkout into a branch, or git checkout <commit> .to checkout into the current branch.

编辑:正如评论中指出的那样,在git checkout不指定分支的情况下使用将使您处于“无分支”状态。使用git checkout <commit> -b <branchname>结帐成一个分支,或者git checkout <commit> .结账进当前的分支。

回答by Ben

The original poster states:

原始海报指出:

The best answer someone could give me was to use git revertX times until I reach the desired commit.

So let's say I want to revert back to a commit that's 20 commits old, I'd have to run it 20 times.

Is there an easier way to do this?

I can't use reset cause this repo is public.

有人能给我的最佳答案是使用git revertX 次,直到我达到所需的提交。

所以假设我想恢复到 20 次提交之前的提交,我必须运行它 20 次。

有没有更简单的方法来做到这一点?

我不能使用重置,因为这个 repo 是公开的。

It's not necessary to use git revertX times. git revertcan accept a commit range as an argument, so you only need to use it once to revert a range of commits.For example, if you want to revert the last 20 commits:

没有必要使用git revertX 次。git revert可以接受提交范围作为参数,因此您只需使用它一次即可恢复提交范围。例如,如果您想恢复最近 20 次提交:

git revert --no-edit HEAD~20..

The commit range HEAD~20..is short for HEAD~20..HEAD, and means "start from the 20thparent of the HEAD commit, and revert all commits after it up to HEAD".

提交范围HEAD~20..是 的缩写HEAD~20..HEAD,意思是“从HEAD 提交的20父级开始,并将其后的所有提交还原到 HEAD”。

That will revert that last 20 commits, assuming that none of those are merge commits.If there are merge commits, then you cannot revert them all in one command, you'll need to revert them individually with

这将恢复最后 20 次提交,假设这些都不是合并提交。如果有合并提交,则您无法在一个命令中全部还原它们,您需要使用以下命令单独还原它们

git revert -m 1 <merge-commit>

Note also that I've tested using a range with git revertusing git version 1.9.0.If you're using an older version of git, using a range with git revertmay or may not work.

另请注意,我已经git revert使用 git 版本 1.9.0使用范围进行了测试。如果您使用的是旧版本的 git,则使用范围git revert可能会也可能不会起作用。

In this case, git revertis preferred over git checkout.

在这种情况下,git revert优先于git checkout

Note that unlike this answer that says to use git checkout, git revertwill actually remove any files that were added in any of the commits that you're reverting, which makes this the correct way to revert a range of revisions.

请注意,与此答案git checkout不同,使用,git revert将实际上删除在您正在还原的任何提交中添加的任何文件,这使其成为还原一系列修订的正确方法。

Documentation

文档

回答by Igor

Step 1:fetch list of commits:

第 1 步:获取提交列表:

git log

You'll get list like in this example:

你会得到这个例子中的列表:

[Comp:Folder User$ git log
commit 54b11d42e12dc6e9f070a8b5095a4492216d5320
Author: author <[email protected]>
Date:   Fri Jul 8 23:42:22 2016 +0300

This is last commit message

commit fd6cb176297acca4dbc69d15d6b7f78a2463482f
Author: author <[email protected]>
Date:   Fri Jun 24 20:20:24 2016 +0300

This is previous commit message

commit ab0de062136da650ffc27cfb57febac8efb84b8d
Author: author <[email protected]>
Date:   Thu Jun 23 00:41:55 2016 +0300

This is previous previous commit message
...

Step 2:copy needed commit hash and paste it for checkout:

第 2 步:复制所需的提交哈希并将其粘贴以进行结帐:

git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f

That's all.

就这样。

回答by jthill

git read-tree -um @ $commit_to_revert_to

will do it. It's "git checkout" but without updating HEAD.

会做的。它是“git checkout”,但没有更新 HEAD。

You can achieve the same effect with

你可以达到同样的效果

git checkout $commit_to_revert_to
git reset --soft @{1}

if you prefer stringing convenience commands together.

如果您更喜欢将方便的命令串在一起。

These leave you with your worktree and index in the desired state, you can just git committo finish.

这些使您的工作树和索引处于所需状态,您只需git commit完成即可。

回答by Karl Morrison

Want HEAD detached mode?

想要 HEAD 分离模式?

If you wish to rollback X time to a certain commit with a DETACHED HEAD (meaning you can't mess up anything), then by all means, use the following:

如果您希望使用 DETACHED HEAD 将 X 时间回滚到某个提交(意味着您不能搞砸任何事情),那么无论如何,请使用以下内容:

(replace X with how many commits you wish to go back)

(将 X 替换为您希望返回的提交次数)

git checkout HEAD~X

I.E. to go back one commit:

IE 返回一次提交:

git checkout HEAD~1

回答by Naved Ahmad

You can find the commit id related to each commit in the commits section of GitHub/BitBucket/Gitlab. Its very simple, suppose your commit id is 5889575then if you want to go back to this part in your code then you simply need to type

您可以在 GitHub/BitBucket/Gitlab 的提交部分找到与每个提交相关的提交 ID。它非常简单,假设您的提交 ID 是5889575那么如果您想回到代码中的这一部分,那么您只需要输入

git checkout 5889575 .

This will take you to that point of time in your code.

这会将您带到代码中的那个时间点。

回答by Donato

Let's say you work on a project and after a day or so. You notice one feature is still giving you errors. But you do not know what change you made that caused the error. So you have to fish previous working commits. To revert to a specific commit:

假设你在一个项目上工作一天左右。您注意到一项功能仍然会给您带来错误。但是您不知道您所做的哪些更改导致了错误。所以你必须钓鱼以前的工作提交。要恢复到特定提交:

git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 .

Ok, so that commit works for you. No more error. You pinpointed the issue. Now you can go back to latest commit:

好的,这样提交对你有用。没有更多的错误。你指出了问题。现在你可以回到最新的提交:

git checkout 792d9294f652d753514dc2033a04d742decb82a5 .

And checkout a specific file before it caused the error (in my case I use example Gemfile.lock):

并在导致错误之前签出特定文件(在我的情况下,我使用示例 Gemfile.lock):

git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 -- /projects/myproject/Gemfile.lock

And this is one way to handle errors you created in commits without realizing the errors until later.

这是处理您在提交中创建的错误的一种方法,而直到以后才意识到这些错误。

回答by ken

I'm not sure what changed, but I am unable to checkout a specific commit without the option --detach. The full command that worked for me was: git checkout --detach [commit hash]

我不确定发生了什么变化,但我无法在没有选项的情况下签出特定的提交--detach。对我有用的完整命令是: git checkout --detach [commit hash]

To get back from the detached state I had to checkout my local branch: git checkout master

为了从分离状态返回,我必须检查我的本地分支: git checkout master