Git sparse checkout for “partial checkout”

Posted by Harald Nezbeda on Mon 18 December 2017

This isn't really ground breaking and it’s not often used. I can imagine this scenario when having a bigger project where two components live together (like a webapp that has the API and the Javascript App in the same repository) and you want to checkout just one of them. Of course the best solution for this might be to use a submodule, but in this case you may have not looked for this approach anyway.

So what is required? Start by defining a new directory and inside that directory initialize an empty repository. After that activate the sparse checkout mode for the repository, add the remote of the repository and configure the folders required for checkout into sparse mode. From here on you can checkout the different brunches and get only the directories or file marked for checkout.

Here are the console commands explained above:

  1. mkdir repo -dir
  2. cd repo -dir
  3. git init
  4. git config core.sparseCheckout true
  5. git remote add -f origin git://repo-location
  6. echo "required_subdir/*" > .git/info/sparse-checkout
  7. git checkout [branchname]
  8. git pull