将git子目录签出到特定文件夹中

时间:2015-03-17 12:59:08

标签: git

我想在我正在开发的项目中使用Sodium库,但我需要进行一些练习并快速掌握它。但是,我也希望以我通常的方式将所有依赖项放在文件夹ext/下。

mkdir Test
cd Test
git init
git config core.sparsecheckout true
echo c++/ >> .git/info/sparse-checkout
git remote add -f sodium https://github.com/SodiumFRP/sodium.git
git pull sodium master

这只给了我测试根目录中的c++/文件夹,这对我来说并不是特别有帮助。我希望它位于ext/文件夹中,并命名为c++以外的其他内容。重命名文件夹会导致它被视为未跟踪,这是不可取的,因为我可以通过从zip复制文件来实现这一点。

如果我创建一个ext/目录并从中执行pull命令,则仍会在根目录中创建c++文件夹,而不是执行pull命令的位置。

有没有办法拉出一个特定的分支并将其复制到另一个名称的子目录?

1 个答案:

答案 0 :(得分:1)

我将如何做到这一点:

假设您有一个名为main的主项目存储库:

# for the sake of completeness
git init main 
cd main

现在,为依赖项添加一个文件夹并使其成为稀疏结帐:

# make and change to directory
mkdir ext
cd ext

# sparse checkout of c++ directory only
git init
git config core.sparsecheckout true
echo c++/ >> .git/info/sparse-checkout

# add remote repository and check it out
git remote add -f sodium https://github.com/SodiumFRP/sodium.git
git pull sodium master

然后你有一个稀疏结账,你作为子模块添加:

cd .. # go back to main repository

# add and commit submodule
git submodule add ./ext
git commit -m "Added submodule"

我用git 1.9.1进行了测试。

如果您愿意,可以添加另一个目录,使其具有如下布局:

main
  + ext        # multiple external dependencies
  |  + sodium
  |     + .git
  |     + c++
  + src        # main project source files