为什么我的GIT子模块HEAD与主控分离? [英] Why is my GIT Submodule HEAD detached from master?

查看:144
本文介绍了为什么我的GIT子模块HEAD与主控分离?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GIT子模块。在从服务器拉出更改后,多次将我的子模块头从主分支中分离出来。



为什么会发生这种情况?

我必须始终这样做:

  git branch 
git checkout master





Muchas Gracias

如何确保我的子模块始终指向主分支? >解决方案

就我个人而言,我讨厌这里的答案,指向可能会停止工作的外部链接,并检查我的答案这里 (除非问题是重复的) - 指向包含其他主题行之间的主题的问题,但总体上等于:我没有回答,请阅读文档。



回到问题:为什么会发生?



您描述的情况


在从服务器拉取更改后,很多时候我的子模块头被分开f rom master分支。


过度使用子模块子模块开始。我相信我是正确的,在我们的子模块的HEAD被分离的某个点上,我们都已经存在。


  • 原因:您的子模块没有跟踪任何或正确的分支。解决方案:确保您的子模块正在跟踪正确的分支


  $ cd< submodule-path> 
#如果master分支已经存在于本地:
#(从git docs - branch)
#-u< upstream>
#--set-upstream-to =<上游>
#设置< branchname>的跟踪信息,以便<上游>
#被认为是< branchname>的上游分支。
#如果没有< branchname>被指定,那么它默认为当前分支。
$ git branch -u< origin> /< branch> <分支>
#else:
$ git checkout -b< branch> --track< origin> /< branch>




  • 原因:您的父回购未配置为跟踪子模块科。解决方案:通过添加具有以下两个命令的新子模块,使您的子模块跟踪其远程分支。


    • 首先告诉git跟踪远程< branch>

    • 其次,你告诉git从远程更新你的子模块。 >



      $ git submodule add -b< branch> <库> [< submodule-path>] 
    $ git submodule update --remote




    • 如果您还没有像这样添加现有的子模块,您可以轻松解决这个问题:


      • 确保您的子模块已检出您希望跟踪的分支。




    $ cd< submodule-path>
    $ git checkout< branch>
    $ cd< parent-repo-path>
    #< submodule-path>在这里,路径相对于父回购根
    #无需开始路径分隔符
    $ git config -f .gitmodules子模块。< submodule-path> .branch< branch>

    然而,即使你已经配置你的子模块来跟踪正确的分支,你仍然可以找到自己在你的子模块得到 HEAD时,< commit-hash>分离的情况



    在常见情况下,现在已经修复了DETACHED HEAD,因为它与上面的配置问题之一有关。但请记住,您的父存储库现在不再管理​​您的子模块的状态(使用提交给父存储库的提交散列),因为您的子模块正在跟踪其自己的远程分支,这为故障创造了新的可能性。

    在您的父级以及子模块的路径中运行 $ git status ,以验证所有内容是否被正确跟踪,是最新的,然后运行 $ cd< parent-repo> git submodule update --remote 。正如你所看到的,如果你再次运行git status,那么现在一切都很好。



    为了证明只要所有东西都配置正确并且你不期望我们来看看下面的内容:

    $ cd <子模块,路径> #并修改你的子模块
    $ git add。
    $ git commit -m您的修改#假设您忘记将其推送到远程。
    $ cd< parent-repo-path>
    $ git status#您将获得
    您的分支与'< origin> /< branch>'保持同步。
    没有为commit提交的更改:
    修改:path / to / submodule(新提交)
    #通常您会将新提交哈希提交给您的父回购
    $ git add - A
    $ git commit -m更新的子模块
    $ git push< origin> <分支取代。
    $ git status
    您的分支是最新的< origin> /< branch>'。
    没有提交,工作目录干净
    #如果你现在更新子模块
    $ git submodule update --remote
    子模块路径'path / to / submodule':签出' commit-hash'
    $ git status#会再次显示(子模块有新的提交)
    $ cd< submodule-path>
    $ git status
    HEAD在< hash>处分离
    #因为你看到你是DETACHED,你很幸运,如果你现在发现
    #因为此时你只是要求git更新你的子模块
    #您的本地分支
    #因为您没有将子模块chage提交到远程。
    #在这里你可以简单地通过。 (在子模块路径中)
    $ git checkout< branch>
    $ git push< origin> /< branch>
    #将修复子模块和父级的状态,因为
    #你告诉已经是父级repo,子模块提交hash
    #来跟踪,所以你不会再看到它作为未跟踪的。

    但是,如果您已经在本地对子模块进行了一些更改并将其提交到远程,你执行'git checkout',Git通知你:

    $ git checkout< branch>
    警告:您将离开1次落后,未连接到您的任何分支:
    如果您想通过创建新分支来保留它,这可能是一个很好的时机:

    创建临时分支的建议选项可以很好,然后您可以合并这些分支等。在这种情况下,我个人只会使用 git cherry-pick< hash>

      $ git cherry-pick< hash> #哈希哪个git显示你与DETACHED HEAD 
    #相关如果你得到'error:could not apply ...'运行mergetool并修复冲突
    $ git mergetool
    $ git status#since your修改只是删除未跟踪的垃圾文件
    $ rm -rf< untracked junk file(s)>
    $ git commit#无参数
    #应该为您打开提交来自DETACHED HEAD的消息
    #只保存它或修改消息。
    $ git push< origin> <分支>
    $ cd< parent-repo-path>
    $ git add -A#或者只是unstaged子模块
    $ git commit -m已更新< submodule>
    $ git push< origin> <分支>

    尽管还有更多的情况可以让你的子模块进入DETACHED HEAD状态,但我希望你能理解现在再多一点如何调试你的特定情况。


    I am using GIT submodules. After pulling changes from server, many times my submodule head gets detached from master branch.

    Why does it happen?

    I have to always do:

    git branch
    git checkout master
    

    How can I make sure that my submodule is always pointing to master branch?

    Muchas Gracias

    解决方案

    Personally I hate answers here which direct to external links which may stop working over time and check my answer here (Unless question is duplicate) - directing to question which does cover subject between the lines of other subject, but overall equals: "I'm not answering, read the documentation."

    So back to the question: Why does it happen?

    Situation you described

    After pulling changes from server, many times my submodule head gets detached from master branch.

    This is a common case when one does not use submodules too often or has just started with submodules. I believe that I am correct in stating, that we all have been there at some point where our submodule's HEAD gets detached.

    • Cause: Your submodule is not tracking any or correct branch. Solution: Make sure your submodule is tracking the correct branch

    $ cd <submodule-path>
    # if the master branch already exists locally:
    # (From git docs - branch)
    # -u <upstream>
    # --set-upstream-to=<upstream>
    #    Set up <branchname>'s tracking information so <upstream>
    #    is considered <branchname>'s upstream branch.
    #    If no <branchname> is specified, then it defaults to the current branch.
    $ git branch -u <origin>/<branch> <branch>
    # else:
    $ git checkout -b <branch> --track <origin>/<branch>
    

    • Cause: Your parent repo is not configured to track submodules branch. Solution: Make your submodule track its remote branch by adding new submodules with the following two commands.
      • First you tell git to track your remote <branch>.
      • Second you tell git to update your submodule from remote.

        $ git submodule add -b <branch> <repository> [<submodule-path>]
        $ git submodule update --remote
    

    • If you haven't added your existing submodule like this you can easily fix that:
      • First you want to make sure that your submodule has the branch checked out which you want to be tracked.

        $ cd <submodule-path>
        $ git checkout <branch>
        $ cd <parent-repo-path>
        # <submodule-path> is here path releative to parent repo root
        # without starting path separator
        $ git config -f .gitmodules submodule.<submodule-path>.branch <branch>
    

    However, even if you have configured your submodule to track the correct branch, you still can find yourself in situations where your submodule gets HEAD detached at <commit-hash>

    In the common cases, you already have fixed by now your DETACHED HEAD since it was related to one of the configuration issues above. But remember, your parent repository is now not managing your submodule's state anymore (using a commit hash committed to the parent repository), since your submodule is tracking its own remote branch, which opens up new possibilities for failure.

    Run $ git status in your parent and also in the submodule's path to verify that everything is tracked correctly and everything is up to date, then run $ cd <parent-repo> and git submodule update --remote. As you see, if you would run git status again everything is just fine for now.

    To demonstrate that just when everything seems to be configured correctly and you don't expect to get DETACHED HEAD things can go wrong, let's take a look at the following:

    $ cd <submodule-path> # and make modification to your submodule
    $ git add .
    $ git commit -m"Your modification" # Let's say you forgot to push it to remote.
    $ cd <parent-repo-path>
    $ git status # you will get
    Your branch is up-to-date with '<origin>/<branch>'.
    Changes not staged for commit:
        modified:   path/to/submodule (new commits)
    # As normally you would commit new commit hash to your parent repo
    $ git add -A
    $ git commit -m"Updated submodule"
    $ git push <origin> <branch>.
    $ git status
    Your branch is up-to-date with '<origin>/<branch>'.
    nothing to commit, working directory clean
    # If you now update your submodule
    $ git submodule update --remote
    Submodule path 'path/to/submodule': checked out 'commit-hash'
    $ git status # will show again that (submodule has new commits)
    $ cd <submodule-path>
    $ git status
    HEAD detached at <hash>
    # as you see you are DETACHED and you are lucky if you found out now
    # since at this point you just asked git to update your submodule
    # from remote master which is 1 commit behind your local branch
    # since you did not push you submodule chage commit to remote. 
    # Here you can fix it simply by. (in submodules path)
    $ git checkout <branch>
    $ git push <origin>/<branch>
    # which will fix the states for both submodule and parent since 
    # you told already parent repo which is the submodules commit hash 
    # to track so you don't see it anymore as untracked.
    

    But if you managed to make some changes locally already for submodule and commited, pushed these to remote then when you executed 'git checkout ', Git notifies you:

    $ git checkout <branch>
    Warning: you are leaving 1 commit behind, not connected to any of your branches:
    If you want to keep it by creating a new branch, this may be a good time to do so with:
    

    The recommended option to create a temporary branch can be good, and then you can just merge these branches etc. However I personally would use just git cherry-pick <hash> in this case.

    $ git cherry-pick <hash> # hash which git showed you related to DETACHED HEAD
    # if you get 'error: could not apply...' run mergetool and fix conflicts
    $ git mergetool
    $ git status # since your modifications are staged just remove untracked junk files
    $ rm -rf <untracked junk file(s)>
    $ git commit # without arguments
    # which should open for you commit message from DETACHED HEAD
    # just save it or modify the message.
    $ git push <origin> <branch>
    $ cd <parent-repo-path>
    $ git add -A # or just the unstaged submodule
    $ git commit -m"Updated <submodule>"
    $ git push <origin> <branch>
    

    Although there are some more cases you can get your submodules into DETACHED HEAD state, I hope that you understand now a bit more how to debug your particular case.

    这篇关于为什么我的GIT子模块HEAD与主控分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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