57

I'm trying to test something on a wordpress install. In doing so, I'd like to quickly replicate the repo. However, the upload directory (wp-content/uploads) is massive, so I'd like to ignore it.

Note: I don't want to .gitignore this directory all the time, just for this scenario.

Basically, I'd like a command like this pseudo code: git clone --ignore wp-content/uploads.

Is the best way to add that directory to .gitignore, clone, and then revert .gitignore? Or is there a better method?

1

5 Answers 5

31

A bit late to the party, but: Don't you want a sparse checkout?

mkdir <repo> && cd <repo>
git init
git remote add –f <name> <url>

Enable sparse-checkout:

git config core.sparsecheckout true

Configure sparse-checkout by listing your desired sub-trees in .git/info/sparse-checkout:

echo some/dir/ >> .git/info/sparse-checkout
echo another/sub/tree >> .git/info/sparse-checkout

Checkout from the remote:

git pull <remote> <branch>

See http://jasonkarns.com/blog/subdirectory-checkouts-with-git-sparse-checkout/ for more info.

2
  • 12
    This still fetches everything, even though only the named directories/files are checked out into the local sandbox. Jul 28, 2017 at 9:48
  • You genius you, I never thought of this fixing my issue.
    – SlyOtis
    Oct 13, 2021 at 8:04
11

git clone will always clone the complete repository*, including all previous commits ever added to the repository. So even if you remove the files temporarily, and clone it then, you will still receive the older versions which do contain those files.

Also, just editing the .gitignore will not remove tracked files from the repository even if they would normally be ignored.

So no, it is not really possible to skip a certain folder during cloning.

*It is possible to limit the amount of commits retrieved during a clone, but this will not make the repository very usable.

0
6

you can specify the depth to clone (--depth=1 to get only 1 commit). You may want to set up a branch with this directory missing and then clone with depth of one only. Since git is snapshot based, it's not easy to exclude a part of a commit when cloning. This is the closest to what you want.

If you have full control of this repo, you may want to make a submodule of this directory and only do a submodule update when you want to admin that part.

1
4

On the server:

 git checkout master^0    # the ^0 checks out the commit itself, not the branch
 git filter-branch --tree-filter 'git rm -r wp-content/uploads' HEAD
 git checkout -b filtered

(filter-branch on a big project here generates new history at about 2-3 commits per second)

Then, anywhere you like,

 git init
 git remote add gimme your://repo/path
 git fetch gimme filtered

edit: fixed syntax errors according to http://git-scm.com/docs/git-filter-branch

1
  • For some reason, the above filter-branch command sometimes works and sometimes fails with an error that "the following files have changes staged in the index". Also, it will fail if the given path doesn't exist in every commit (it seems you need git rm --ignore-unmatch in that case.) But a faster alternative is to use --index-filter and git rm --cached instead: git filter-branch --index-filter 'git rm --cached --ignore-unmatch -q -r wp-content/uploads' HEAD
    – floppus
    Jun 22, 2021 at 21:36
0

I feel the previous answers are too complicated to me. So I forked the github repo and deleted the directory. Then I downloaded my fork's zip directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.