Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 487
bryan kennedy Avatar asked May 13 '11 16:05

bryan kennedy


People also ask

What is the git command to create the archive files?

The git archive command is a Git command line utility that will create an archive file from specified Git Refs like, commits, branches, or trees.


2 Answers

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.
like image 135
manojlds Avatar answered Oct 13 '22 06:10

manojlds


You can use git archive for this purpose with the --remote option.
You can pipe it to create zip or tar or whatever you like.

Please, see the git-archive(1) manual page.

like image 40
ralphtheninja Avatar answered Oct 13 '22 05:10

ralphtheninja