Github 如何下载仓库的部分文件
Update April 2021: there are a few tools created by the community that can do this for you:
- Download Directory (Credits to fregante)
- It has also been integrated into the excellent Refined GitHub Chrome extension as a button in the GitHub web user interface.
- GitZip (Credits to Kino—see his answer here)
- DownGit (Credits to Minhas Kamal—see his answer here)
Note: if you're trying to download a large number of files, you may need to provide a token to these tools to avoid rate limiting.
Original (manual) approach: Checking out an individual directory is not supported by Git natively, but GitHub can do this via Subversion (SVN). If you checkout your code with Subversion, GitHub will essentially convert the repository from Git to Subversion on the backend, and then serve up the requested directory.
Update November 2024: The Subversion support has been removed after January 8, 2024: https://github.blog/news-insights/product-news/sunsetting-subversion-support/. The rest of this answer is outdated and describes the functionality in the past.
Here's how you can use this feature to download a specific folder. I'll use the popular JavaScript library Lodash as an example.
Navigate to the folder you want to download. Let's download
/test
frommaster
branch.Modify the URL for subversion. Replace
tree/master
withtrunk
.https://github.com/lodash/lodash/tree/master/test
➜https://github.com/lodash/lodash/trunk/test
Download the folder. Go to the command line and grab the folder with SVN.
svn checkout https://github.com/lodash/lodash/trunk/test
You might not see any activity immediately because GitHub takes up to 30 seconds to convert larger repositories, so be patient.
Full URL format explanation:
- If you're interested in
master
branch, usetrunk
instead. So the full path istrunk/foldername
- If you're interested in
foo
branch, usebranches/foo
instead. The full path looks likebranches/foo/foldername
- Pro tip: You can use
svn ls
to see available tags and branches before downloading if you wish