Question

Solved Problem: to show external code dependency by submodules: Thanks to VonC!

Current Problem: to have a submodule without duplicate contents in two folders

A temporary solution to the current problem: to change the name of the folder to bin-github, since the name of the folder as a submodule is bin. This means I need to duplicate contents at my Home.

My HOME folder is dependent on ~/bin. I have files located at HOME at the repository Masi, while at ~/bin at the repo bin at Github. My relevant folder structure at Home:

~
|-- [drwxr-xr-x] bin
    | -- fileA
    ` -- folderA
    ...

I would like to know how to show make ~/bin a submodule of the Git at Masi.

How can you make ~/bin a submodule of my Git at ~/ ?


#3 Reply to VonC's comment:

My .gitmodules

[submodule "bin"]   
         path = bin
         url = git://github.com/masi/bin.git

I feel that I do not need to add anymore the submodule again, since I have it in my .gitmodules.

I run

Sam master $ git submodule update
Sam master $ git submodule foreach git pull
Sam master $ ls bin 

#2 Reply to VonC's answer:

I changed my .gitmodules to the following such that I do not have a duplicate copy of it in my home.

[submodule "bin"]
    path = bin
    url = git://github.com/masi/bin.git

This seems to be a different situation as I cannot pull the submodule-bin -folder similarly as above.

I get the following now at a fresh-cloned git-repo

Sam master $ git submodule git init
error: pathspec 'git' did not match any file(s) known to git.
error: pathspec 'init' did not match any file(s) known to git.
Did you forget to 'git add'?                          # I am not sure what this means
Sam master $ git submodule foreach git pull
Sam master $ git submodule update

I have the external repository in my .gitmodules. Why is it asking me to git add?


Reply to VonC's answer:

I run

git submodule add git://github.com/masi/bin.git bin-github-copy

because I cannot add a new repo with the same name as my existing folder bin.

My .gitmodules is

[submodule "bin-github-copy"]
    path = bin-github-copy
    url = git://github.com/masi/bin.git

I run

git clone git://github.com/masi/Sam.git

I get an emty bin -directory.

How can I get the content of bin?

Was it helpful?

Solution

First, do not forget that with Git, you can not "add" a directory, only the content of at least one file within that directory.

Then you have two choices:

  • submodules, in order for Masi to refer a specific commit of your [files within a] bin directory stored at GitHub (or to the latest commit of a branch, stored at GitHub)

  • subtree merge, in order to mix the two repositories together and more easily follow modifications to files within bin directly from the Masi repository. That may be simpler in your scenario.

See this answer for more on those 2 strategies.


On the submodules path, declaring the submodule is only half the work. You still have to initialize your submodule the first time by

git submodule git init

You need then pull your submodules:

git submodule foreach git pull

and then update your submodule

git submodule update

See also this practical example with Git submodules, part I and Git submodules, part II.

The .gitmodules file only stores the paths, but the commit version does get stored with the main project.
I created a project on GitHub to demonstrate what happens. Specifically look at this commit, which adds the submodule, to see what’s going on:

+Subproject commit 60be4b09f51d2560802ebd744893bb6f737ef57c
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top