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

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

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

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

    Now we can! As this is the first result on google, I thought I'd update this to the latest standing. With the advent of git 1.7.9.5, we have the git archive command which will allow you to retrieve a single file from a remote host.

    git archive --remote=git://git.foo.com/project.git HEAD:path/in/repo filename | tar -x
    

    See answer in full here https://stackoverflow.com/a/5324532/290784

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

    Very simple:

    git checkout from-branch-name -- path/to/the/file/you/want
    

    This will not checkout the from-branch-name branch. You will stay on whatever branch you are on, and only that single file will be checked out from the specified branch.

    Here's the relevant part of the manpage for git-checkout

    git checkout [-p|--patch] [<tree-ish>] [--] <pathspec>...
           When <paths> or --patch are given, git checkout does not switch
           branches. It updates the named paths in the working tree from the
           index file or from a named <tree-ish> (most often a commit). In
           this case, the -b and --track options are meaningless and giving
           either of them results in an error. The <tree-ish> argument can be
           used to specify a specific tree-ish (i.e. commit, tag or tree) to
           update the index for the given paths before updating the working
           tree.
    

    Hat tip to Ariejan de Vroom who taught me this from this blog post.

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

    If you already have a copy of the git repo, you can always checkout a version of a file using a git log to find out the hash-id (for example 3cdc61015724f9965575ba954c8cd4232c8b42e4) and then you simply type:

    git checkout hash-id path-to-file
    

    Here is an actual example:

    git checkout 3cdc61015724f9965575ba954c8cd4232c8b42e4 /var/www/css/page.css
    
    0 讨论(0)
  • 2020-11-22 09:07

    Two variants on what's already been given:

    git archive --format=tar --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | tar -O -xf -
    

    and:

    git archive --format=zip --remote=git://git.foo.com/project.git HEAD:path/to/directory filename | funzip
    

    These write the file to standard output.

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

    Here is the complete solution for pulling and pushing only a particular file inside git repository:

    1. First you need to clone git repository with a special hint –no checkout
    git clone --no-checkout <git url>
    
    1. The next step is to get rid of unstaged files in the index with the command:
    git reset
    
    1. Now you are allowed to start pulling files you want to change with the command:
    git checkout origin/master <path to file>
    
    1. Now the repository folder contains files that you may start editing right away. After editing you need to execute plain and familar sequence of commands.
    git add <path to file>
    git commit -m <message text>
    git push
    
    0 讨论(0)
  • 2020-11-22 09:08

    Working in GIT 1.7.2.2

    For example you have a remote some_remote with branches branch1, branch32

    so to checkout a specific file you call this commands:

    git checkout remote/branch path/to/file
    

    as an example it will be something like this

    git checkout some_remote/branch32 conf/en/myscript.conf
    git checkout some_remote/branch1 conf/fr/load.wav
    

    This checkout command will copy the whole file structure conf/en and conf/fr into the current directory where you call these commands (of course I assume you ran git init at some point before)

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