git - checkout single file under bare repository

后端 未结 3 2044
不思量自难忘°
不思量自难忘° 2021-02-19 07:47

On the server I have bare repository which is origin for development process and to simplify deployment to QA environment.

So in post-receive it simply does

相关标签:
3条回答
  • 2021-02-19 08:06

    I know this is ooooooooold, but I found my own use-case for this functionality and looked around for a while for a better solution to this before combining a few solutions into a simple one-liner:

    GIT_WORK_TREE=/home/dev git checkout $branch -- deploy.sh
    

    In my case, I just wanted to be able to "peek" into some of my bare repositories without unpacking the whole thing (some of them are huge). People were talking about sparse checkouts and other such things, but I just needed one-off functionality. To check out just the "Documents/Health Records" folder, for example, I would do the following:

    GIT_WORK_TREE=/tmp/my-files git checkout master -- "Documents/Health Records"
    

    And lo! There it did appear.

    0 讨论(0)
  • 2021-02-19 08:10

    This git show or similar git cat-file blob approaches work more-or-less fine for text files, but they are hopeless for binary files.

    Better approach which works reliably for any kind of files and even allows to checkout entire folders:

    git archive mybranch folder/file.txt --output result.tar
    

    It creates a tar archive with desired content, exactly the file that sits in the source control. Works perfectly fine with binary files.

    The only thing you need to do is to extract this tar file

    tar -xf result.tar
    

    See my blogpost for more details

    0 讨论(0)
  • 2021-02-19 08:31

    As I explain in "checkout only one file from git", you cannot checkout just one file without cloning or fetching first.

    But you git show that file, which means you can dump its content into a /another/path./deploy.sh file, and execute that file.

    git-show HEAD:full/repo/path/to/deploy.sh > /another/path./deploy.sh
    /another/path./deploy.sh
    

    Since you execute that from a post-receive hook, the git-show will show the latest version of the deploy.sh file.


    The other alternative would be try

     GIT_WORK_TREE=$SOURCE_PATH git checkout -- path/to/deploy.sh
    

    And checkout only that file, directly in your working tree.

    The '--' help the git command to understand it is a file, not another parameter like a tag or a named branch.

    From the OP AlexKey's test, it requires that the working tree has been checked out (fully) at least once.

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