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


当前回答

这对我很有用。使用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

其他回答

如果您编辑了文件的本地版本,并希望恢复到中央服务器上维护的原始版本,可以使用Git扩展轻松实现。

最初文件将被标记为提交,因为它已被修改 在文件树菜单中选择(双击)该文件 列出了单个文件的修订树。 选择树的顶部/HEAD,然后右键单击save as 保存该文件以覆盖修改后的文件本地版本 文件现在有正确的版本,将不再被标记为提交!

简单!

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

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

非常简单:

git checkout from-branch-name -- path/to/the/file/you/want

这将不会签出from-branch-name分支。您将停留在您所在的分支上,并且只有该文件将从指定的分支签出。

下面是git-checkout手册的相关部分

git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
       When <paths> or --patch are given, git checkout does not switch
       branches. It updates the named paths in the working tree from the
       index file or from a named <tree-ish> (most often a commit). In
       this case, the -b and --track options are meaningless and giving
       either of them results in an error. The <tree-ish> argument can be
       used to specify a specific tree-ish (i.e. commit, tag or tree) to
       update the index for the given paths before updating the working
       tree.

向Ariejan de Vroom致敬,他在这篇博客文章中教会了我这些。

如果你需要一个来自远程Git存储库的特定分支的特定文件,命令如下:

git archive --remote=git://git.example.com/project.git refs/heads/mybranch path/to/myfile |tar xf -

剩下的可以从@VonC的回答中得到:

如果你需要一个来自主分支的特定文件,它是:

git archive --remote=git://git.example.com/project.git HEAD path/to/myfile |tar xf -

如果你需要一个标签的特定文件,它是:

git archive --remote=git://git.example.com/project.git mytag path/to/myfile |tar xf -

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

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

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

cd the_repo
git checkout HEAD name_of_file