1

I have some php code in a git repository. I use that code in multiple vhosts on the same web server, but different vhosts may use different versions of the code.

How can I create copies of the files from a specific tag in the various vhost httpdocs directories, but without having to replicate the .git directory for each one?

I realise I could git clone/git checkout tag for each vhost, and just delete the .git directory, or use git archive and then unpack the files, but I am assuming/hoping there is a 'proper' git way to do this... something like git archive, but where the files are copied directly to the required location.

rowatt
  • 421
  • 4
  • 16
  • 1
    Well, the proper way is to clone the repo. Why do you want to prevent that? – Šimon Tóth Mar 22 '11 at 13:16
  • Two reasons.(1) it seems unnecessary (and ultimately wasteful of space) to have multile .git directories across the server, particularly when the copies aren't ever going to be updated. (2) I don't want to have the .git directories web accessible. I realise I can fix that with .htaccess etc, but the most secure way is to not have them there at all. – rowatt Mar 22 '11 at 14:16
  • On a Unix or Linux system, Git will create hardlinks when making a local clone, so you won't be duplicating all the .git directory contents. – mipadi Mar 22 '11 at 15:28

1 Answers1

1

git archive (like you mention) is the right command to extract a subdirectory for a given tag.

git archive [-o | --output=<file>] <tree-ish> [<path>…]
  • <tree-ish> would be your tag
  • path would be the right subset of files you need.

But that means for you to process that archive and uncompress it to the right place, which, as Bombe points out in the comments, can be done with:

git archive --format=tar … | tar -C /path/to/target -xf - 
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 2
    That can be done in a single command: `git archive --format=tar … | tar -C /path/to/target -xf -` – Bombe Mar 22 '11 at 13:49
  • @Bombe: thank you for this command, I have included it in the answer. – VonC Mar 22 '11 at 14:19
  • Thanks. I was assuming there would be a more direct way to do this... compressing and then decompressing files just to copy them seems rather convoluted! Easy enough to implement, though, so should work fine if that's the best way to do it. – rowatt Mar 22 '11 at 14:24
  • @rowatt Tar does not compress, it only archives. There is very little overhead here; tar is actually a popular method for copying files between machines in a very similar manner. – Jonathan Mar 22 '11 at 14:36
  • Of course... I am so used to using tar to compress (ie tar -z etc) I overlooked that! – rowatt Mar 22 '11 at 15:28