1

I want to mimic behavior like svn:externals: I have a master project and I have some "common" code in another repository. With SVN I would do:

svn co <url>/src common

and my folder structure would be:

repo/common/*.cpp

Now with git + submodule + sparse checkout this is a lot more complicated and I have an extra subdirectory layer when I add the submodule;

git submodule add <url> common

now my folder structure is:

repo/common/src/*.cpp
repo/common/lib/...

with sparse checkout I can make sure only to checkout the src folder, but than I still have one more layer (src) compared to the svn:externals solution:

repo/common/src/*.cpp

is there a better solution for common code? Or a way to clean this up? I know that everything will be working; it's just not so clean...

Chris Maes
  • 35,025
  • 12
  • 111
  • 136

1 Answers1

1

You cannot avoid the extra folder from the submodule, but you can add a symbolic link (even on Windows) in order to find the content you want.

Add your submodule with its repo name:

git submodule add -b master /url/of/repo

Add a symlink:

ln -s repo/src common

Don't forget to add the '-b master' if you want your repo to follow its master branch.
This is possible since git 1.8.2: see "git submodule tracking latest".


Even the Git 2.25 and its git sparse-checkout command would not avoid the extra folder level "common".

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Wow thanks for the hint on the '-b master' option! I'll check that out further... – Chris Maes Jan 25 '14 at 08:01
  • @ChrisMaes yes, it is a *local* config you associate with your submodule. Any `git submodule update --remote` would then update the content of the submodule *after* fetching the submodule upstream branch. – VonC Jan 25 '14 at 08:03
  • Is this answer still correct, given recent changes to both submodules and sparse-checkout? – eggyal Aug 02 '20 at 09:51
  • @eggyal Yes it is. I have presented the Git 2.25 `git sparse-checkout` new command in https://stackoverflow.com/a/62617098/6309. But in the OP's context, you would still have an intermediate folder level for any submodule you would be using. – VonC Aug 02 '20 at 10:02
  • Okay, thanks. In my case I have a submodule that's concerned with only a tiny fraction of the whole repo, rather than just wanting to avoid an intermediate folder, but I guess the working set will have to include the full submodule for now. – eggyal Aug 02 '20 at 10:06
  • @eggyal you can still include only a subset of the submodule, but still at the cost of an extra folder, hence the symlink suggestion of this 2014 answer. – VonC Aug 02 '20 at 10:09
  • Ah, I'd misunderstood. Should first have read the answer to which you linked in your previous comment. Many thanks. – eggyal Aug 02 '20 at 10:12