SirKumbskull
SirKumbskull

Reputation: 166

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?

Upvotes: 4

Views: 3642

Answers (2)

SirKumbskull
SirKumbskull

Reputation: 166

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.

Upvotes: 2

phd
phd

Reputation: 94473

Options -b requires a name of a branch in the repository https://my/repo. For example, master:

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

Git cannot "track head of the current branch of the super project". The branch must be given explicitly.

When you switch branch of the superproject with git checkout git doesn't automatically switch branches of submodules — you have to run git submodule update manually or from post-checkout hook.

See Why is git submodule update not automatic on git checkout?

https://stackoverflow.com/search?q=%5Bgit-submodules%5D+switch+branch

Upvotes: 0

Related Questions