How do I pipe a git clone to archive (tar or gzip)

bryan kennedy picture bryan kennedy · May 13, 2011 · Viewed 16.4k times · Source

I am trying to make a simple backup script for my remotely hosted git repos. In the script I have a few lines that currently look like this:

git clone git@server:repo.git $DEST
tar czvf repo.tgz $DEST
rm -rf $DEST

Is there a way to make this all happen in one line? Can I pipe the git clone into the tar command? I don't need the cloned directory, I just want the compressed archive of it.

I've tried some experiments but can't seem to figure out the syntax.

Answer

manojlds picture manojlds · May 13, 2011

No you cannot just pipe git clone because it does not write it out to the standard output. And why do you need a one-liner? They are great for boasting that you can do something cool in just one line, but not really ideal in real world.

You can do something like below, but you would not get .git like you would in a git clone :

git archive --format=tar --remote=git@server:repo.git master | tar -xf -

From git archive manual

--remote=<repo>

    Instead of making a tar archive from the local repository, retrieve a tar archive from a remote repository.