在我使用git reset进入较旧的变更集之后,如何返回历史记录中的最后一次提交? [英] How to go back to the last commit in the history after I used git reset to go to an older changeset?
问题描述
假设我的历史记录是这样的:
Suppose my history goes that way :
A-B-C-D(主)
A - B - C - D (master)
如果我执行 git reset B
,我会得到:
If I do git reset B
, I'll got :
A-B(主)
麻烦的是, git log
现在仅向我显示从A到B的历史记录,而我再也看不到C和D.
Trouble is, git log
now show me only the history from A to B, and I can't see C and D anymore.
如何返回D?
推荐答案
You should be able to see D with git reflog
.
例如参见此文章.
唯一一次删除提交的时间就是git gc --prune(所以要小心!).
The only time commits are actually deleted is if you git gc --prune (so be careful with that one!).
如果您现在在一直在使用的存储库中运行git reflog,您将看到许多类似以下内容的更改:
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
这些行可以分为4部分:
These lines can be broken down into 4 parts:
- 提交哈希,
- 提交指针,
- 动作,
- 以及其他信息.
如果我们想找回在 HEAD @ {1}
丢失的提交,我们可以 git reset --hard HEAD @ {2}
.
现在,在进行重置之前,我们当前的分支(和工作副本)已设置为存储库状态.
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.
如果我们只想查看该状态是什么,我们可以 git checkout -b temp HEAD @ {2}
(或者 git checkout HEAD @ {2}
您拥有git 1.5.0及更高版本).
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).
这篇关于在我使用git reset进入较旧的变更集之后,如何返回历史记录中的最后一次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!