Search code examples
gitsparse-checkout

How do I check out only top level files from git?


I am trying to check out a specific file (.gitignore) committed to my git branch, and no other files.

I am able to use

git checkout origin/master "build/system/.gitignore",

but when I use

git checkout origin/master ".gitignore"

this doesn't work. (I get the error "error: pathspec '.gitignore' did not match any file(s) known to git.)

I have a sparse checkout of git where currently only certain directories are included in my checkout. I would like to include certain files as well.

Is this possible, and if so, how?


Solution

  • Apparently git sparse-checkout doesn't work properly with files, but there seems to be a work around:

    Add the file under .git/info/sparse-checkout (Add /.gitignore)

    Perform a git operation, such as changing branches.

    .gitignore will then populate in your local repository.