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

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

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

相关标签:
21条回答
  • 2020-11-22 09:09

    In git you do not 'checkout' files before you update them - it seems like this is what you are after.

    Many systems like clearcase, csv and so on require you to 'checkout' a file before you can make changes to it. Git does not require this. You clone a repository and then make changes in your local copy of repository.

    Once you updated files you can do:

    git status
    

    To see what files have been modified. You add the ones you want to commit to index first with (index is like a list to be checked in):

    git add .
    

    or

    git add blah.c
    

    Then do git status will show you which files were modified and which are in index ready to be commited or checked in.

    To commit files to your copy of repository do:

    git commit -a -m "commit message here"
    

    See git website for links to manuals and guides.

    0 讨论(0)
  • 2020-11-22 09:09

    I am adding this answer as an alternative to doing a formal checkout or some similar local operation. Assuming that you have access to the web interface of your Git provider, you might be able to directly view any file at a given desired commit. For example, on GitHub you may use something like:

    https://github.com/hubotio/hubot/blob/ed25584f/src/adapter.coffee
    

    Here ed25584f is the first 8 characters from the SHA-1 hash of the commit of interest, followed by the path to the source file.

    Similary, on Bitbucket we can try:

    https://bitbucket.org/cofarrell/stash-browse-code-plugin/src/06befe08
    

    In this case, we place the commit hash at the end of the source URL.

    0 讨论(0)
  • 2020-11-22 09:12

    I don’t see what worked for me listed out here so I will include it should anybody be in my situation.

    My situation, I have a remote repository of maybe 10,000 files and I need to build an RPM file for my Linux system. The build of the RPM includes a git clone of everything. All I need is one file to start the RPM build. I can clone the entire source tree which does what I need but it takes an extra two minutes to download all those files when all I need is one. I tried to use the git archive option discussed and I got “fatal: Operation not supported by protocol.” It seems I have to get some sort of archive option enabled on the server and my server is maintained by bureaucratic thugs that seem to enjoy making it difficult to get things done.

    What I finally did was I went into the web interface for bitbucket and viewed the one file I needed. I did a right click on the link to download a raw copy of the file and selected “copy shortcut” from the resulting popup. I could not just download the raw file because I needed to automate things and I don’t have a browser interface on my Linux server.

    For the sake of discussion, that resulted in the URL:

    https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest
    

    I could not directly download this file from the bitbucket repository because I needed to sign in first. After a little digging, I found this worked: On Linux:

    echo "myUser:myPass123"| base64
    bXlVc2VyOm15UGFzczEyMwo=
    
    curl -H 'Authorization: Basic bXlVc2VyOm15UGFzczEyMwo=' 'https://ourArchive.ourCompany.com/projects/ThisProject/repos/data/raw/foo/bar.spec?at=refs%2Fheads%2FTheBranchOfInterest' > bar.spec
    

    This combination allowed me to download the one file I needed to build everything else.

    0 讨论(0)
提交回复
热议问题