开发者

How can I check out a single file from GitHub without cloning the whole repository?

On a production machine I want to check out a specific file at a specific revision from GitHub to facilitate data开发者_高级运维base 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?


I do this with for backbones like so

curl -O https://raw.github.com/documentcloud/backbone/master/backbone-min.js


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


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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜