8

I need to create an archive of my Git project, ignoring and leaving out files specified by .gitignore, but including the actual .git repository folder. Simply running git archive master leaves out the .git folder.

Is there a way to make git archive include the .git folder but still ignore files specified by .gitignore?

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
  • 1
    You can just copy the `.git` folder alone and later checkout HEAD to get your files. Or you simply transfer it by fetching from it as a remote repository. – poke Jul 23 '12 at 23:02

3 Answers3

16

Since git archive just produces a tar archive you can work on that file with tar directly.

$ git archive HEAD > tar.tar
$ tar -rf tar.tar .git

tar's -r option appends the files given to the archive being worked on (specified after -f). Check tar's man page for the intimidating list of features tar has.

Benjamin Bannier
  • 55,163
  • 11
  • 60
  • 80
3

Looks like doing something like

# copy over all project files except for those ignored
git clone /path/to/repository /path/to/output 
# create a tar.gz archive of the project location
tar czvf projectExport.tar.gz /path/to/output
# remove the cloned directory
rm -fr /path/to/output

gets the job done. It's not the most beautiful solution in the world, but it looks like it works.

Naftuli Kay
  • 87,710
  • 93
  • 269
  • 411
0

git bundle is a newer command which allows the creation of an archive to be shared with a remote machine. The git bundle is more flexible in that it allows incremental updates and merging of different branches. However, you can create a bundle that use a complete specification, such as master.

A downside is that the remote machine also needs git. The bundle is a 'pack' file and the files would need to be 'checked out' from the bundle to create a working directory on the remote (as opposed to tar). The set of machines with tar && git and !tar && !git is much larger than tar && !git and !tar && git. Ie, the machine likely has both or neither. Especially as you would intend to build source.

For Windows machines which likely have neither. You can use the 'zip' form.

git archive -o ../archive.zip --format=zip HEAD
zip -ur ../archive.zip .git/

.. and you have my condolences.

artless noise
  • 21,212
  • 6
  • 68
  • 105