132

I am attempting to add a few submodules to my .vim/bundles directory, and when I attempt to add this particular repo Git gives me a strange error I've never seen before:

$ git submodule add -f git://github.com/derekwyatt/vim-scala.git .vim/bundle/vim-scala
fatal: You are on a branch yet to be born
Unable to checkout submodule '.vim/bundle/vim-scala'

Any idea what can cause this?

If I clone the same repo to a test directory (not through the submodule command), it works fine, and creates the expected files.

matt b
  • 138,234
  • 66
  • 282
  • 345

4 Answers4

231

To fix that error, you should delete the folder with the same path to the submodule inside .git/modules/ directory. This error can occurs when url was incorrect for submodule for the first-time when submodule was added.

lisachenko
  • 5,952
  • 3
  • 31
  • 51
  • 2
    Thanks, that did the trick. Was a bit non-obvious at first that since I was cloning to `.vim/bundle/vim-scala` the path to delete was `.git/modules/.vim/bundle/vim-scala` – matt b Aug 22 '12 at 14:17
  • 2
    If you're on windows note that the directory under .git/modules may not show up unless you `dir /AH` – oz10 Sep 15 '12 at 22:47
  • 13
    After I see delete that `.git/modules/path/to/submodule` directory and try to re-add the submodule, I receive the error _The following path is ignored by one of your .gitignore files: path/to/submodule_. – Drew Noakes Sep 18 '12 at 15:46
  • 1
    @Drew Noakes remove .vim/bundle/vim-scala created after first run of git submodule add – Piotr Król Feb 20 '13 at 14:01
  • On windows you can also use the Windows Explorer options (in the same place you would choose to see file extensions) to see the hidden folders in the GUI and delete them. – user83358 Jul 23 '13 at 20:46
  • If this the parent submodule is a submodule itself, the .git directory will be of the root module (the submodules will only have a .git file, not a directory). – Yoav Nov 07 '13 at 09:23
  • same result after deleting modules dir from .git and the dir created in parent project. Why should it be any different than when it failed the first time and those dirs were not there yet? – davidjmcclelland Aug 15 '15 at 16:41
  • If you are adding a submodule that consists of a shiny new empty repo like I was, you will go in circles until you do a commit to master (like a readme.md,) in addition to the removal of the directory mentioned here. The "branch yet to be born" is master. – davidjmcclelland Aug 15 '15 at 16:58
  • 1
    This, **together with Useless'** answer solved my problem. An ideal answer would be a combination of both. Thanks! – polynomial_donut Jun 02 '17 at 15:32
20

This error can happen if you are adding a submodule which doesn't have a master branch. If you want to use another branch when adding the submodule (develop for instance), you can use the following command:

git submodule add -b <branch> <repository>
k4nar
  • 301
  • 2
  • 6
  • 1
    In my case repo was newly created and therefore didn't have any files and branches (was empty), so `git submodule add -b master ` helped – vladkras Nov 17 '16 at 15:24
  • This was my problem my submodule had only 2 branches none of them is a `master` your solution worked for me. – Guerneen4 Mar 16 '17 at 14:10
13

You need to add a submodule inside an existing repo, that repo needs to be in a state to add & commit the submodule link, and the submodule repo itself must have a commit to check out.

Now, the submodule repo itself must be ok if you can create a regular clone elsewhere. However, it looks like submodule add complains if the repo is empty while clone does not. This guy suggests this is fixable by just running the same submodule add command again.

If the inner repo is not empty, check the repo you want to contain the submodule. Change to the same directory where you ran git submodule add, and run git status, and git branch to verify that your containing repo has at least one branch created and isn't in a weird state.

Useless
  • 64,155
  • 6
  • 88
  • 132
  • I've verified all of these things. When I check out the desired repo to another directory, it has many files, and a `master` branch. The repo I am trying to add this one to as a submodule is on the `master` branch, has no obvious problems (status returns some modified files, but that's all), and weirdest of all is that I can add other submodules just fine to this repository. – matt b Aug 09 '12 at 17:48
  • Also odd: after the `fatal` warning from my OP, git leaves an empty dir at `.vim/bundle/vim-scala`. I also noticed that even though `.gitmodules` is untouched, it added some lines referring to this github.com repo to `.git/config`, but after removing those I still get the same `fatal` error message. – matt b Aug 09 '12 at 17:49
  • 7
    +1 Argh! `This guy suggests this is fixable by just running the same submodule add command again.`. I've been battling this problem trying to find a sensible answer and this finally fixed it. This was for git 1.8.0 under MSYS while creating a submodule from BitBucket, so if you're in the same situation, try this first. – Aaron Newton Dec 28 '12 at 06:10
  • Adding twice worked for me, too. But I don't understand why. Any explanation? – Elliot Mar 01 '18 at 05:04
  • Adding twice here as well. I would love to understand why though – Vinicius Dantas Aug 05 '19 at 12:07
  • Probably because `git submodule add` is a compound operation: (clone if necessary and checkout the correct branch) and then add. On the first run, the clone succeeds but the checkout fails because there is no commit to find, yet. – Useless Aug 05 '19 at 12:13
0

As alluded to by @drew-noakes, this can be caused by attempting to add a submodule using a directory name that is listed in your .gitignore file.

John McFarlane
  • 5,528
  • 4
  • 34
  • 38