Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
441 views
in Technique[技术] by (71.8m points)

git - 如何仅克隆Git存储库的子目录?(How do I clone a subdirectory only of a Git repository?)

I have my Git repository which, at the root, has two sub directories:

(我有我的Git存储库,它的根目录有两个子目录:)

/finisht
/static

When this was in SVN , /finisht was checked out in one place, while /static was checked out elsewhere, like so:

(在SVN中时/finisht在一个位置检出,而/static在其他地方检出,如下所示:)

svn co svn+ssh://[email protected]/home/admin/repos/finisht/static static

Is there a way to do this with Git?

(有没有办法用Git做到这一点?)

  ask by Nick Sergeant translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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/diranother/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答案 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...