What you are trying to do is called a sparse checkout , and that feature was added in git 1.7.0 (Feb. 2012).
(您尝试执行的操作称为稀疏签出 ,该功能已在git 1.7.0中添加(2012年2月)。)
The steps to do a sparse clone are as follows: (进行稀疏克隆的步骤如下:)
mkdir <repo>
cd <repo>
git init
git remote add -f origin <url>
This creates an empty repository with your remote, and fetches all objects but doesn't check them out.
(这将使用您的遥控器创建一个空的存储库,并获取所有对象,但不将其检出。)
Then do: (然后做:)
git config core.sparseCheckout true
Now you need to define which files/folders you want to actually check out.
(现在,您需要定义要实际检出的文件/文件夹。)
This is done by listing them in .git/info/sparse-checkout
, eg: (通过在.git/info/sparse-checkout
列出它们来完成此操作,例如:)
echo "some/dir/" >> .git/info/sparse-checkout
echo "another/sub/tree" >> .git/info/sparse-checkout
Last but not least, update your empty repo with the state from the remote:
(最后但并非最不重要的一点是,使用远程状态更新空仓库:)
git pull origin master
You will now have files "checked out" for some/dir
and another/sub/tree
on your file system (with those paths still), and no other paths present.
(现在,您将在文件系统上“检出” some/dir
和another/sub/tree
的文件(这些路径仍然存在),并且不存在其他路径。)
You might want to have a look at the extended tutorial and you should probably read the official documentation for sparse checkout .
(您可能想看一下扩展教程,并且应该阅读稀疏签出的官方文档 。)
As a function:
(作为功??能:)
function git_sparse_clone() (
rurl="$1" localdir="$2" && shift 2
mkdir -p "$localdir"
cd "$localdir"
git init
git remote add -f origin "$rurl"
git config core.sparseCheckout true
# Loops over remaining args
for i; do
echo "$i" >> .git/info/sparse-checkout
done
git pull origin master
)
Usage:
(用法:)
git_sparse_clone "http://github.com/tj/n" "./local/location" "/bin"
Note that this will still download the whole repository from the server – only the checkout is reduced in size.
(请注意,这仍将从服务器下载整个存储库-仅减少结帐的大小。)
At the moment it is not possible to clone only a single directory. (目前,无法仅克隆单个目录。)
But if you don't need the history of the repository, you can at least save on bandwidth by creating a shallow clone. (但是,如果不需要存储库的历史记录,则至少可以通过创建浅表克隆来节省带宽。)
See udondan's answer below for information on how to combine shallow clone and sparse checkout. (有关如何组合浅表克隆和稀疏校验的信息,请参见下面的udondan答案 。)