如何从git仓库只签出一个文件('稀疏结帐')? [英] How to checkout only one file from git repository ('sparse checkout')?
问题描述
-
你可以使用git archive(参见 Jared Forsyth 的答案和 Robert Knight 的回答),因为 git1.7.9.5(2012年3月):
git archive --remote = git://git.foo.com/project.git HEAD:path / to / directory filename | tar -x
这将从当前目录的远程仓库的
HEAD
中保存一份'文件名
'的副本。
:path / to / directory 部分是可选的。如果排除,则获取的文件将被保存到
<当前工作目录> /路径/目录/文件名
。
Fizer Khan
git archive --format
它类似于 Paul Brannan 的回答:
git archive --format = tar --remote = origin HEAD:path / to / directory - filename | tar -O -xf -
As 评论由 Doron Gold ,为了 git archive --remote = xxx
才能正常工作,您必须首先在git服务器上的存储库目录中运行此目录(git守护程序在其中运行) :
git config daemon.uploadarch true
请参阅 patthoyts '2009年的评论 JakubNarębski在 git:Retrieve
如果您拥有裸仓库的本地副本,您可以执行以下操作,如th是答案,
git - 无页面 - git-dir / path / to / bar / repo .git show branch:path / to / file> file
- 在工作树中.git回购
- 。
但是,您可以执行 稀疏结帐 (如果您使用的是Git1.7 +),那么:
$ b
git config core.sparsecheckout true
)
.git / info / sparse-checkout中添加要查看的内容
重新读取工作树:
$ git read- tree -m -u HEAD
这样,你最终会得到一个工作树,包括你想要的东西(即使它只有一个文件)。
How do I checkout just one file from a git repo?
As mentioned in the other answers:
you can use git archive (see Jared Forsyth's answer and Robert Knight's answer), since git1.7.9.5 (March 2012):
git archive --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -x
That will save a copy of '
filename
' from theHEAD
of the remote repository in the current directory.The
:path/to/directory
part is optional. If excluded, the fetched file will be saved to<current working dir>/path/to/directory/filename
.
Fizer Khan mentions below specifying the tar format explicitly:
git archive --format=tar --remote=origin HEAD -- <file> | tar xf -
It is similar to Paul Brannan's answer:
git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -
As commented by Doron Gold, for git archive --remote=xxx
to work, you MUST first run this inside the directory of your repository on your git server (where git daemon runs):
git config daemon.uploadarch true
See patthoyts' comment of 2009 Jakub Narębski's answer on "git: Retrieve a single file from a repository".
You could alternatively do the following if you have a local copy of the bare repository as mentioned in this answer,
git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file
or you must clone first the repo, meaning you get the full history:
- in the .git repo
- in the working tree.
But then you can do a sparse checkout (if you are using Git1.7+),:
- enable the sparse checkout option (
git config core.sparsecheckout true
) - adding what you want to see in the
.git/info/sparse-checkout
file - re-reading the working tree to only display what you need
- enable the sparse checkout option (
To re-read the working tree:
$ git read-tree -m -u HEAD
That way, you end up with a working tree including precisely what you want (even if it is only one file)
这篇关于如何从git仓库只签出一个文件('稀疏结帐')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!