How to sparsely checkout only one single file from a git repository?

前端 未结 21 1992
Happy的楠姐
Happy的楠姐 2020-11-22 08:14

How do I checkout just one file from a git repo?

21条回答
  •  爱一瞬间的悲伤
    2020-11-22 08:57

    Normally it's not possible to download just one file from git without downloading the whole repository as suggested in the first answer. It's because Git doesn't store files as you think (as CVS/SVN do), but it generates them based on the entire history of the project.

    But there are some workarounds for specific cases. Examples below with placeholders for user, project, branch, filename.

    GitHub

    wget https://raw.githubusercontent.com/user/project/branch/filename
    

    GitLab

    wget https://gitlab.com/user/project/raw/branch/filename
    

    GitWeb

    If you're using Git on the Server - GitWeb, then you may try in example (change it into the right path):

    wget "http://example.com/gitweb/?p=example;a=blob_plain;f=README.txt;hb=HEAD"
    

    GitWeb at drupalcode.org

    Example:

    wget "http://drupalcode.org/project/ads.git/blob_plain/refs/heads/master:/README.md"
    

    googlesource.com

    There is an undocumented feature that allows you to download base64-encoded versions of raw files:

    curl "https://chromium.googlesource.com/chromium/src/net/+/master/http/transport_security_state_static.json?format=TEXT" | base64 --decode
    

    In other cases check if your Git repository is using any web interfaces.

    If it's not using any web interface, you may consider to push your code to external services such as GitHub, Bitbucket, etc. and use it as a mirror.

    If you don't have wget installed, try curl -O (url) alternatively.

提交回复
热议问题