简体   繁体   中英

Is there a way to add a submodule to a non-empty directory

I am trying to add a common document (used by almost all of my products) to a folder that contains other documents necessary to my project. When I use git submodule add , I get the error that the directory is not empty, which is the point. If I add it any other way, it creates a new sub-directory which breaks all of my links to that file.

a submodule lives in its own directory, mainly because git does not store the directory but a "file" pointing to the current head of the submodule. This means that either you can add the file to the submodule, either you won't be able to use this directory for the submodule.

If I understood you correctly, your project X has a structure like:

- /
  +- docs/
     +- projectdoc1.txt
     +- projectdoc2.txt

.. and then you have a common project in another git repository which contains (at least) a single document you want to integrate to project X?

(Also I'm assuming you have a bash shell for executing commands.)

Assuming this, you could perhaps use symlinks to resolve your issue?

First, add the common project as a submodule in the root directory:

git submodule add my-common-repo.git

Now you have

- /
  +- docs/
  |  +- projectdoc1.txt
  |  +- projectdoc2.txt
  +- my-common-repo/
     +- commondoc.txt

Now, go to the docs subdirectory and create a link to the commondoc.txt:

cd docs/
ln -s ../my-common-repo/commondoc.txt

Then you can add the link just like a normal file:

git add commondoc.txt
git commit -m 'Add commondoc.txt symlink from my-common-repo'

That's it. Now you have:

- /
  +- docs/
  |  +- projectdoc1.txt
  |  +- projectdoc2.txt
  |  +- commondoc.txt
  +- my-common-repo/
     +- commondoc.txt

And whenever you update my-common-repo/ submodule, docs/commondoc.txt stays up-to-date.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM