graham.reeds
graham.reeds

Reputation: 16486

Checkout a git subdirectory into a particular folder

I would like to use the Sodium library in a project I am working on, but I need to have a bit of a practice and get up to speed with it. However I also want to work in my usual way of having all my dependencies under the folder ext/.

mkdir Test
cd Test
git init
git config core.sparsecheckout true
echo c++/ >> .git/info/sparse-checkout
git remote add -f sodium https://github.com/SodiumFRP/sodium.git
git pull sodium master

This gives me just the c++/ folder in the root of Test which is not particularly helpful. I would prefer it to be in the ext/ folder and be named something other than c++. Renaming the folder causes it to be seen as untracked which is undesirable as I can achieve that just by copying the files from a zip.

If I create an ext/ directory and do the pull command from within it the c++ folder is still created in the root, not where the pull command was performed.

Is there a way of pulling a specific branch and copying it to subdirectory of a different name?

Upvotes: 1

Views: 157

Answers (1)

martin
martin

Reputation: 3249

Here is how I would do that:

Suppose you have a main project repository called main:

# for the sake of completeness
git init main 
cd main

Now, add a folder for your dependencies and make it a sparse checkout:

# make and change to directory
mkdir ext
cd ext

# sparse checkout of c++ directory only
git init
git config core.sparsecheckout true
echo c++/ >> .git/info/sparse-checkout

# add remote repository and check it out
git remote add -f sodium https://github.com/SodiumFRP/sodium.git
git pull sodium master

You then have a sparse checkout which you add as a submodule:

cd .. # go back to main repository

# add and commit submodule
git submodule add ./ext
git commit -m "Added submodule"

I tested that with git 1.9.1.

If you like, you can add another directory as to have a layout like:

main
  + ext        # multiple external dependencies
  |  + sodium
  |     + .git
  |     + c++
  + src        # main project source files

Upvotes: 1

Related Questions