8

On a production machine I want to check out a specific file at a specific revision from GitHub to facilitate database migration. I don't want to checkout (or even clone) the whole source code repository (because this is my production environment). How can I pull only the file I am looking for?

Niel de Wet
  • 7,806
  • 9
  • 63
  • 100
  • 2
    Already solved for sure: http://stackoverflow.com/questions/610208/how-to-retrieve-a-single-file-from-specific-revision-in-git http://stackoverflow.com/questions/692246/how-do-i-revert-one-file-to-the-last-commit-in-git http://stackoverflow.com/questions/6580641/how-to-checkout-a-single-file-in-git http://stackoverflow.com/questions/1125476/git-retrieve-a-single-file-from-a-repository – fuzzyalej Sep 08 '11 at 06:21
  • 1
    All of the above are mostly irrelevant, except http://stackoverflow.com/questions/1125476/git-retrieve-a-single-file-from-a-repository , but unfortunately you can't use ``--remote=`` with GitHub (http://groups.google.com/group/github/browse_thread/thread/cfcbcb1dc5f41f16). – Niel de Wet Sep 11 '11 at 12:49

3 Answers3

11

I do this with for backbones like so

curl -O https://raw.github.com/documentcloud/backbone/master/backbone-min.js
Kevin
  • 25,946
  • 2
  • 19
  • 21
4

My solution is like in first post. But I try to explain a bit more. There is a "Raw" button on GitHub for every file - it will show just plain text in browser. Also, you can use that url.

For example, I have repo https://github.com/MasterSergius/conf_files.git And I want to get my .vimrc file, so here the link to my file: https://raw.githubusercontent.com/MasterSergius/conf_files/master/.vimrc I do think, that by this template you even can guess file url by repo and file full pathname. So, now I can download it with curl:

curl https://raw.githubusercontent.com/MasterSergius/conf_files/master/.vimrc -o ~/.vimrc
Sergius
  • 908
  • 8
  • 20
  • 1
    This helped me, thank you! I also looked more into CURL, and if you replace the `-o` flag with `-O`, it will use the filename from the server without you needing to explicitly specify the local filename, with the tradeoff that you must run the command from within the destination folder. You can also have it iterate over multiple files using `{thing1,thing2,etc}` in the filename. Between these two options, I was able to download a set of CSV seed data files for a class project very quickly. – joequincy May 02 '19 at 15:17
3

As far as I can tell, downloading a single file from a git repository served over http is currently impossible. I believe GitHub has a separate "download" feature they want people to use instead (but I do not know whether it will support downloading a single file).

The workaround is to clone the entire repository (!) and then pull out the file of interest. Here's a bash function which does the job:

git-cat() {
    if  [ -z "$1" -o -z "$2" ]; then
        echo "Usage: git-cat REPO_URL FILE [BRANCH]"
        exit 1
    fi
    tmprepo=`mktemp -d -t gitrepo.XXXXXX`
    reponame=$1
    filename=$2
    branchname=$3
    if [ -z "$branchname" ]; then
        branchname="master"
    fi
    git clone -nq $reponame $tmprepo &&
    git --git-dir $tmprepo/.git show ${branchname}:${filename} &&
    rm -rf $tmprepo
}
Paul Price
  • 2,657
  • 30
  • 26