How to pull specific directory with git

I have a project with git, and I just want to clone or pull a specific directory, like myproject/javascript just like subversion does.
make some changes, commit and push back again.
It's possible?


Solution 1:

  1. cd into the top of your repo copy
  2. git fetch
  3. git checkout HEAD path/to/your/dir/or/file

    • Where "path/..." in (3) starts at the directory just below the repo root containing your ".../file"

    • NOTE that instead of "HEAD", the hash code of a specific commit may be used, and then you will get the revision (file) or revisions (dir) specific to that commit.

Solution 2:

In an empty directory:

git init
git remote add [REMOTE_NAME] [GIT_URL]
git fetch REMOTE_NAME
git checkout REMOTE_NAME/BRANCH -- path/to/directory

Solution 3:

After much looking for an answer, not finding, giving up, trying again and so on, I finally found a solution to this in another SO thread:

How to git-pull all but one folder

To copy-paste what's there:

git init
git remote add -f origin <url>
git config core.sparsecheckout true
echo <dir1>/ >> .git/info/sparse-checkout
echo <dir2>/ >> .git/info/sparse-checkout
echo <dir3>/ >> .git/info/sparse-checkout
git pull origin master

To do what OP wants (work on only one dir), just add that one dir to .git/info/sparse-checkout, when doing the steps above.

Many many thanks to @cforbish !