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

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

问题描述

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

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

我正在运行 gitlab-ci,当运行程序检查我的代码时,它会作为一个独立的头执行此操作.这是我在 runners 目录中运行 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

我需要做的是将这个头重新附加到我的开发分支,然后在完整的 repo 中将 git pull 重新附加到 docker 容器中.我猜 gitlab ci 只检查最后一次提交以保存克隆完整的 repo,这是可以理解的.

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 的简写,它不是分支名称;它是一个 remote-tracking name(有时称为 remote-tracking branch,这是一组糟糕的词,因为这确实听起来类似于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天全站免登陆