Quicktip: Exporting from a bare Git repository

Published on and tagged with git  tip

Recently I had to export the latest version of a project from a bare Git repository. As it seems like there is no equivalent to svn export in Git, I had to look for an alternative. After some searching I found the following solution:

$ cd ~/example.git
$ git archive master | tar -x -C /path/to/target/folder

The first part of this statement creates a tar archive with the content of the master branch. The second part then extracts this archive to the specified folder (-x means “extract”, and -C means “change to directory”).

I hope this is useful for some of you!

4 comments baked

  • Dieter_be

    Seems like a waste of cpu cycles to me.
    Why not just clone, and if you really don’t want the ‘.git’ directory, just remove it?
    or if you already have it checked out, cp && rm -rf .git
    keep it simple ;)


  • Silver Knight

    Re: “I hope this is useful for some of you!”

    Actually, this was indeed useful for me, insofar as I had completely forgotten about the existence of the ‘git archive’ command. i agree with Dieter about just cloning and removing the .git folder to keep things simple and fast, but I gotta admit that ‘git archive’ is a useful tool and I appreciate being reminded about it. ;)

  • cakebaker

    @Dieter, Silver Knight: Thanks for your comments!

    @Dieter: I don’t think I have to care much about cpu cycles, my repos are not that big ;-) But you are right, using ‘clone’ and then removing the ‘.git’ directory is a good alternative.

  • Eduardo Bustamante (dualbus)

    Dieter: Actually it’s the other way. When you clone a repository, it goes with all the history (so you’re wasting time and bandwidth). The archive creates a snapshot of the current state of the repository, which is a lot less than the whole history.

    Another way is to use the GIT_DIR and GIT_WORK_TREE variables to “checkout” the bare repository to a local work tree, although I find archive better for network transfers.

© daniel hofstetter. Licensed under a Creative Commons License