0

My Requirement was to checkout a specific directory from a repo and also checkout each branch which has that directory and copy each branch directory to a target path.

For Example football is a repo and src/ronaldo is the directory which I am trying to checkout and football repo has Branch A,B,C,D. But src/ronaldo is only in branch A and B. Now I wanna checkout the directory from Branch A and B and copy it to target folder Messi.

I tried to do a sparse checkout a specific directory in a repo and I am able to do it using the below commands.

git clone --filter=blob:none --no-checkout --depth 1 --sparse <project-url>
cd <project>
Specify the folders you want to clone
 
git sparse-checkout add <folder1> <folder2>
git checkout

But I am not able to find the rest of the branch which has the directory. What other git commands need to be added here.

1 Answers1

0

You could use git rev-parse ${commit-ish}:${path} to test if the commit-ish tracks a path. If it succeeds, it returns the sha1 value of the tree object. If it does not, it returns literally ${commit-ish}:${path} and the exit code is not 0.

git rev-parse origin/A:src/ronaldo  # returns a sha1 value and $? is 0
git rev-parse origin/C:src/ronaldo  # returns origin/C:src/ronaldo and $? is 128

There is another method to implement your requirement, without checkout and copy.

After clone, read the tree (if it exists) into a temporary index file.

GIT_INDEX_FILE=/tmp/.tmp.index git read-tree ${commit-ish}:${path}

Then checkout the index into the target folder.

GIT_INDEX_FILE=/tmp/.tmp.index git checkout-index -f -a --prefix=/path/to/foo/
rm -rf /tmp/.tmp.index

Note that the ending / cannot be omitted.

ElpieKay
  • 27,194
  • 6
  • 32
  • 53