回到特定的提交然后回到现在 [英] go back to a specific commit then go back to the present
问题描述
我想从过去的特定提交中看到该文件,我在考虑 git reset --hard
但我无法看到提交之后的提交。如何回到提交文件(连同文件的状态),查看文件,再次进入目前的状态?
master
,而旧的提交是 a1b2c3
,那么你可以改变你工作树中的所有文件恢复到旧的提交: git checkout a1b2c3
...并返回到 master
:
git checkout master
这样在你的git历史中跳跃(即检查一个提交的对象名称,也称为它的散列或SHA1sum)对于找到一个以前的良好提交 git bisect
,例如,因为它不会移动你的分支。
要记住的一件事是,当你这样做时你会得到一个可能令人困惑的警告:if你从它的对象名称( a1b2c3
)签出一个提交这会让你进入一个被称为分离HEAD的状态,其中 HEAD
通常表示你当前的分支,而不是直接指向特定的提交。这并不值得担心 - 它在你的历史中移动非常有用 - 但它确实意味着如果在 HEAD
被分离时创建新的提交,他们会赢得' t向前移动分支。
fork0 在下面的注释中指出了可能有用的快捷方式 git checkout -
,它将检出前一个分支或提交 HEAD
指出。
I'd like to see the file from a specific commit in the past, I'm thinking of git reset --hard
but I won't be able to see the commits beyond that commit. How do I go back to a commit (together with the state of the files), have a look at the files, the go to the present again?
Suppose your current branch is master
and the old commit is a1b2c3
, then you can change all the files in your working tree back to the old commit with:
git checkout a1b2c3
... and return to master
with:
git checkout master
This way of hopping about in your git history (i.e. checking out a commit with its object name, also known as its hash or SHA1sum) is very useful for finding a previous good commit for git bisect
, for example, since it won't move your branches.
One thing to bear in mind is that you'll get a possibly confusing warning when doing this: if you check out a commit from its object name (a1b2c3
) that will put you into a state known as "detached HEAD", where HEAD
, which usually represents your current branch, instead points directly to a particular commit. This isn't something to worry about - it's very useful for moving about in your history - but it does mean that if you create new commits when HEAD
is detached, they won't move a branch forward.
fork0 points out in the comment below the potentially useful shortcut git checkout -
, which will checkout the previous branch or commit that HEAD
pointed at.
这篇关于回到特定的提交然后回到现在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!