5

I am trying to zip a git repository directory with the .git directory included. I tried doing a tar on the directory, but it creates a bigger folder than original because it appends all the submodules.

I have tried the following commands but they zip without the .git directory-

git archive master --format=tar --output="../gridImageSearch.zip"

The following commands zips with .git directory, but the folder is too big -

tar -zcvf ../blah.tar.gz blah

Any suggestions?

3 Answers 3

5

Compress the git repository using

tar -c -f - foo | gzip >foo.tgz

If you wish to compress the repository without .git directory then run

tar -c --exclude .git -f - foo | gzip >foo.tgz
1

If your git repository directory contains files that are not versioned and you don't want to include them in the zip file, a simpler alternative is to git clone the repository to a new directory, and compress the whole new directory.

0

Here's one possible answer. You can use the find command to generate a list of all the files in your repository, exluding those contained in submodules. We identify a submodule as "a directory that contains a .git directory". The find command would be:

find . \( \! -depth 0 -type d -execdir test -f {}/.git \; -prune \) \
  -o -type f -print

That means "looking at everything contained in this directory, if it is a directory and contains a directory named .git and is not the top-level directory, do not descend into it, otherwise print out the filename".

You can use this as input into the tar command:

tar -T- -n -cz -f repo.tar.gz

That accepts a list of files on stdin (-T-, makes sure do not recurse into subdirectories (because find has done that for us), and creates (-c) gzip-compressed (-z) output.

The full command looks like:

find . \( \! -depth 0 -type d -execdir test -f {}/.git \; -prune \) \
  -o -type f -print |
tar -T- -n -cz -f repo.tar.gz

This would create a compress tar archive named repo.tar.gz in your current directory.

Ways that this will fail:

  • By not using git archive, you may in accidentally includes files in your working directory that are not part of the git repository. Using git clean can help avoid that.

  • You may also build an archive that includes changes that have not yet been committed to the repository. You can avoid this by explicitly checking for uncomitted changes before running your script.

Not the answer you're looking for? Browse other questions tagged or ask your own question.