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

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

问题描述

我正在使用 Git 子模块.从服务器拉取更改后,我的子模块头多次与主分支分离.

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

为什么会这样?

我必须始终这样做:

git branch
git checkout master

如何确保我的子模块始终指向 master 分支?

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

推荐答案

参见 @Simba 答案有效解决方案

See @Simba Answer for valid solution

submodule..update 是你想要改变的,见 docs - 默认checkout
submodule..branch 指定要跟踪的远程分支 - 默认 master

submodule.<name>.update is what you want to change, see the docs - default checkout
submodule.<name>.branch specify remote branch to be tracked - default master

<小时>

旧答案:

我个人讨厌这里的答案指向外部链接,这些链接可能会随着时间的推移停止工作并检查我的答案 这里 (除非问题是重复的) - 指向的问题确实涵盖了其他主题之间的主题,但总体上等于:我没有回答,请阅读文档."

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

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

你描述的情况

从服务器拉取更改后,我的子模块头多次与主分支分离.

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

当人们不经常使用子模块或刚开始使用子模块时,这是一种常见情况.我相信我的说法是正确的,我们所有人都曾在某个时候出现过子模块的 HEAD 分离的情况.

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.

  • 原因:您的子模块没有跟踪正确的分支(默认主模块).
    解决方案:确保您的子模块正在跟踪正确的分支
$ 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>

  • 原因:您的父存储库未配置为跟踪子模块分支.
    解决方案:通过使用以下两个命令添加新的子模块,使您的子模块跟踪其远程分支.
    • 首先,您告诉 git 跟踪您的远程 .
    • 您告诉 git 执行变基或合并而不是结帐
    • 你告诉 git 从远程更新你的子模块.
    •     $ git submodule add -b <branch> <repository> [<submodule-path>]
          $ git config -f .gitmodules submodule.<submodule-path>.update rebase
          $ git submodule update --remote
      

      • 如果您还没有像这样添加现有的子模块,您可以轻松解决这个问题:
        • 首先,您要确保子模块已检出要跟踪的分支.
        •     $ 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>
              $ git config -f .gitmodules submodule.<submodule-path>.update <rebase|merge>
          

          在常见情况下,您现在已经修复了 DETACHED HEAD,因为它与上述配置问题之一有关.

          In the common cases, you already have fixed by now your DETACHED HEAD since it was related to one of the configuration issues above.

          .update = checkout

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

          但是,如果您已经设法在本地为子模块进行了一些更改并提交,请将这些更改推送到远程,然后当您执行git checkout"时,Git 会通知您:

          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:
          

          创建临时分支的推荐选项可能很好,然后您可以合并这些分支等.但是我个人在这种情况下只使用 git cherry-pick .

          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>
          

          尽管还有更多情况可以让您的子模块进入 DETACHED HEAD 状态,但我希望您现在对如何调试特定情况有了更多了解.

          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 与 master 分离?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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