如何在GIT中重新连接分离的HEAD [英] How to reattach a detached HEAD in GIT

查看:317
本文介绍了如何在GIT中重新连接分离的HEAD的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我可以看到很多有关此问题,但我还没有找到能解决我确切问题的问题).

(I can see there are many questions about this but I haven't found one that solves my exact problem).

我正在运行gitlab-ci,当运行程序检出我的代码时,它作为分离的头部运行.这是在运行程序目录中运行git status命令时得到的信息.

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.

git status
# HEAD detached at 847fe59
nothing to commit, working directory clean

我需要做的是将这个头重新连接到我的开发分支,然后在完整存储库中按git pull以便在docker容器中使用.我猜gitlab ci只会签出最后一次提交,以保存克隆完整的仓库,这是可以理解的.

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.

在我的.gitlab-ci.yml文件中,我尝试了以下操作...

In my .gitlab-ci.yml file I've tried the following...

- 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.

有没有简单的方法可以重新固定头部?我见过的大多数解决方案都处理了一个事实,即已对分离的头部进行了更改,但对我而言并非如此.我只想在我的docker容器中获得完整的develop分支以及所有的git历史记录.

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.

或者是否有一种方法可以阻止gitlab ci检查分离的头部,这也很好.

Or if there is a way to stop gitlab ci from checking out the detached head that would also be great.

推荐答案

一个分离的HEAD 只是一个包含提交的原始哈希ID的HEAD.如评论中所述,将其用于构建系统通常是相当合理的,无论是否进行某种持续集成:您都可以通过哈希ID检出特定的提交,或检出标记名,但是无论哪种方式HEAD结束时包含提交哈希ID,现在保证是稳定的.

A detached HEAD is 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,那么用 Git 术语要做的就是运行git checkout <branch-name>.这会将分支的名称写入HEAD,现在HEAD附加到该分支.这意味着,它根本不是HEAD而是分支名称来确定哪个提交是当前的.任何更新分支名称的操作,都会更改当前提交.

If you do want to have an "attached" (not-detached) HEAD, though, all you have to do in Git terms 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.

请注意,此属性仅 适用于分支名称,即具有位于refs/heads/命名空间中的名称.名称origin/branch通常是refs/remotes/origin/branch的缩写,它不是分支名称.这是一个远程跟踪名称(有时称为远程跟踪分支,这是一个较差的词集,因为确定的声音类似于分支,不是吗?).向git checkout提供任何可以解析为提交的名称,但不是分支名称,则会导致产生分离的HEAD(如果结帐完全可行,无论如何) ).

Note that this property only applies 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 sounds like "branch", doesn't it?). Supplying any name to git checkout that can be resolved to a commit, but is not a branch name, results in a detached HEAD (if the checkout works at all, anyway).

如果要附加一个HEAD,则必须将其附加到分支名称上,即,其名称以refs/heads/开头的引用.

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/.

这篇关于如何在GIT中重新连接分离的HEAD的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆