使用 git Reset 转到较旧的变更集后,如何返回到历史记录中的最后一次提交?

发布于 2024-08-27 04:38:51 字数 204 浏览 6 评论 0原文

假设我的历史是这样的:

A - B - C - D(master)

如果我执行git reset B,我会得到:

A - B(master)

问题是,git log 现在只显示从 A 到 B 的历史记录,而我再也看不到 C 和 D 了。

我怎样才能回到D?

Suppose my history goes that way :

A - B - C - D (master)

If I do git reset B, I'll got :

A - B (master)

Trouble is, git log now show me only the history from A to B, and I can't see C and D anymore.

How can I go back to D ?

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

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

发布评论

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

评论(2

请止步禁区 2024-09-03 04:38:52

您应该能够使用 git reflog 看到 D

例如,请参阅这篇文章

唯一一次真正删除的提交是 git gc --prune (所以要小心那个!)。

如果您现在在您一直在使用的存储库中运行 git reflog,您将看到许多如下所示的更改:

c5c3a82... HEAD@{0}: pull origin featureB: Merge made by recursive.
49d0608... HEAD@{1}: reset --hard HEAD^: updating HEAD
3ed01b1... HEAD@{2}: pull origin featureA: Merge made by recursive.
49d0608... HEAD@{3}: pull origin bugfixJ: Merge made by recursive.
854d44e... HEAD@{4}: commit: Add more cowbell to foo.c
6dbc22d... HEAD@{5}: pull origin bugfixI: Merge made by recursive.
9bdb763... HEAD@{6}: commit: Remove weevils
8518f9d... HEAD@{7}: checkout: moving from wickedfeature to master

这些行可以分为 4 部分:

  • 提交哈希,
  • 提交指针,
  • 行动,
  • 以及额外信息。

如果我们想找回在 HEAD@{1} 处丢失的提交,我们只需git reset --hard HEAD@{2}即可。< br>
现在,我们当前的分支(和工作副本)在重置之前已设置为存储库状态。

如果我们只想查看该状态是什么,我们可以 git checkout -b temp HEAD@{2} (或者 git checkout HEAD@{2} 如果您有 git 1.5.0 及更高版本)。

You should be able to see D with git reflog.

See this article for instance.

The only time commits are actually deleted is if you git gc --prune (so be careful with that one!).

If you run git reflog right now in a repository you’ve been working in, you’ll see lots of changes that look something like this:

c5c3a82... HEAD@{0}: pull origin featureB: Merge made by recursive.
49d0608... HEAD@{1}: reset --hard HEAD^: updating HEAD
3ed01b1... HEAD@{2}: pull origin featureA: Merge made by recursive.
49d0608... HEAD@{3}: pull origin bugfixJ: Merge made by recursive.
854d44e... HEAD@{4}: commit: Add more cowbell to foo.c
6dbc22d... HEAD@{5}: pull origin bugfixI: Merge made by recursive.
9bdb763... HEAD@{6}: commit: Remove weevils
8518f9d... HEAD@{7}: checkout: moving from wickedfeature to master

These lines can be broken down into 4 parts:

  • commit hash,
  • commit pointer,
  • action,
  • and extra info.

If we wanted to get back the commit that was lost at HEAD@{1}, we could just git reset --hard HEAD@{2}.
Now our current branch (and working copy) are set to the repository state before we did the reset.

If we wanted to just see what that state was, we could git checkout -b temp HEAD@{2} (or git checkout HEAD@{2} if you have git 1.5.0 and up).

挽袖吟 2024-09-03 04:38:52

好的,找到了。

您可以使用git reflog

我不知道它的用途,但现在我可以看到它是 HEAD 指向的所有引用的日志。

Ok, found it.

You can use git reflog.

I didn't know what it was for, but now I can see it's a log of all the references HEAD have been pointing to.

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