4

I want to fetch a single file from a git repository. Using SVN, I can use svn export as follows:

sudo svn export http://www.example.com/repos/Trunk/pathtofile/file.ext

This just downloads the file, naked, i.e without any repository info with it.

Is there an equivalent to svn export in git?

The closest I got was "git show upstream/master:pathtofile/file.ext" but this isn't really equivalent because you already need to be inside a git repo for it to work.

Another option I have, because I'm using github, is to try to wget the github raw file.

wget https://raw.github.com/<mygithubuser>/repo/master/pathtofile/file.ext?login=<mylogin>&token=<notsurewhattoputhere>

The problem is that the repo is private so I need to supply a token and I'm not sure how to generate this and use it in a wget?

Tom
  • 14,041
  • 16
  • 64
  • 80

3 Answers3

2

If the repo is in GitHub then just use svn export!

svn export https://github.com/username/repo-name/trunk/pathtofile/file.ext

Community
  • 1
  • 1
Anthony Hatzopoulos
  • 10,437
  • 2
  • 40
  • 57
2

I've not checked, but according to this git hub page your authentication token is supplied on you profile page. So assuming it's an option to just manually embed it in the command, then I'd go for that. Otherwise I suppose you'd need to accept it as a parameter to the script this command will be contained in, or possibly in a configuration file. Although I guess both of those options could have security concerns.

obmarg
  • 9,369
  • 36
  • 59
  • That github page looked very promising. Unfortunately, I wasn't able to get a `wget` working even though I was using the token as specified. – Tom Nov 14 '11 at 16:07
  • 1
    Odd, that page certainly seems to imply that the token based authentication should work anywhere. I don't have access to any private repos myself to test anything out myself though. Did you enclose the URL in quotes when running wget? Just ask because that's a mistake I commonly make, and I notice they're missing in your example command above. – obmarg Nov 14 '11 at 17:38
  • Also, have you tried supplying the username & token as POST parameters? That seems to be what the example from that link does with it's -F parameter to curl – obmarg Nov 14 '11 at 17:45
  • YES YES YES - It worked when I enclosed the URL in quotes when running wget. I didn't know that was needed. Thanks @obmarg. – Tom Nov 15 '11 at 14:36
  • No problem. It's needed because the shell treats & (and possibly ?) as special characters, so you either need to escape them or enclose the whole parameter in quotes, just as you would if the url contained spaces. – obmarg Nov 15 '11 at 14:42
  • This is great. Did not know this existed. I have leveraged this to add a more secure answer below. – rynop Feb 17 '12 at 14:13
1

I would not put the token and username in the query string. Instead do something like this:

curl -F 'login=mylogin' -F 'token=mytoken' https://raw.github.com/<mygithubuser>/repo/master/pathtofile/file.ext > /tmp/file.ext

As for why you should not put it in the QS this is a good explanation.

Community
  • 1
  • 1
rynop
  • 50,086
  • 26
  • 101
  • 112