0

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..

bairog
  • 3,143
  • 6
  • 36
  • 54
  • As I know you cannot do this with sparse checkout. But can you explain, why you need this maybe we can suggest sth else? e.g do you need Folder1 or do you need to work with just one standalone folder. Are these folders have dependencies? And are you admin of the repo, can you change the structure? – Alperen Tahta May 29 '23 at 05:45

1 Answers1

0

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.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53
  • Is Git Bash necessary or maybe same commands can be executed in CMD.exe? – bairog May 29 '23 at 07:31
  • Your 2 `set` commands are correct. Technically I get folder structure that I need. But it's impossible to work with them: TortoiseGit `Show Log` dialog shows no commits (and 0 revisions) for that folder, `Git Commit` dialog hangs forever on **Please wait...** message.. – bairog May 30 '23 at 04:31
  • @bairog It only checks out `SubFolder2/SubSubFolder2`. The new folder is not a git repository, and it is not part of a git repository either. – ElpieKay May 30 '23 at 06:24
  • So for Git there is no way to checkout a folder into a different location (get rid of a leading directory) AND still work with it like ordinary repository part - right? It is possible in SVN and I miss this functionality.. – bairog May 30 '23 at 06:32
  • @bairog then it needs to rewrite the history, which is roughly equivalent to maintain another branch. – ElpieKay May 30 '23 at 06:40