从远程git存储库获取单个文件内容的最有效机制(就传输的数据和使用的磁盘空间而言)是什么?
到目前为止,我已经想出了:
git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
这似乎还是有些过分。
从回购中获取多个文件呢?
从远程git存储库获取单个文件内容的最有效机制(就传输的数据和使用的磁盘空间而言)是什么?
到目前为止,我已经想出了:
git clone --no-checkout --depth 1 git@github.com:foo/bar.git && cd bar && git show HEAD:path/to/file.txt
这似乎还是有些过分。
从回购中获取多个文件呢?
当前回答
在git版本1.7.9.5中,这似乎可以从远程导出单个文件
git archive --remote=ssh://host/pathto/repo.git HEAD README.md | tar xO
这将包含文件README.md的内容。
其他回答
Yisrael Dov的答案很简单,但它不允许压缩。您可以使用——format=zip,但不能像使用tar一样使用管道命令直接解压缩,因此需要将其保存为临时文件。这是一个脚本:
#!/bin/bash
BASENAME=$0
function usage {
echo "usage: $BASENAME <remote-repo> <file> ..."
exit 1
}
[ 2 -gt "$#" ] && { usage; }
REPO=$1
shift
FILES=$@
TMPFILE=`mktemp`.zip
git archive -9 --remote=$REPO HEAD $FILES -o $TMPFILE
unzip $TMPFILE
rm $TMPFILE
这也适用于目录。
我使用curl,它与公共回购或通过web界面使用https基本身份验证的人一起工作。
curl -L——retry 20——retry-delay 2 -O https://github.com/ACCOUNT/REPO/raw/master/PATH/TO/FILE/FILE.TXT -u USER:PASSWORD
我在github和bitbucket上进行了测试,两者都适用。
我用这个
$ cat ~/.wgetrc
check_certificate = off
$ wget https://raw.github.com/jquery/jquery/master/grunt.js
HTTP request sent, awaiting response... 200 OK
Length: 11339 (11K) [text/plain]
Saving to: `grunt.js'
我是这样解决的:
git archive --remote=ssh://git@gitlab.com/user/mi-repo.git BranchName /path-to-file/file_name | tar -xO /path-to-file/file_name > /path-to-save-the-file/file_name
如果你愿意,你可以用"BranchName"代替"HEAD"
下面两个命令对我来说很有用:
Git存档——remote={remote_repo_git_url} {branch} {file_to_download} -o {tar_out_file}
从远程存储库的分支(url为remote_repo_git_url)下载file_to_download作为tar归档文件,并存储在tar_out_file中
Tar -x -f {tar_out_file}.tar从tar_out_file中提取file_to_download文件