[英]Git commit/Push/Pull with sparse checkout
I started Git yesterday and before that I was using SVN since many years.我昨天开始使用 Git,在此之前我已经使用 SVN 很多年了。 Let me explain you what exactly I am trying to achieve:
让我向您解释一下我想要达到的目标:
I have a main git repo for different projects.我有一个用于不同项目的主要 git 仓库。 Something like this:
像这样的东西:
I can't create separate repos for these sub-projects.我无法为这些子项目创建单独的存储库。 Now I (or other users) should be able to checkout/commit/push/pull in these independent projects.
现在我(或其他用户)应该能够在这些独立项目中签出/提交/推送/拉取。
For this thing I am trying sparse-checkout with following commands:对于这件事,我正在尝试使用以下命令进行稀疏结帐:
mkdir proj1; cd proj1
git init
git remote add origin https://main_repo
git config core.sparsecheckout true
echo "proj1/" > .git/info/sparse-checkout
git pull origin master
Now what I need is to get all the files present in proj1 in the same current dir.现在我需要的是在同一个当前目录中获取 proj1 中存在的所有文件。 What I am getting is something like this:
我得到的是这样的:
/proj1/proj1/files_and_dirs_in_proj1
What I need:我需要的:
/proj1/files_and_dirs_in_proj1
Second this is that the checkout-dir doesn't behaves like a git repo.其次, checkout-dir 的行为不像 git repo。 It doesn't carry any .git dir.
它不携带任何 .git 目录。 So I don't understand how to do commits/push/pull in sparse-checkedout projs.
所以我不明白如何在 sparse-checkedout projs 中进行提交/推/拉。
I hope I explained it well.我希望我解释得很好。 Please suggest.
请建议。
If I have correctly understood you then you want to:如果我正确理解了你,那么你想要:
In this case you can use this simple steps:在这种情况下,您可以使用以下简单步骤:
1) Add your main repo to git 1) 将你的主仓库添加到 git
git init
git remote add origin ...
git add .
git commit -m 'Initial commit'
git push
2) Clone & adjust your git repos for each commands (teamproj1, teamproj2, teamproj3, ...). 2) 为每个命令(teamproj1、teamproj2、teamproj3...)克隆并调整您的 git 存储库。 Repeat code below for each team/project
为每个团队/项目重复下面的代码
git clone ... teamproj1
cd teamproj1
git config core.sparsecheckout true
echo 'proj1' › .git/info/sparse-checkout
git read-tree -m -u HEAD
3) Bingo. 3) 宾果游戏。 Each command will be with its own folder and project.
每个命令都有自己的文件夹和项目。 This folder is normal git repo, but this repo will show only some files which will be in
.git/info/sparse-checkout
这个文件夹是普通的 git repo,但这个 repo 只会显示一些文件,这些文件将在
.git/info/sparse-checkout
I've added some drawings to facilitate understanding of this scheme我添加了一些图纸以方便理解这个方案
The recent git sparse-checkout
I mention here can help, especially when combined with git clone --filter=blob:none --no-checkout
我在这里提到的最近
git sparse-checkout
可以提供帮助,尤其是与git clone --filter=blob:none --no-checkout
结合使用时
git clone --filter=blob:none --no-checkout https://github.com/<me>/<myrepo>
git config core.sparseCheckoutCone false
git sparse-checkout disable
# Add the expected pattern, to include just a subfolder without top files:
git sparse-checkout set /mySubFolder/
# populate working-tree with only the right files:
git read-tree -mu HEAD
Plus, with Git 2.32 (Q2 2021), " git add
" ( man ) and git rm
( man ) learned not to touch those paths that are outside of sparse checkout.另外,在 Git 2.32(2021 年第 2 季度)中,“
git add
” ( man )和git rm
( man )学会了不要触及稀疏结帐之外的那些路径。
So you won't make any mistake with paths outside of the relevant submodules.因此,您不会对相关子模块之外的路径犯任何错误。
See commit d5f4b82 , commit a20f704 , commit b243012 , commit 719630e , commit d73dbaf , commit 6594afc , commit 4e95698 (08 Apr 2021) by Matheus Tavares ( matheustavares
) .请参阅由Matheus Tavares (
matheustavares
) 提交的 d5f4b82 、 提交 a20f704 、 提交 b243012 、 提交 719630e 、 提交 d73dbaf 、 提交 6594afc 、 提交 4e95698 (2021 年 4 月 8 日)
(Merged by Junio C Hamano -- gitster
-- in commit fe069dc , 07 May 2021) (由Junio C
gitster
合并gitster
在fe069dc 提交中,2021 年 5 月 7 日)
rm
: honor sparse checkout patternsrm
:尊重稀疏结帐模式Suggested-by: Elijah Newren
推荐人:Elijah Newren
Signed-off-by: Matheus Tavares签字人:马修斯·塔瓦雷斯
git add
( man ) refrains from adding or updating index entries that are outside the current sparse checkout, butgit rm
( man ) doesn't follow the same restriction.git add
( man )避免添加或更新当前稀疏结帐之外的索引条目,但git rm
( man )不遵循相同的限制。
This is somewhat counter-intuitive and inconsistent.这有点违反直觉和不一致。
So makerm
honor the sparsity rules and advise on how to removeSKIP_WORKTREE
entries just likeadd
does.因此,让
rm
遵守稀疏规则,并就如何像add
一样删除SKIP_WORKTREE
条目提供建议。
Also add some tests for the new behavior.还为新行为添加一些测试。
git config
now includes in its man page : git config
现在包含在其手册页中:
Advice shown when either
git add
orgit rm
is asked to update index entries outside the current sparse checkout.当要求
git add
或git rm
更新当前稀疏结帐之外的索引条目时显示的建议。
git rm
now includes in its man page : git rm
现在包含在其手册页中:
allowing the file to be removed from just the index.
允许仅从索引中删除文件。 When sparse-checkouts are in use (see
git sparse-checkout
),git rm
will only remove paths within the sparse-checkout patterns.当使用稀疏结帐时(请参阅
git sparse-checkout
),git rm
只会删除稀疏结帐模式中的路径。
With Git 2.34 (Q4 2021), " git add
" ( man ) can work better with the sparse index.在 Git 2.34(2021 年第四季度)中,“
git add
” ( man )可以更好地处理稀疏索引。
See commit 42f8ed6 , commit 939fa07 , commit 4eaffd8 , commit 5e7cbab , commit 83ad8ca (29 Jul 2021) by Derrick Stolee ( derrickstolee
) .见提交42f8ed6 , 提交939fa07 , 提交4eaffd8 , 提交5e7cbab , 提交83ad8ca由(2021年7月29日)井架Stolee(
derrickstolee
) 。
(Merged by Junio C Hamano -- gitster
-- in commit 2f71366 , 24 Aug 2021) (由Junio C
gitster
合并-- gitster
-- in commit 2f71366 ,2021 年 8 月 24 日)
add
: ignore outside the sparse-checkout in refresh()add
:在 refresh() 中忽略稀疏结帐之外Reviewed-by: Elijah Newren
审核人:以利亚纽伦
Signed-off-by: Derrick Stolee签字人:德里克·斯托利
Since b243012 (
refresh_index()
: add flag to ignoreSKIP_WORKTREE
entries, 2021-04-08, Git v2.32.0-rc0 -- merge listed in batch #14 )(refresh_index()
: add flag to ignoreSKIP_WORKTREE
entries, 2021-04-08), 'git add --refresh
' ( man )<path>
will output a warning message when the path is outside the sparse-checkout definition.由于b243012 (
refresh_index()
:添加标志以忽略SKIP_WORKTREE
条目,2021-04-08,Git v2.32.0-rc0 -- 合并列在第 14 批中)(refresh_index()
:添加标志以忽略SKIP_WORKTREE
条目,2021-04 -08), 'git add --refresh
' ( man )<path>
将在路径超出稀疏结帐定义时输出警告消息。
The implementation of this warning happened in parallel with the sparse-index work to addensure_full_index()
calls throughout the codebase.此警告的实现与 sparse-index 工作并行发生,以在整个代码库中添加
ensure_full_index()
调用。Update this loop to have the proper logic that checks to see if the pathspec is outside the sparse-checkout definition.
更新此循环以使用正确的逻辑来检查 pathspec 是否在 sparse-checkout 定义之外。
This avoids the need to expand the sparse directory entry and determine if the path is tracked, untracked, or ignored.这避免了扩展稀疏目录条目并确定路径是跟踪、未跟踪还是忽略的需要。
We simply avoid updating the stat() information because there isn't even an entry that matches the path!我们只是避免更新 stat() 信息,因为甚至没有与路径匹配的条目!
First off, git is not svn, different concepts, different approaches.首先,git 不是 svn,不同的概念,不同的方法。 Git is a full source-control system on the client, not a front-end to a remote server
Git 是客户端上的完整源代码控制系统,而不是远程服务器的前端
And one of the differences is you clone the whole repository and not subsections.区别之一是您克隆整个存储库而不是小节。 I believe there are ways to make it appear that you've only cloned a subset but in reality the whole repo is there.
我相信有一些方法可以让您看起来只是克隆了一个子集,但实际上整个 repo 都在那里。
In the testing/usage I've done, it doesn't matter.在我完成的测试/使用中,这无关紧要。 Yes, perhaps the initial cloning is a little slower but after that it's really fast.
是的,也许最初的克隆有点慢,但之后真的很快。 My company has a large 4G+ source tree repository, 100s of developers, everyone has a complete copy of the repo even though their responsibility might be segmented, even remote over our mediocre corporate WAN there have been no problems.
我公司有一个大型 4G+ 源代码树存储库,有 100 名开发人员,每个人都有一份完整的存储库副本,即使他们的职责可能被分割,即使通过我们平庸的企业 WAN 远程也没有问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.