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

发布于 2024-08-16 20:42:21 字数 186 浏览 7 评论 0原文

我如何才能回滚到 git 中的特定提交?

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

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

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

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

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

The best answer someone could give me was to use git revert X 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 because this repository is public.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

挽容 2024-08-23 20:42:21

试试这个:

git checkout [revision] .

其中 [revision] 是提交哈希(例如:12345678901234567890123456789012345678ab)。

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

您可以通过

git reset --hard 

删除工作目录和暂存区域中的所有修改来撤消此操作。

Try this:

git checkout [revision] .

where [revision] is the commit hash (for example: 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.

You can undo this by

git reset --hard 

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

策马西风 2024-08-23 20:42:21

回滚到特定提交:

git reset --hard commit_sha

回滚 10 次提交:

git reset --hard HEAD~10

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

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

To rollback to a specific commit:

git reset --hard commit_sha

To rollback 10 commits back:

git reset --hard HEAD~10

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

How to revert Git repository to a previous commit?

木有鱼丸 2024-08-23 20:42:21

好吧,我想问题是,“回滚”是什么意思?如果您无法重置,因为它是公开的,并且您希望保持提交历史记录完整,您的意思是您只想让您的工作副本反映特定的提交吗?使用 git checkout 和提交哈希。

编辑:正如评论中所指出的,使用 git checkout 而不指定分支将使您处于“无分支”状态。使用 git checkout; -b检出到分支,或 git checkout. 检出到当前分支。

Well, I guess the question is, what do you mean by 'roll back'? If you can't reset because 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 checkout and the commit hash.

Edit: As was pointed out in the comments, using git checkout without 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.

方圜几里 2024-08-23 20:42:21

原海报写道:

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

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

有更简单的方法吗?

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

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

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

提交范围 HEAD~20..HEAD~20..HEAD 的缩写>,表示“从 HEAD 提交的第 20 个父级开始,并将其之后的所有提交恢复到 HEAD”。

这将恢复最后 20 次提交,假设这些提交都没有合并
如果存在合并提交,那么您无法在一个命令中将它们全部还原,您需要使用

git revert -m 1 <merge-commit>

单独还原它们。另请注意,我已经使用带有 < 的范围进行了测试。 code>git revert 使用 git 版本 1.9.0。 如果您使用的是较旧版本的 git,则使用 git revert 的范围可能有效,也可能无效。

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

请注意,与这个答案说使用git checkout不同,git恢复
实际上会删除您正在提交的任何提交中添加的所有文件
恢复,这使得这是恢复一系列修订的正确方法。

文档

The original poster states:

The best answer someone could give me was to use git revert X 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.

It's not necessary to use git revert X times. git revert can 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 revert --no-edit HEAD~20..

The commit range HEAD~20.. is short for HEAD~20..HEAD, and means "start from the 20th parent of the HEAD commit, and revert all commits after it up to 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

git revert -m 1 <merge-commit>

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

In this case, git revert is preferred over git checkout.

Note that unlike this answer that says to use git checkout, git revert
will 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.

Documentation

命硬 2024-08-23 20:42:21

第 1 步:获取提交列表:

git log

您将获得如下例所示的列表:

[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
...

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

git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f

就这样.

Step 1: fetch list of commits:

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:

git checkout fd6cb176297acca4dbc69d15d6b7f78a2463482f

That's all.

娇纵 2024-08-23 20:42:21

想要 HEAD 分离模式吗?

如果您希望使用 DETACHED HEAD 将 X 时间回滚到某个提交(意味着您不能弄乱任何东西),那么请务必使用以下命令:(

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

git checkout HEAD~X

IE返回一个提交:

git checkout HEAD~1

Want HEAD detached mode?

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:

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

git checkout HEAD~X

I.E. to go back one commit:

git checkout HEAD~1
南七夏 2024-08-23 20:42:21
git read-tree -um @ $commit_to_revert_to

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

则可以达到相同的效果

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

如果您更喜欢将便捷命令串在一起,

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

git read-tree -um @ $commit_to_revert_to

will do it. It's "git checkout" but without updating 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 commit to finish.

你是年少的欢喜 2024-08-23 20:42:21

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

要从分离状态返回,我必须签出我的本地分支:git checkout master

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]

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

那一片橙海, 2024-08-23 20:42:21

假设您在完成一个项目大约一天后。您注意到一项功能仍然给您带来错误。但您不知道您所做的更改导致了错误。所以你必须收集以前的工作提交。恢复到特定提交:

git checkout 8a0fe5191b7dfc6a81833bfb61220d7204e6b0a9 .

好的,所以该提交适合您。不再有错误。你指出了问题所在。现在您可以返回到最新的提交:

git checkout 792d9294f652d753514dc2033a04d742decb82a5 .

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

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

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

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):

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.

落日海湾 2024-08-23 20:42:21

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

git checkout 5889575 .

This will take you to that time point in your code。

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 5889575 then if you want to go back to this part in your code then you simply need to type

git checkout 5889575 .

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

奢欲 2024-08-23 20:42:21

这是一个执行此操作的示例

    cd /yourprojects/project-acme 


    git checkout efc11170c78 .

Here is an example to do that

    cd /yourprojects/project-acme 


    git checkout efc11170c78 .
温柔戏命师 2024-08-23 20:42:21

回滚 git 中的特定提交:

  • 取回旧的 git 提交:(ac2ec... 是提交名称)
git checkout ac2ece0219689ed86b08c93dfebb0d02c0f1d5b1
  • 命名一个您希望 HEAD 分离将指向的新分支:
git branch get_back_to_past
  • 签出到该分支
git checkout get_back_to_past

现在该分支具有过去的提交项。例如,如果您愿意,可以将其合并到母版中。

有关 git head 以及回滚到旧版本的更多信息:点击此处

to rollback a specific commit in git:

  • to get back an old git commit: (the ac2ec... is the commit name)
git checkout ac2ece0219689ed86b08c93dfebb0d02c0f1d5b1
  • name a new branch that you want the HEAD detach will point to:
git branch get_back_to_past
  • checkout to that branch
git checkout get_back_to_past

now that branch has the past commit items. you can merge it to the master for example if you would like to.

for more info about git head, and rollback to old version: click here

柠檬色的秋千 2024-08-23 20:42:21

在 Github 上找到您所做的提交并签出它。

git checkout <commit#>

示例: git checkout b29ce12

在此处输入图像描述

on the Github find the commit you made and checkout to it.

git checkout <commit#>

example: git checkout b29ce12

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文