我如何从一个git回购签出一个文件?


当前回答

在GIT 1.7.2.2中工作

例如,你有一个带有分支branch1和branch32的远程some_remote

所以要签出一个特定的文件,你可以调用这些命令:

git checkout remote/branch path/to/file

举个例子,大概是这样的

git checkout some_remote/branch32 conf/en/myscript.conf
git checkout some_remote/branch1 conf/fr/load.wav

这个检出命令将整个文件结构conf/en和conf/fr复制到当前目录,在这里您调用这些命令(当然,我假设您在之前的某个时候运行了git init)

其他回答

首先使用-n选项和——depth 1选项克隆repo,前者抑制所有文件的默认签出,后者意味着它只获取每个文件的最新修订

git clone -n git://path/to/the_repo.git --depth 1

然后签出你想要的文件,就像这样:

cd the_repo
git checkout HEAD name_of_file

这对我很有用。使用git和一些shell命令

git clone --no-checkout --depth 1 git.example.com/project.git && cd project && git show HEAD:path/to/file_you_need > ../file_you_need && cd .. && rm -rf project

另一个解决方案,类似于使用——filter=blob:none的解决方案是使用——filter=tree:0(你可以在这里阅读关于区别的解释)。

这种方法通常比blob-one更快,因为它不下载树结构,但有一个缺点。考虑到您延迟了树的检索,当您进入repo目录时将受到惩罚(取决于repo的大小和结构,它可能比简单的浅克隆大许多倍)。

如果你是这种情况,你可以通过不进入回购来解决:

git clone -n --filter=tree:0 <repo_url> tgt_dir
git -C tgt_dir checkout <branch> -- <filename>
cat tgt_dir/<filename> # or move it to another place and delete tgt_dir ;)

请考虑到,如果您必须签出多个文件,树填充也会影响您的性能,因此我建议仅在回购足够大的情况下才对单个文件执行此操作。

这听起来像是你试图从集中式版本控制中继承一个想法,而git本质上不是这样的——它是分布式的。如果你想使用git存储库,你可以克隆它。然后,您就拥有了工作树的所有内容和所有历史记录(好吧,至少是指向当前分支顶端的所有内容),而不仅仅是单个文件或单个提交的快照。

 git clone /path/to/repo
 git clone git://url/of/repo
 git clone http://url/of/repo

如果您只需要下载文件,则不需要使用Git签出。

GitHub Mate更容易做到这一点,它是一个Chrome扩展,让你点击文件图标下载它。也是开源的