Search code examples
gitsparse-checkout

Git sparse checkout to only honor a folder at the root of the repo?


We have a repo with a bunch of projects. I am only concern with the "oracle" project within the repo. However, upon pulling down the codebase, I found subdirectories called "oracle" within other folders. Is there a way to only grab the parent folder that matches?

git init <repo>
cd <repo>
git remote add -f origin <url>

git config core.sparseCheckout true

echo "oracle/" >> .git/info/sparse-checkout

Output

#ls -l
  oracle
  directory123
  directoryabc

#find . | grep "oracle"
  directory123/sub1/sub2/oracle
  directoryabc/sub1/sub2/sub3/oracle

Solution

  • By writing oracle/ to $GIT_DIR/info/sparse-checkout, you're telling your Git repo to only populate the working directory with files (if any) whose parent directory is called "oracle". Directories

    oracle/
    directory123/sub1/sub2/oracle/
    directoryabc/sub1/sub2/sub3/oracle/
    

    all satisfy this condition.

    If you want to restrict the sparse checkout to the oracle directory that is at the root of your Git repo, you need to write, not oracle/, but /oracle/ (note the leading forward slash) to $GIT_DIR/info/sparse-checkout.