简体   繁体   English

如何在 GIT 中重新连接分离的 HEAD

[英]How to reattach a detached HEAD in GIT

(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.我正在运行 gitlab-ci,当运行器检查我的代码时,它会作为一个独立的头来检查。 Here is what I get when running a git status command in the runners directory.这是我在 runners 目录中运行git status命令时得到的结果。

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.对于我正在做的事情,我需要做的是将这个头重新连接到我的开发分支,然后git pull入完整的 repo 以在 docker 容器中使用。 I guess gitlab ci only checks out the last commit to save cloning down the full repo which is understandable.我猜 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...在控制台中给出以下 output...

    $ 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.我只想在我的 docker 容器中获得我的完整develop分支,其中包含我所有的 git 历史记录。

Or if there is a way to stop gitlab ci from checking out the detached head that would also be great.或者,如果有一种方法可以阻止 gitlab ci 检查分离的头,那也很好。

A detached HEAD is simply a HEAD containing the raw hash ID of a commit.分离的 HEAD只是一个包含提交的原始哈希 ID 的 HEAD。 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.正如评论中所指出的,将它用于构建系统通常是非常合理的,无论是否是某种持续集成:您可以通过哈希 ID 检查特定提交,或检查标记名称,但无论哪种方式HEAD最终包含提交哈希 ID,现在保证稳定。

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> .但是,如果您确实想要一个“附加”(未分离)的 HEAD,那么在Git术语中您所要做的就是运行git checkout <branch-name> This writes the name of the branch into HEAD , and now HEAD is attached to that branch.这会将分支的名称写入HEAD ,现在HEAD附加到该分支。 This means that it's not HEAD at all, but rather the branch name, that determines which commit is current.这意味着它根本不是HEAD ,而是分支名称,它决定了哪个提交是当前的。 Anything that updates the branch name, changes the current commit.任何更新分支名称的内容都会更改当前提交。

Note that this property only applies to branch names, ie, with names that live in the refs/heads/ name-space.请注意,此属性适用于分支名称,即名称位于refs/heads/命名空间中的名称。 The name origin/branch is typically shorthand for refs/remotes/origin/branch , which is not a branch name;名称origin/branch通常是refs/remotes/origin/branch简写,它不是分支名称; 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).git checkout提供任何可以解析为提交但不是分支名称的名称,会导致分离的 HEAD(如果 checkout 可以正常工作,无论如何)。

If you want to have an attached HEAD, it must be attached to a branch name, ie, a reference whose name starts with refs/heads/ .如果你想有一个附加的 HEAD,它必须附加到一个分支名称,即名称以refs/heads/开头的引用。

This worked for me这对我有用

Command:命令:

git checkout FETCH_HEAD

Output:输出:

Previous HEAD position was 3cf5de5... Initial commit
HEAD is now at 600ea51... Sample Git Demo
#git status

HEAD detached at v1.0

nothing to commit, working tree clean

📌 Use the following command to attach HEAD with the master branch ( I have master branch, you can use any branch you have (use git branch to know your branch name) 📌 使用以下命令将HEAD附加到master分支(我有master分支,您可以使用任何分支(使用git branch知道您的分支名称)

#git checkout master

Switched to branch 'master'

#git status

On branch master

nothing to commit, working tree clean

i had about the same situation: you can always create a new branch and merge it with the branch you where detached from我遇到了大致相同的情况:您始终可以创建一个新分支并将其与您分离的分支合并

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

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