44

I want to add a git submodule with different name like:

git submodule add --name foo [email protected]:ironsand/cookbook-foo.git

I wanted to create a git submodule directory named foo, but the repository are created with the name cookbook-foo.

Most likely I'm doing something wrong, but I don't know what was wrong.

How can I change the name of git submodule directory?

3 Answers 3

90

Don't conflate the path and the name of a submodule. You want to run

git submodule add [email protected]:ironsand/cookbook-foo.git foo/

instead. For more details, see the git-submodule man page; the relevant git-submodule syntax here is

git submodule [--name <name>] <repository> [<path>]

where...

  • <repository> is the URL of the new submodule's origin repository.
  • <path>, if specified, determines the name of the subdirectory (of the superproject's root directory) to receive the clone of the repo living at <repository>; if left unspecified, <path> defaults to the name of that repo.
  • <name> is the submodule's name, i.e. the name that appear in the corresponding submodule entry in the .gitmodules file; if left unspecified, <name> simply defaults to <path>.

Here is a toy example to fix ideas:

$ cd ~/Desktop
$ mkdir test
$ cd test
$ git init
$ git submodule add --name brutus https://github.com/bradfitz/gitbrute bradfitz_bruteforce
$ ls -a
.           .git            bradfitz_bruteforce
..          .gitmodules
$ cat .gitmodules
[submodule "brutus"]
    path = bradfitz_bruteforce
    url = https://github.com/bradfitz/gitbrute
1
  • 6
    Me, I "conflated" :)
    – Jono
    Commented May 7, 2021 at 8:48
1

In last versions of git (since 1.8.5) you can do a git mv as a second step if the command failed or you forgot to set the option. The .submodule file will automatically updated.

git mv cookbook-foo foo

Check this post for more information about git mv over submodules.

0

In my case at least the answer by @jubObs did not work. I had to add the -- separator before the URL and the path, as shown in the examples of the submodule command's help.

$ git submodule -h
usage: git submodule [--quiet] [--cached]
   or: git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]

So, for example:

git submodule add --name foo \
  -- [email protected]:ironsand/cookbook-foo.git \
  path/to/foo

For reference, my version of Git is 2.30.

$ git version
git version 2.30.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.