Wednesday, February 7, 2018

How to checkout git repo subdirectory into current directory?

Leave a Comment

There is a remote repo with directory structure:

-directory1     -file1_1     -file1_2     ... -directory2     -file2_1     -file2_2     ... 

I have a folder on a web hosting with a custom name, say, "/path/public_html".

How do I set up git on the web hosting, so my "public_html" tracks a subdirectory "directory2" of a remote repo?

So, in other words, I want to execute some form of git command on the web hosting and update public_html to the latest content of "directory2". I don't care about pushing back to repo from web hosting, if it helps.

4 Answers

Answers 1

You cannot clone directly directory2 content, only directory2 and its content, meaning you always have a directory2/ folder on your disk.

If you want to see directory2 content in public_html (meaning not public_html/directory2/...), then you need a symlink.

That being said, you can still clone and checkout only directory2 folder, instead of cloning the full repo, as described in "Is it possible to do a sparse checkout without checking out the whole repository first?"

That is:

git init /a/path cd /a/path git config core.sparseCheckout true git remote add -f origin /url/remote/repo echo "directory2/" > .git/info/sparse-checkout git checkout [branchname] # ex: master 

That would give a a/path/directory2 folder, than your public_html can symlink to.

If having a directory2 folder in public_html does not bother you, then you could repeat the above commands in public_html instead of a/path.

Answers 2

how about cloning the entire repo on the host and use symlink between directories public_html -> directory2

Answers 3

This is straightforward git read-tree work. Make a bare git repo on the webserver, anywhere. Keep a manifest aka index for what's in your deployment directory and handle your updates with a pre-receive like so:

#!/bin/sh while read old new ref; do [[ $ref = refs/heads/deploy ]] && {     export GIT_INDEX_FILE=$GIT_DIR/deployment-manifest     export GIT_WORK_TREE=/path/public-html     git read-tree -um `git write-tree` $new:directory2 || exit 1 }; done 

Then push what you want deployed to the webserver's deploy branch, e.g. git push server master:deploy

Note that if some file being deployed this way has been changed in the deployment tree, the git read-tree here and the push will fail because git won't overwrite content you haven't told it about.

Answers 4

I don't care about pushing back to repo from web hosting

In this case you could use the archive command. Something like this:

git archive --remote=remote_repo --format=tar branch_name  path/to/directory2 > /path/public_html/dir2.tar && cd /path/public_html && tar xvf dir2.tar 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment