Pablo Guerrero
Pablo Guerrero

Reputation: 1084

How do I add a git submodule in a non empty directory?

I am trying to add a submodule in a non empty folder inside a repo with:

git submodule add repo_path.git non_empty_folder

I get the following message:

'non_empty_folder' already exists and is not a valid git repo

Is it possible to force the creation of the submodule? Thanks!

Upvotes: 2

Views: 1201

Answers (1)

Alexander Pogrebnyak
Alexander Pogrebnyak

Reputation: 45616

  • Create a new submodule repo outside your main project repository.
  • Rsync files from the directory which you want to make a submodule in your main project.
  • Commit these files into submodule repo and push it to origin.
  • git remove all files in the submodule directory in main project
  • Commit changes in main project
  • Now you are ready to create a submodule

A word of caution. I would highly suggest using a visual tool like SourceTree to help you manage submodules. It is an advanced feature of git and if not managed properly will give you plenty of grief. On a flip side, if used properly it is very powerful and can help you avoid code duplication.

Upvotes: 1

Related Questions