How can I checkout specific folder and its content from a specific commit to external folder?

StackOverflow https://stackoverflow.com/questions/18105037

  •  23-06-2022
  •  | 
  •  

質問

How do I retrieve a complete subfolder from a git (version 1.7.0.4) repository to a local folder WITHOUT checking out the whole repo in the first place? I thought that this would be the solution:

git --work-tree=/home/tmp/testcheckout checkout COMMITID -- images/*

Example repo structure with some directories:

/git/project1/sourcecode  
/git/project1/images  
/git/project1/howto  
/git/project1/readme.txt

So the example command should checkout all files under /git/project1/images to /home/tmp/testcheckout that have been committed to that git repo up to commit COMMITID but it only checks out files that have been committed at that given COMMITID, ignoring all other files that have been committed before that.

Problem:
With the command above I only get the files from that subfolder that have been committed with the COMMITID.

Question:
What I want is to retrieve ALL files up to that COMMITID. Any solution for this?

Important: I don't want to clone the whole repository (700GB) just to be able to retrieve 1GB of files later. So there must be a way to directly get the files out of a repo without cloning the whole repo.

役に立ちましたか?

解決

Here is a working solution to this. The solution has the following features:
- It does not checkout the WHOLE repo, it just checks out what you need (this comes in very handy if you have repos of 100GB and more) - You create a new repo as target, so nothing gets changed in your original repo, nice ey?

Solution

git init /example/gittest
cd /example/gittest
git remote add -f origin /PATH/TO/SOURCE/REPO/.git
git config core.sparsecheckout true
echo images >> .git/info/sparse-checkout
git pull origin master

Now it copies only the imagesfolder from your original repo to this test repo. Then you can do with the data what you want.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top