Skip to content

Commit

Permalink
How to create a tarball of a git repository using "git archive"
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw authored Nov 16, 2022
1 parent 2e0632c commit 416b54b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions git/git-archive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# How to create a tarball of a git repository using "git archive"

I figured this out in [a Gist in 2016](https://gist.github.com/simonw/a44af92b4b255981161eacc304417368) which has attracted a bunch of comments over the years. Now I'm upgrading it to a retroactive TIL.

Run this in the repository folder:

git archive --format=tar.gz -o /tmp/my-repo.tar.gz --prefix=my-repo/ main

This will write out a file to `/tmp/my-repo.tar.gz`.

When you `tar -xzvf my-repo.tar.gz` that file it will output a `my-repo/` directory with just the files - not the `.git` folder - from your repository.

You can use a commit hash or tag or branch name instead of `main` to create an archive of a different point in that repository.

Without the `--prefix` option you'll get a `.tar.gz` file which, when compressed, writes a bunch of stuff to your current directory. This usually isn't what you want!

Here's a version that picks up the name of the directory you run it in:

git archive --format=tar.gz -o $(basename $PWD).tar.gz --prefix=$(basename $PWD)/ main

Note the trailing `/` on `--prefix` - without this you'll get folders called things like `datasettetests`.

`basename $PWD` gives you the name of your current folder.

0 comments on commit 416b54b

Please sign in to comment.