Как мне клонировать только подкаталог Git-репозитория? - PullRequest
1202 голосов
/ 01 марта 2009

У меня есть мой репозиторий Git, в корне которого есть два подкаталога:

/finisht
/static

Когда это было в SVN , /finisht было извлечено в одном месте, в то время как /static было извлечено в другом месте, например:

svn co svn+ssh://admin@domain.com/home/admin/repos/finisht/static static

Есть ли способ сделать это с помощью Git?

Ответы [ 12 ]

5 голосов
/ 08 марта 2018

Вот сценарий оболочки, который я написал для сценария использования единственной подкаталога разреженной проверки

coSubDir.sh

localRepo=$1
remoteRepo=$2
subDir=$3


# Create local repository for subdirectory checkout, make it hidden to avoid having to drill down to the subfolder
mkdir ./.$localRepo
cd ./.$localRepo
git init
git remote add -f origin $remoteRepo
git config core.sparseCheckout true

# Add the subdirectory of interest to the sparse checkout.
echo $subDir >> .git/info/sparse-checkout

git pull origin master

# Create convenience symlink to the subdirectory of interest
cd ..
ln -s ./.$localRepo$subDir $localRepo
2 голосов
/ 22 марта 2019

Это приведет к клонированию определенной папки и удалению всей истории, не связанной с ней.

git clone --single-branch -b {branch} git@github.com:{user}/{repo}.git
git filter-branch --subdirectory-filter {path/to/folder} HEAD
git remote remove origin
git remote add origin git@github.com:{user}/{new-repo}.git
git push -u origin master
...