我有一个Git仓库,根目录下有两个子目录:
/finisht
/static
当这个项目在SVN上时,/finisht
被检出到一个地方,而/static
则被检出到另一个地方,如下所示:
svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static
有没有使用Git的方法可以实现这个?
我有一个Git仓库,根目录下有两个子目录:
/finisht
/static
当这个项目在SVN上时,/finisht
被检出到一个地方,而/static
则被检出到另一个地方,如下所示:
svn co svn+ssh://admin@domain.example/home/admin/repos/finisht/static static
有没有使用Git的方法可以实现这个?
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
对我来说可行 - (git版本2.35.1)
git init
git remote add origin <YourRepoUrl>
git config core.sparseCheckout true
git sparse-checkout set <YourSubfolderName>
git pull origin <YourBranchName>
这是我所做的事情
git init
git sparse-checkout init
git sparse-checkout set "YOUR_DIR_PATH"
git remote add origin https://github.com/AUTH/REPO.git
git pull --depth 1 origin <SHA1_or_BRANCH_NAME>
git sparse-checkout init
很多文章会告诉你要设置 git sparse-checkout init --cone
如果加上 --cone
会获取到一些不需要的文件。
git sparse-checkout set "..."
将 .git\info\sparse-checkout
文件内容设置为 ...
假设你不想使用该命令,可以打开 git\info\sparse-checkout
文件后进行编辑。
假设我想要获取完整 repo 大小>10GB↑(包含 git)2个文件夹,如下总大小< 2MB
git init
git sparse-checkout init
// git sparse-checkout set "chrome/common/extensions/api/"
start .git\info\sparse-checkout open the "sparse-checkut" file
/* .git\info\sparse-checkout for example you can input the contents as below
chrome/common/extensions/api/
!chrome/common/extensions/api/commands/ ! unwanted : https://www.git-scm.com/docs/git-sparse-checkout#_full_pattern_set
!chrome/common/extensions/api/devtools/
chrome/common/extensions/permissions/
*/
git remote add origin https://github.com/chromium/chromium.git
start .git\config
/* .git\config
[core]
repositoryformatversion = 1
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[extensions]
worktreeConfig = true
[remote "origin"]
url = https://github.com/chromium/chromium.git
fetch = +refs/heads/*:refs/remotes/Github/*
partialclonefilter = blob:none // Add this line, This is important. Otherwise, your ".git" folder is still large (about 1GB)
*/
git pull --depth 1 origin 2d4a97f1ed2dd875557849b4281c599a7ffaba03
// or
// git pull --depth 1 origin master
partialclonefilter = blob:none
我知道添加这行代码是因为我从git clone --filter=blob:none
命令中得知它会生成这行代码,所以我模仿了它。
Git 版本:git version 2.29.2.windows.3
git pull --depth 1 origin dev
- Sudhir N正在使用 Linux?只需访问易于使用且整洁的工作目录,而无需打扰计算机上的其他代码。尝试使用符号链接!
git clone https://github.com:{user}/{repo}.git ~/my-project
ln -s ~/my-project/my-subfolder ~/Desktop/my-subfolder
测试
cd ~/Desktop/my-subfolder
git status
为了澄清一些很好的答案,许多答案中概述的步骤都假设您已经有了远程存储库。
给定:一个现有的Git存储库,例如git@github.com:some-user/full-repo.git
,其中包含一个或多个您希望独立于其余存储库拉取的目录,例如名为app1
和app2
的目录。
假设您有一个如上所述的Git存储库...
那么: 您可以运行以下步骤,仅从该较大的存储库中拉取特定目录:
mkdir app1
cd app1
git init
git remote add origin git@github.com:some-user/full-repo.git
git config core.sparsecheckout true
echo "app1/" >> .git/info/sparse-checkout
git pull origin master
$ mkdir com.unity.render-pipelines.core
$ cd com.unity.render-pipelines.core/
$ git init
$ git remote add origin https://github.com/Oculus-VR/Unity-Graphics.git
$ git config core.sparsecheckout true
$ echo "com.unity.render-pipelines.core/" >> .git/info/sparse-checkout
$ git pull origin 2021.2/oculus-appsw-particles
文件夹大小约为7mb,但是...$ ...
$ Receiving objects: 6% (24305/375290), 27.30 MiB | 121.00 KiB/s
- Vlad我为单个子目录稀疏检出编写了一个shell脚本
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
ln -s ./.$localRepo/$subDir $localRepo
而不是 ln -s ./.$localRepo$subDir $localRepo
。 - valentin_nastagit init <repo>
cd <repo>
git remote add origin <url>
git config core.sparsecheckout true
echo "<path you want to clone>/*" >> .git/info/sparse-checkout
git pull --depth=1 origin <branch you want to fetch>
git init MyFolder
cd MyFolder
git remote add origin git@github.com:android/compose-samples.git
git config core.sparsecheckout true
echo "Jetsurvey/*" >> .git/info/sparse-checkout
git pull --depth=1 origin main
@Chronial的答案已经不适用于最近的版本,但它提供了一个有用的脚本。
根据我收集的信息和我只想检出分支的子目录这一事实,我创建了以下shell函数。它获取提供的目录中分支最新版本的浅层副本。
function git_sparse_clone_branch() (
rurl="$1" localdir="$2" branch="$3" && shift 3
git clone "$rurl" --branch "$branch" --no-checkout "$localdir" --depth 1 # limit history
cd "$localdir"
# git sparse-checkout init --cone # fetch only root file
# Loops over remaining args
for i; do
git sparse-checkout set "$i"
done
git checkout "$branch"
)
因此,以下是使用示例:
git_sparse_clone_branch git@github.com:user/repo.git localpath branch-to-clone path1_to_fetch path2_to_fetch
在我的情况下,克隆版本只有23MB,而完整克隆版本为385MB。
使用git版本2.36.1测试。
"mydir/myfolder"
我不得不使用
mydir/myfolder
此外,如果您只想下载所有子目录,请使用以下命令:
git sparse-checkout set *
git clone
最简单的命令是什么?我使用了这个简单的答案。如果有更简单的方法,请评论。 - Peter Krauss