Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add a submodule to a sub-directory?

I have a git repo in ~/.janus/ with a bunch of submodules in it. I want to add a submodule in ~/.janus/snipmate-snippets/snippets/, but when I run git submodule add <[email protected]:...> in the snipmate-snippets directory, I get the following error message:

You need to run this command from the toplevel of the working tree. 

So the question is: How do I add a submodule to the snipmate-snippets directory?

like image 920
Robert Audi Avatar asked Jan 27 '12 15:01

Robert Audi


People also ask

How do I initialize a git submodule?

If you already cloned the project and forgot --recurse-submodules , you can combine the git submodule init and git submodule update steps by running git submodule update --init . To also initialize, fetch and checkout any nested submodules, you can use the foolproof git submodule update --init --recursive .


2 Answers

You go into ~/.janus and run:

git submodule add <git@github ...> snipmate-snippets/snippets/ 

If you need more information about submodules (or git in general) ProGit is pretty useful.

like image 118
BergmannF Avatar answered Oct 20 '22 02:10

BergmannF


Note that starting git1.8.4 (July 2013), you wouldn't have to go back to the root directory anymore.

 cd ~/.janus/snipmate-snippets  git submodule add <git@github ...> snippets 

(Bouke Versteegh comments that you don't have to use /., as in snippets/.: snippets is enough)

See commit 091a6eb0feed820a43663ca63dc2bc0bb247bbae:

submodule: drop the top-level requirement

Use the new rev-parse --prefix option to process all paths given to the submodule command, dropping the requirement that it be run from the top-level of the repository.

Since the interpretation of a relative submodule URL depends on whether or not "remote.origin.url" is configured, explicitly block relative URLs in "git submodule add" when not at the top level of the working tree.

Signed-off-by: John Keeping

Depends on commit 12b9d32790b40bf3ea49134095619700191abf1f

This makes 'git rev-parse' behave as if it were invoked from the specified subdirectory of a repository, with the difference that any file paths which it prints are prefixed with the full path from the top of the working tree.

This is useful for shell scripts where we may want to cd to the top of the working tree but need to handle relative paths given by the user on the command line.

like image 25
VonC Avatar answered Oct 20 '22 01:10

VonC