Github 如何下载仓库的部分文件

https://stackoverflow.com/questions/7106012/download-a-single-folder-or-directory-from-a-github-repository


Update April 2021: there are a few tools created by the community that can do this for you:

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.

  1. Navigate to the folder you want to download. Let's download /test from master branch.

    GitHub repository URL example

  2. Modify the URL for subversion. Replace tree/master with trunk.

    https://github.com/lodash/lodash/tree/master/test

    https://github.com/lodash/lodash/trunk/test

  3. 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, use trunk instead. So the full path is trunk/foldername
  • If you're interested in foo branch, use branches/foo instead. The full path looks like branches/foo/foldername
  • Pro tip: You can use svn ls to see available tags and branches before downloading if you wish
posted @ 2024-12-24 20:19  dewxin  阅读(28)  评论(0编辑  收藏  举报