Reputation: 335
My goal is to clone/pull/whatever a copy of the latest version of a branch of my repository to my test web server, to test before production.
Note: I realize there are a ton of similar questions on here, but the ones I've looked into were different enough from my situation that I still came away a bit confused.
I am not very well-versed in git
on the command line (I tend to use GUI tools), so I'm not sure if I have this right. What I've got so far works, but the initial pull seemed pretty big, so I'm wondering if I am getting the whole history, or only the latest version.
I do not ever want to make changes live on the server or push back to origin. This only needs to be a one-way street.
Let's say I have a branch called test
, which is what I will load onto the test web server. However, we don't want the root directory of the repository, because there's some cruft in there. We only want the /sub
directory and everything below. The initial setup on my server:
$ cd ~/webapps/test_dir
$ git init
$ git remote add origin https://github.com/user/repository.git
$ git config core.sparseCheckout true
$ echo "sub/*" >> .git/info/sparse-checkout
Then each time I want to update the code on the server, this is what I will run:
$ git pull --depth=1 origin test
Is this command correct, to replace what's on the server with only the latest code, with no history? Is there a more efficient command to accomplish what I want?
In light of Github's lack of support for git archive
(which seems like the way to go otherwise), I am trying to work up a solution getting an archive using their APIv3, which they suggested in a support email response.
I've gotten as far as getting a downloaded archive of the correct branch of my repo:
$ curl -L --user "username" https://api.github.com/repos/<username>/<reponame>/tarball/<branchname> > example.tar.gz
But that gets me everything from the root of the branch. What I really want is only one subfolder, but I have yet to figure out how to filter for that.
Got confirmation from GitHub that there is no way to retrieve a subfolder through this API method. So… back to the way I was doing it in the first place, I think.
Upvotes: 1
Views: 841
Reputation: 335
Referencing this answer by Anthony Hatzopoulos, it seems the best way to do this may be to take advantage of GitHub’s Subversion compatibility, and do this:
$ svn export --force https://github.com/<user>/<repo>/branches/<branch>/<subfolder>
Does exactly what I wanted it to do, without any cruft.
The --force
is to replace any current files/folders with the new ones. Of course that means I'm replacing everything instead of incremental updates, but… tradeoffs? ¯\_(ツ)_/¯
Upvotes: 0
Reputation: 7367
You probably want to consider using git archive
rather than a pull to render out data with no history attached to it. The formal docs for that command are at https://git-scm.com/docs/git-archive.
Something like git archive branchname | tar xf -
should be about right to dump the commit at the head of the relevant branch to the local directory.
If you want to put your exported bits into a subdirectory, you can use something like git archive foo --prefix branch/ | tar xf -
and that will put all the commits from the HEAD of branch foo
into the directory branch
. You could also redirect the tar
command to move it elsewhere - but since you're in the directory with your git enlistment, the local directory from the first part of this will still have a .git in it.
Upvotes: 1