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


当前回答

现在我们可以了!因为这是谷歌上的第一个结果,我想我应该把它更新到最新的位置。随着git 1.7.9.5的出现,我们有了git归档命令,它将允许您从远程主机检索单个文件。

git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x

在这里可以看到完整的答案https://stackoverflow.com/a/5324532/290784

其他回答

首先使用-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 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 archive --format=tar --remote=origin HEAD | tar xf -
git archive --format=tar --remote=origin HEAD <file> | tar xf -

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

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

在git中,你在更新文件之前不会“签出”它们-这似乎是你所追求的。

许多系统,如clearcase, csv等,都要求您在对文件进行更改之前“签出”文件。Git不需要这个。克隆存储库,然后在存储库的本地副本中进行更改。

一旦你更新了文件,你可以做:

git status

查看修改了哪些文件。你先添加你想要提交给index的对象(index就像一个要签入的列表):

git add .

or

git add blah.c

然后do git status会告诉你哪些文件被修改了,哪些文件在索引中准备提交或签入。

要将文件提交到存储库副本,请执行以下操作:

git commit -a -m "commit message here"

参见git网站上的手册和指南链接。