Search code examples
gitgit-submodules

git: submodule tracking current branch


According to the git documentation, I should be able to run this command:

git submodule add -b . https://my/repo

And have a sub-module added, which will track head of the current branch of the super project.

Branch of repository to add as submodule. The name of the branch is recorded as submodule..branch in .gitmodules for update --remote. 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.

But when I do this, I get this error:

fatal: 'origin/.' is not a commit and a branch '.' cannot be created from it

I'm running git 2.21. Have I read the instructions completely wrong?


Solution

  • git submodule add doesn't seem to have any code that does any sort of checking for a dot. After the clone, it'll try and checkout the branch ".", which naturally doesn't exist.

    But, git submodule update --remote does have a check, and uses "." specially.

    To get this to work, you need to do:

    1. git submodule add -b master https://my/repo
    2. git submodule set-branch --branch . REMOTE_NAME
    3. git submodule update --remote
    4. Profit

    Every time you run an update on the super project it'll get the tip of the branch.

    Whether the documentation is just not clear, or a bug exists in adding a submodule, I'm not sure.

    Edit: updated with Akom's comment as it's easier than editing the .gitmodules file to add the dot in as the branch name manually.