git 回到 SourceTree 中的先前提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42523720/
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
Going back to previous commit in SourceTree
提问by Nikhil Sridhar
I am new to Git, and I was trying to revert back to a previous commit in SourceTree. I right clicked on the commit I was going to revert to and then clicked checkout. It gave me a prompt saying that my working copy would become a detached head. What does this mean and is this something I should avoid?
我是 Git 的新手,我试图恢复到 SourceTree 中的先前提交。我右键单击要还原的提交,然后单击结帐。它给了我一个提示,说我的工作副本将成为一个超然的头脑。这是什么意思,这是我应该避免的事情吗?
回答by LuFFy
As Per Git-Tower's Article : What's a "detached HEAD" in Git?
根据 Git-Tower 的文章:Git 中的“分离的 HEAD”是什么?
Understanding how "checkout" works
With the "git checkout" command, you determine which revision of your project you want to work on. Git then places all of that revision's files in your working copy folder.
Normally, you use a branch name to communicate with "git checkout"
$ git checkout development
However, you can also provide the SHA1 hash of a specific commit instead:
$ git checkout 56a4e5c08 Note: checking out '56a4e5c08'. You are in 'detached HEAD' state...
This exact state - when a specific commit is checked out instead of a branch - is what's called a detached HEAD.
The problem with a detached HEAD
The HEAD pointer in Git determines your current working revision (and thereby the files that are placed in your project's working directory). Normally, when checking out a proper branch name, Git automatically moves the HEAD pointer along when you create a new commit. You are automatically on the newest commit of the chosen branch.
When you instead choose to check out a commit hash, Git won't do this for you. The consequence is that when you make changes and commit them, these changes do NOT belong to any branch. This means they can easily get lost once you check out a different revision or branch: not being recorded in the context of a branch, you lack the possibility to access that state easily (unless you have a brilliant memory and can remember the commit hash of that new commit...).
了解“结帐”的工作原理
使用“git checkout”命令,您可以确定要处理项目的哪个版本。Git 然后将所有该修订的文件放在您的工作副本文件夹中。
通常,您使用分支名称与“git checkout”进行通信
$ git checkout development
但是,您也可以提供特定提交的 SHA1 哈希值:
$ git checkout 56a4e5c08 Note: checking out '56a4e5c08'. You are in 'detached HEAD' state...
这个确切的状态——当一个特定的提交被检出而不是一个分支时——就是所谓的分离 HEAD。
分离的 HEAD 的问题
Git 中的 HEAD 指针决定了您当前的工作修订版(以及放置在项目工作目录中的文件)。通常,在检查正确的分支名称时,Git 会在您创建新提交时自动移动 HEAD 指针。您将自动处于所选分支的最新提交上。
当您选择检出提交哈希时,Git 不会为您执行此操作。结果是,当您进行更改并提交时,这些更改不属于任何分支。这意味着一旦您检出不同的修订或分支,它们很容易丢失:没有记录在分支的上下文中,您无法轻松访问该状态(除非您有出色的记忆力并且可以记住那个新的提交......)。
Summary :From SourceTree, Kindly Checkout to Particular Branch Instead of Checking out to Particular Commit.
总结:从SourceTree,请检出到特定的分支,而不是检出到特定的提交。
回答by Suraj Muraleedharan
The issue seems not exactly related to git, but specific to the git client / provider you are using ( bitbucket I suspect).
该问题似乎与 git 并不完全相关,而是特定于您正在使用的 git 客户端/提供程序(我怀疑是 bitbucket)。
I would suggest you to use the command line client, instead of web UI to learn git better.
我建议您使用命令行客户端而不是 Web UI 来更好地学习 git。
In detached head state, whatever changes you make (and commit) gets detached from the commit tree, and you need additional work to put that commit back to the commit tree. Normally we don't make changes in detached head state, it is used for re-arranging the commit tree. But it is worth experimenting in detached state.
在分离头状态下,您所做的任何更改(和提交)都会从提交树中分离出来,您需要额外的工作才能将该提交放回提交树。通常我们不会在分离头状态中进行更改,它用于重新排列提交树。但在分离状态下值得尝试。