git 回到一个特定的提交然后回到现在

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

go back to a specific commit then go back to the present

git

提问by Jürgen Paul

I'd like to see the file from a specific commit in the past, I'm thinking of git reset --hardbut 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?

我想查看过去特定提交中的文件,我正在考虑,git reset --hard但我将无法看到超出该提交的提交。我如何回到提交(连同文件的状态),查看文件,再回到现在?

回答by Mark Longair

Suppose your current branch is masterand the old commit is a1b2c3, then you can change all the files in your working tree back to the old commit with:

假设您当前的分支是master并且旧的提交是a1b2c3,那么您可以将工作树中的所有文件更改回旧的提交:

git checkout a1b2c3

... and return to masterwith:

...并返回master

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.

这种在 git 历史记录中跳转的方式(即使用其对象名称检查提交,也称为其哈希或 SHA1sum)对于查找以前的良好提交非常有用git bisect,例如,因为它不会移动您的分支.

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 HEADis detached, they won't move a branch forward.

要记住的一件事是,在执行此操作时,您可能会收到一个可能令人困惑的警告:如果您从其对象名称 ( a1b2c3) 中检出一个提交,这将使您进入一种称为“分离的 HEAD”的状态,其中HEAD,通常代表你当前的分支,而不是直接指向一个特定的提交。这不是什么值得担心的 - 它对于在你的历史中移动非常有用 - 但这确实意味着如果你在HEAD分离时创建新的提交,它们不会向前移动分支。



fork0points out in the comment below the potentially useful shortcut git checkout -, which will checkout the previous branch or commit that HEADpointed at.

fork0在下面的注释中指出了潜在有用的快捷方式git checkout -,它将检出HEAD指向的上一个分支或提交。