如何在 GIT 中重新附加分离的 HEAD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47154114/
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
How to reattach a detached HEAD in GIT
提问by Remotec
(I can see there are many questions about this but I haven't found one that solves my exact problem).
(我可以看到有很多关于此的问题,但我还没有找到可以解决我的确切问题的问题)。
I'm running gitlab-ci and when the runner checks out my code it does so as a detached head. Here is what I get when running a git status
command in the runners directory.
我正在运行 gitlab-ci,当跑步者检查我的代码时,它作为一个独立的头这样做。这是我git status
在 runners 目录中运行命令时得到的结果。
git status
# HEAD detached at 847fe59
nothing to commit, working directory clean
What I need to do for what I am working on is to re-attach this head back to my develop branch and then git pull
in the full repo for use in a docker container. I guess gitlab ci only checks out the last commit to save cloning down the full repo which is understandable.
我需要为我正在做的事情做的是将这个头重新连接到我的开发分支,然后git pull
在完整的 repo 中用于 docker 容器。我猜 gitlab ci 只检查最后一次提交以保存克隆完整的 repo,这是可以理解的。
In my .gitlab-ci.yml
file I've tried the following...
在我的.gitlab-ci.yml
文件中,我尝试了以下...
- git checkout origin/$CI_BUILD_REF_NAME
- git pull
Which gives the following output in the console...
这在控制台中给出了以下输出......
$ git checkout $CI_BUILD_REF_NAME
Switched to a new branch 'develop'
Branch develop set up to track remote branch develop from origin.
$ git pull
You are not currently on a branch. Please specify which
branch you want to merge with. See git-pull(1) for details.
Is there an easy way to reattach the head? Most of the solutions I've seen deal with the fact a change has been committed onto the detached head however this isn't the case for me. I just want to get my full develop
branch in my docker container with all of my git history.
有没有简单的方法可以重新连接头部?我见过的大多数解决方案都处理这样一个事实,即已将更改提交到分离的头部,但对我而言并非如此。我只想develop
在我的 docker 容器中使用我所有的 git 历史记录来获取我的完整分支。
Or if there is a way to stop gitlab ci from checking out the detached head that would also be great.
或者,如果有一种方法可以阻止 gitlab ci 检查分离的头部,那也很棒。
回答by torek
A detached HEADis simply a HEAD containing the raw hash ID of a commit. As noted in the comments, it's generally pretty reasonable to use this for a build system, whether that's some sort of continuous integration or not: you might check out a specific commit by hash ID, or check out a tag name, but either way HEAD
winds up containing the commit hash ID and is now guaranteed to be steady.
甲分离HEAD是简单地含有一个提交的原始散列ID的HEAD。正如在评论中指出,这是一般非常合理使用这种用于构建系统,这是否是某种形式的持续集成或没有的:你可以检查出具体的承诺将通过哈希ID,或查看标签名,但方式无论是HEAD
风up 包含提交哈希 ID,现在保证稳定。
If you do want to have an "attached" (not-detached) HEAD, though, all you have to do in Gitterms is to run git checkout <branch-name>
. This writes the name of the branch into HEAD
, and now HEAD
is attached to that branch. This means that it's not HEAD
at all, but rather the branch name, that determines which commit is current. Anything that updates the branch name, changes the current commit.
但是,如果您确实想要一个“附加”(未分离)的 HEAD,那么在Git术语中您所要做的就是运行git checkout <branch-name>
. 这将分支的名称写入HEAD
,现在HEAD
附加到该分支。这意味着它根本不是HEAD
决定哪个提交是当前提交的,而是分支名称。任何更新分支名称的内容都会更改当前提交。
Note that this property onlyapplies to branch names, i.e., with names that live in the refs/heads/
name-space. The name origin/branch
is typically shorthand for refs/remotes/origin/branch
, which is not a branch name; it's a remote-tracking name(sometimes called a remote-tracking branch, which is a poor set of words because that sure soundslike "branch", doesn't it?). Supplying any name to git checkout
that canbe resolved to a commit, but is nota branch name, results in a detached HEAD (if the checkout works at all, anyway).
请注意,此属性仅适用于分支名称,即名称位于refs/heads/
命名空间中的名称。该名称origin/branch
通常是 的简写refs/remotes/origin/branch
,它不是分支名称;这是一个远程跟踪名称(有时称为远程跟踪分支,这是一组糟糕的词,因为它听起来确实像“分支”,不是吗?)。提供任何名字git checkout
是可以解决的承诺,而不是一个分支的名字,结果在一个分离的头(如果退房在所有工作,反正)。
If you want to have an attached HEAD, it must be attached to a branch name, i.e., a reference whose name starts with refs/heads/
.
如果要附加 HEAD,则必须将其附加到分支名称,即名称以refs/heads/
.
回答by Parag Jadhav
This worked for me
这对我有用
Command:
命令:
git checkout FETCH_HEAD
git checkout FETCH_HEAD
Output:
输出:
Previous HEAD position was 3cf5de5... Initial commit
HEAD is now at 600ea51... Sample Git Demo