5

Is there any way to config Git to reject a merge if the action will result in a detached HEAD?

I know how to solve the detached HEAD, but I would rather have git to reject the merge-action to avoid the detached HEAD entirely.

The issue occurs when this happens:

  1. I am working on a develop branch in the main repo and I also comitted some changes in a submodule repository. The develop branch is updated to match the newest commit of the submodule.
  2. A colleague wants to merge develop into the master, but forgets to pull the submodule on master first. This results in a detached HEAD.
ThomasCle
  • 6,792
  • 7
  • 41
  • 81

2 Answers2

0

A submodule is always in detached head state because Git does not keep any information on which branch it has to be.

Git keep track of the submodule commit it is connected to because it is stored along with the commit information. When you merge two different branches that are connected to different submodule commits, Git cannot checkout a branch on the sub-module that's why you have a detached HEAD.

There is no solution to let your sub-modules attached to a branch.

A possible solution to your issue is:

https://github.com/kollerma/git-submodule-tools

nowox
  • 25,978
  • 39
  • 143
  • 293
0

Indeed, there is a possibility to track a branch in git. As seen in this answer you can add a submodule with tracking information.

Note:

git submodule add -b . [URL to Git repo];
                    ^^^

See git submodule man page:

A special value of . is used to indicate that the name of the branch in the submodule should be the same name as the current branch in the current repository.

In my project we use to track the same branch in the subrepo with the special . character. E.g. if your main repo is on development branch the submodule will track development branch. Same is for a main-main pair.

This <branch> will be written to your .gitmodules file as seen below.

[submodule "My_submodule"]
    path = <directory>
    url = <url of the submodule>
    branch = .

In your example, I assume you would use the master as the tracking branch.

zerocukor287
  • 555
  • 2
  • 8
  • 23