Git稀疏检出子文件夹并将其放置在我的存储库文件夹内的不同位置

huangapple go评论69阅读模式
英文:

Git sparse checkout a subfolder into a different location inside my repo folder

问题

假设我们有一个带有三级子文件夹的Git仓库:

|_
|_Folder1
|_Folder2
        |_ SubFolder2
                    |_ SubSubFolder2

我需要对这个文件夹进行稀疏检出,并将其放置在C:\MyRepo文件夹的不同位置(在Windows上) - 去掉前导的 Folder2 目录:

C:\MyRepo
        |_ SubFolder2
                    |_ SubSubFolder2

是否有可能以某种方式实现这一点,还是Git稀疏检出总是尊重原始(远程仓库)文件夹结构,无法更改?

P.S. 我尝试了以下标准代码:

git clone --no-checkout --sparse <remoteRepoURL> "C:\MyRepo"
cd /d "C:\MyRepo"
git sparse-checkout set "Folder2/SubFolder2/SubSubFolder2"
git checkout

但我得到了原始(远程仓库)文件夹结构:

C:\MyRepo
        |_Folder2
                |_ SubFolder2
                            |_ SubSubFolder2

是否有一种方法可以更改该默认行为?

P.P.S. 7年前曾提出类似的问题 - 那里仍然没有明确的答案。

英文:

Lets assume we have a Git repo with a three-level subfolder:

|_
|_Folder1
|_Folder2
        |_ SubFolder2
                    |_ SubSubFolder2

I need to sparse checkout this folder and place it into a different location inside C:\MyRepo folder (on Windows) - get rid of leading Folder2 directory:

C:\MyRepo
        |_ SubFolder2
                    |_ SubSubFolder2

Is that possible somehow or Git sparse checkout always respect original (remote repo) folder structure and that cannot be changed?

P.S. I've tried the following standard code:

git clone --no-checkout --sparse <remoteRepoURL> "C:\MyRepo"
cd /d "C:\MyRepo"
git sparse-checkout set "Folder2/SubFolder2/SubSubFolder2"
git checkout

But I get original (remote repo) folder structure:

C:\MyRepo
        |_Folder2
                |_ SubFolder2
                            |_ SubSubFolder2

Is there a way to change that default behaviour?

P.P.S. There was a similar question asked 7 years ago - still no clear answer there..

答案1

得分: 0

在git-bash中,首先将树读入临时索引文件中。

GIT_INDEX_FILE=/tmp/.tmp.index git read-tree ${revision}:${parent_dir}

# 在你的情况下可能是
GIT_INDEX_FILE=/tmp/.tmp.index git read-tree HEAD:_Folder2

然后,从索引中检出文件夹和文件到目标位置。

GIT_INDEX_FILE=/tmp/.tmp.index git checkout-index -f -a --prefix=/c/MyRepo/

移除临时索引。我们也可以使用mktemp命令创建一个随机的临时文件,以避免命名冲突。

rm -rf /tmp/.tmp.index

请注意,在Windows中/c/MyRepo/也可以是c:\\MyRepo\\。结尾的/\\不能缺失。

对于Windows CMD(我尝试过Microsoft Windows [Version 10.0.19042.1165]),

set GIT_INDEX_FILE=%TEMP%\.tmp.index
git read-tree HEAD:_Folder2
git checkout-index -f -a --prefix=c:\MyRepo\
set GIT_INDEX_FILE=
del %TEMP%\.tmp.index

我对Windows CMD不太熟悉,所以不确定这两个set命令是否正确。

英文:

In git-bash, first read the tree into a temporary index file.

GIT_INDEX_FILE=/tmp/.tmp.index git read-tree ${revision}:${parent_dir}

# In your case it could be
GIT_INDEX_FILE=/tmp/.tmp.index git read-tree HEAD:_Folder2

Then, checkout the folders and files from the index to the destination.

GIT_INDEX_FILE=/tmp/.tmp.index git checkout-index -f -a --prefix=/c/MyRepo/

Remove the temporary index. We can also use the command mktemp to create a random tempfile, avoiding naming conflicts.

rm -rf /tmp/.tmp.index

Note that on Windows /c/MyRepo/ could also be c:\\MyRepo\\. The ending / and \\ cannot be missing.

For Windows CMD (I tried with Microsoft Windows [Version 10.0.19042.1165]),

set GIT_INDEX_FILE=%TEMP%\.tmp.index
git read-tree HEAD:_Folder2
git checkout-index -f -a --prefix=c:\MyRepo\
set GIT_INDEX_FILE=
del %TEMP%\.tmp.index

I'm not familiar with Windows CMD, so I'm not sure if the 2 set commands are proper.

huangapple
  • 本文由 发表于 2023年5月29日 13:01:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/76354780.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定