65

I'd like to export from github remote repository, not cloning it. Similar to svn export, I do not want to get .git folder with it. I can work around it by cloning and removing .git folder. I wonder if there is a cleaner way?

I read it somewhere you can use git archive to achieve this.

However I got the following errors..

$ git archive --format=tar --remote=git@github.com:xxx/yyy.git master | tar -xf -

Invalid command: 'git-upload-archive 'xxx/yyy.git''
You appear to be using ssh to clone a git:// URL.
Make sure your core.gitProxy config option and the
GIT_PROXY_COMMAND environment variable are NOT set.
fatal: The remote end hung up unexpectedly

Any help would be great. Thanks.

Adrian Gunawan
  • 13,979
  • 11
  • 40
  • 41
  • Possible duplicate of [Do a "git export" (like "svn export")?](https://stackoverflow.com/questions/160608/do-a-git-export-like-svn-export) – Andre Figueiredo Dec 23 '18 at 19:07
  • You might be interested in the API to get a zip mentioned here: https://stackoverflow.com/questions/8377081/github-api-download-zip-or-tarball-link – russau Jul 23 '21 at 03:08
  • Oct. 2021: the command-line `gh repo archive ` can help: see [my answer below](https://stackoverflow.com/a/69576856/6309) – VonC Oct 14 '21 at 20:16

10 Answers10

67

Thanks to the Subversion support by GitHub, you can use svn export to get the project without any version control files:

svn export https://github.com/user/project/trunk

Notice the URL format:

  • The base URL is https://github.com/
  • USERNAME/PROJECTNAME without .git
  • /trunk appended at the end

This way you can get branches and subdirectories too.

This creates a directory with the exported files. It's not possible to create a tar/zip directly, you have to do in two steps (export + zip). This is a limitation of svn export itself.

As @Jon pointed out, this will create the export in a directory named trunk by default. You can specify a different name if you prefer:

svn export https://github.com/username/projectname/trunk projectname

You can use this technique to export any sub-directory of the project. For example if you want only some/path, you can do:

svn export https://github.com/username/projectname/trunk/some/path local-dir-name

You can get paths from branches and tags too. The endpoint https://github.com/username/projectname behaves fully as a Subversion repository with a regular layout, so you will find branches in https://github.com/username/projectname/branches and tags in https://github.com/username/projectname/tags.

Before you export something large by mistake, it's good to check first the content of the path. You can do that using svn ls, for example:

svn ls https://github.com/username/projectname/

Normally this should give you:

branches/
tags/
trunk/

You could iteratively explore the repository this way.

Community
  • 1
  • 1
janos
  • 120,954
  • 29
  • 226
  • 236
  • 1
    I don't have svn/git and don't plan to install either. I just want the subdirectory. What do I do? – Mark Jeronimus Aug 13 '14 at 19:39
  • @MarkJeronimus if I knew how to do it without `svn`, I would have answered that way... Unfortunately I don't :( So you have to either download the entire repo using GitHub's own download feature, or install `svn` to use this hack. – janos Aug 13 '14 at 21:38
  • Thank you for sharing. However how sure are we that this isn't downloading the whole repo behind the scenes? I just used this to grab a tiny file from a large repo and the command took minutes to complete. – Oliver Pearmain Jun 04 '19 at 09:47
  • @OliverPearmain On a fairly large repo with long history, I see a huge difference between `time svn export repo/trunk/path` and `time git clone --bare repo`. (I added `--bare` to eliminate the extra time taken by creating the working tree.) – janos Jun 04 '19 at 20:00
  • Yup, this worked... The url formatting is very tricky. The url you need to use IS NOT the actual url. You need to use the format you described with "trunk" even if your "trunk" is called "master". There's a couple of hours of my life I'm never getting back ;) – John Apr 17 '20 at 19:27
18

For unknown (to me at least) reasons GitHub doesn't support this.

We don’t support people running git-archive against our servers.

Seems silly, since via SVN you can, but... I upvoted @Janos' answer.

Hugh W
  • 716
  • 2
  • 10
  • 33
9

If your goal is to limit the quantity of information exchanged with the server, have you considered using clone with --depth? You would still need to remove the (much reduced) .git subdirectory though:

git clone --depth=1 git@github.com:xxx/yyy.git && rm -rf yyy/.git
Samuel Tardieu
  • 2,071
  • 1
  • 14
  • 17
8

If you're only interested in exporting from GitHub then they provide a mechanism to download tarballs. For example:

https://github.com/torvalds/linux/downloads

Even though it says "there aren't any downloads for this repository." you can still use the buttons to download a tarball of the master branch.

Or see this link for a list of tarballs linked to tags:

https://github.com/torvalds/linux/tags

This should work for any GitHub repo, not just the linux kernel.

rathers
  • 81
  • 2
5

If you need this for named commits (that is branches and tags), then you can use git clone --depth=1 combined with git archive

It is worth knowing that git clone --depth=1 clones all top commits on all branches and tags (not only master). So after doing such a shallow clone you can enter the local directory and make a git archive --format=tar tag_to_be_exported.

So if you want to export the tag release1.1

git clone --depth=1 git@github.com:xxx/yyy.git
cd yyy
git archive --format=tar release1.1 -o release1.1.tar

So unnless you need to export unnamed commit-ids, this may be a good solution.

Jarl
  • 2,831
  • 4
  • 24
  • 31
  • 1
    It's interesting that you mention the "all branches and tags" topic. Indeed one might want to export a lightweight branch in an otherwise huge repository. Yet in my git (1.9.1) `man git-clone` has option `--[no-]single-branch` "Clone only the history leading to the tip of a single branch" and "When creating a shallow clone with the --depth option, this is the default, unless --no-single-branch is given to fetch the histories near the tips of all branches." – Stéphane Gourichon Oct 01 '14 at 15:19
  • @StéphaneGourichon Yep, defaults have changed since this answer was posted: http://stackoverflow.com/a/9920956/239408 – xverges Jan 31 '17 at 21:45
1

I think it is not possible to export github repository with git archive. Please read this

https://help.github.com/articles/can-i-archive-a-repository

Only possible ways are

git clone  
github download (ZIP) button   
Gihan De Silva
  • 458
  • 8
  • 17
1

I ran into this same problem before, and AFAICT, it only seems to work with Bitbucket, e.g.

ysim:~/repos$ git archive --format=tar --prefix=alembic/ --remote=ssh://git@bitbucket.org/zzzeek/alembic.git master | tar -xf -
ysim:~/repos$ ls
alembic

But I found a workaround for GitHub using wget - so in the new GitHub interface, you'll find a button that says "Download ZIP" on the bottom of the right sidebar; right-click on it and copy the link address.

Download ZIP

Next, in the command line (I got the idea from here):

wget -qO- -O tmp.zip <zipball url> && unzip tmp.zip && rm tmp.zip

That will unzip it to a directory called something like repo-master/.

If you want, you can also alias this in your .gitconfig so you don't have to remember/type all of that out, e.g.

export = "! f() { wget -qO- -O tmp.zip \"$1\" && unzip tmp.zip && rm tmp.zip; }; f"

So you can just do something like this in the shell:

ysim:~/repos$ git export https://github.com/ysim/verbinski/archive/master.zip
ysim:~/repos$ ls
verbinski-master
Community
  • 1
  • 1
3cheesewheel
  • 9,133
  • 9
  • 39
  • 59
0

One way to achieve this is to use the SVN support offered by GIT.

Steps to perform as root one off are (for Cent os);

yum install git

yum install subversion

Then to do a selective export the following syntax should be used:

svn export  <git repository>/<source folder> <empty target folder>  --no-auth-cache --force --username <active directory username>

If you do not use parameter --no-auth-cache when prompted to save the password type "no" for security reasons, or it will be saved unencrypted.

When translating from the GIT notation to the SVN notation the following mapping applies; replace "tree/master" with "trunk" for master replace "tree/branch_name" with "branches/branch_name" for branches

This works for both files and directories.

Anto
  • 63
  • 9
0

If you need to get a tarball of a commit on github I think this is the easiest way:

Say your repository name is https://github.com/arielgabizon/librustzcash

and the commit id is 4be41ca6160c16abfd983c8c878b6ea04105b224

Just go in your browser to the address https://github.com/arielgabizon/librustzcash/archive/4be41ca6160c16abfd983c8c878b6ea04105b224.tar.gz

and, at least on google chrome, it will start downloading the file.

Ariel Gabizon
  • 2,851
  • 2
  • 17
  • 22
-6

For a normal export:

$ git archive master | tar -x -C /path/to/destination

For a zip archive:

$ git archive --format zip --output /path/to/destination/file.zip master

Of course for this to work, you'll need to clone it locally first, there's no clean way around that.

Marvin Pinto
  • 30,138
  • 7
  • 37
  • 54
  • @AdrianGunawan You won't be able to export it _directly_ from GitHub, you need to first clone it locally. After you clone it locally, `master` refers to the branch name. – Marvin Pinto Mar 07 '12 at 22:34
  • 3
    Downvoted for disregarding the crux of the question as stated and titled. – danorton Aug 22 '12 at 21:56