简体   繁体   English

“git 结帐<commit id> ”正在将分支更改为“无分支”

[英]“git checkout <commit id>” is changing branch to “no branch”

I am working on a branch in git.我正在 git 的一个分支上工作。 When I do当我做

git checkout <commit id>

(commit id obtained from git log ), it is getting committed to that particular change but branch is changed to <No-branch> . (从git log获得的提交 ID),它正在提交该特定更改,但分支更改为<No-branch>

Why is this happening?为什么会这样? How do you resolve this?你如何解决这个问题?

If you checkout a commit sha directly, it puts you into a "detached head" state, which basically just means that the current sha that your working copy has checked out, doesn't have a branch pointing at it.如果您直接checkout提交 sha,它会将您置于“分离头”状态,这基本上只是意味着您的工作副本已检出的当前 sha 没有指向它的分支。

If you haven't made any commits yet, you can leave detached head state by simply checking out whichever branch you were on before checking out the commit sha:如果您还没有进行任何提交,您可以通过在检查提交 sha 之前简单地检查您所在的任何分支来离开分离的头部状态:

git checkout <branch>

If you did make commits while you were in the detached head state, you can save your work by simply attaching a branch before or while you leave detached head state:如果您确实在处于 detached head 状态时进行了提交,则可以通过在离开 detached head 状态之前或同时附加一个分支来保存您的工作:

# Checkout a new branch at current detached head state:
git checkout -b newBranch

You can read more about detached head state at the official Linux Kernel Git docs for checkout .您可以在官方 Linux Kernel Git docs for checkout 中阅读有关分离头状态的更多信息。

By checking out to one of the commits in the history you are moving your git into so called 'detached state', which looks like is not what you want.通过检查历史记录中的提交之一,您将 git 移动到所谓的“分离状态”,这看起来不是您想要的。 Use this single command to create a new branch on one of the commits from the history:使用此单个命令在历史记录中的其中一个提交上创建一个新分支:

git checkout -b <new_branch_name> <SHA1>

If you are branch master and you do a git checkout <SHA>如果你是分支master并且你做了一个git checkout <SHA>

I'm fairly certain that this causes git to load that commit in a detached state, changing you out of the current branch.我相当肯定这会导致 git 以分离状态加载该提交,从而将您从当前分支中移出。

If you want to make changes you can and then you can do a git checkout -b <mynewbranch> to create a new branch based off that commit and any changes you have made.如果您想进行更改,您可以,然后您可以执行git checkout -b <mynewbranch>以根据该提交和您所做的任何更改创建一个新分支。

Is that commit in the other branch?那是在另一个分支中提交吗? Git checkout <commitid> will just switch over to the other branch if the commit has happened in the other branch.如果提交发生在另一个分支中,则Git checkout <commitid>只会切换到另一个分支。 You will want to merge the changes to your first branch if you want the code there.如果您想要那里的代码,您将希望将更改合并到您的第一个分支。

Other answers have explained what 'detached HEAD' means.其他答案已经解释了“分离头”的含义。 I try to answer why I want to do that.我试着回答我为什么要这样做。 There are some cases I prefer checkout a commit than checkout a temporary branch.在某些情况下,我更喜欢签出提交而不是签出临时分支。

  1. To compile/build at some specific commit (maybe for your daily build or just to release some specific version to test team), I used to checkout a tmp branch for that, but then I need to remember to delete the tmp branch after build.为了在某​​些特定提交时编译/构建(可能是为了您的日常构建,或者只是为了向测试团队发布一些特定版本),我曾经为此签出一个 tmp 分支,但是我需要记住在构建后删除 tmp 分支。 So I found checkout a commit is more convenient, after the build I just checkout to the original branch.所以我发现 checkout a commit 更方便,在构建之后我只是 checkout 到原始分支。

  2. To check what codes look like at that commit, maybe to debug an issue.要检查该提交时的代码是什么样的,也许是为了调试问题。 The case is not much different from my case #1, I can also checkout a tmp branch for that but then I need to remember delete it.这个案例与我的案例#1 没有太大不同,我也可以为此签出一个 tmp 分支,但我需要记住删除它。 So I choose to checkout a commit more often.所以我选择更频繁地检查提交。

  3. This is probably just me being paranoid, so I prepare to merge another branch but I already suspect I would get some merge conflict and I want to see them first before merge.这可能只是我偏执,所以我准备合并另一个分支,但我已经怀疑我会遇到一些合并冲突,我想在合并之前先看到它们。 So I checkout the head commit then do the merge, see the merge result.所以我检查头部提交然后进行合并,查看合并结果。 Then I git checkout -f to switch back to my branch, using -f to discard any merge conflict.然后我git checkout -f切换回我的分支,使用-f丢弃任何合并冲突。 Again I found it more convenient than checkout a tmp branch.我再次发现它比结帐 tmp 分支更方便。

This worked best for me when I wanted to check out the code, given the commit ID <commit_id_SHA1>考虑到提交 ID <commit_id_SHA1> ,当我想检查代码时,这对我来说效果最好

git fetch origin <commit_id_SHA1>
git checkout -b new_branch FETCH_HEAD

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM