Convert git repository into a tarball for sharing — Hugo Sum

Convert git repository into a tarball for sharing

A new hire joined our company, but the engineer who manages role and permission for the company is still on holiday. Obviously he didn’t get access to our remote git repository, so I had to convert our repository into a tarball and send it to him.

In this post, I will share how can you convert git repository into a tarball that respects .gitignore for easy sharing.

Creating tarball from remote repository

After searching for a while, I came across this Stackoverflow answer and learnt about git archive. This command creates an archive of files from a commit, therefore .gitignore is respected and only commited files will be included in the archive. Possible output format supported are tar, zip, tar.gz and tgz.

Since git archive is commit-based, you can reference your remote git repository to create an archive.

git archive --format=tar -o ~/myarchive.tar --remote=https://mygitrepository/myproject.git master # it accepts branch, commit or tag

Be mindful that this command would only work if the target git server supports it. Also if you want to use http instead of git protocol for your remote git repository, you have to use git 2.44.0 or later.

Creating tarball from local repository

If archiving directly from remote git repository is not an option for you, you can still clone the repository first, and then create an archive locally.

git archive --format=tar -o ~/myarchive.tar master

Hugo Sum

A Hongkonger living in the UK. The only thing I know is there is so much I don't know.

Archive