如何从git仓库只签出一个文件('稀疏结帐')? [英] How to checkout only one file from git repository ('sparse checkout')?

查看:182
本文介绍了如何从git仓库只签出一个文件('稀疏结帐')?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>




这将从当前目录的远程仓库的 HEAD 中保存一份'文件名'的副本。



:path / to / directory 部分是可选的。如果排除,则获取的文件将被保存到<当前工作目录> /路径/目录/文件名


Fizer Khan

  git archive --format 

= tar --remote = origin HEAD - < file> | tar xf -

它类似于 Paul Brannan 回答

  git archive --format = tar --remote = origin HEAD:path / to / directory  -  filename | tar -O -xf  -  

As 评论 Doron Gold ,为了 git archive --remote = xxx 才能正常工作,您必须首先在git服务器上的存储库目录中运行此目录(git守护程序在其中运行) :

  git config daemon.uploadarch true 

请参阅 patthoyts '2009年的评论 JakubNarębski git:Retrieve



如果您拥有裸仓库的本地副本,您可以执行以下操作,如th是答案

  git  - 无页面 -  git-dir / path / to / bar / repo .git show branch:path / to / file> file 









  • 在工作树中.git回购



  • 但是,您可以执行 稀疏结帐 (如果您使用的是Git1.7 +),那么:
    $ b


    • 启用稀疏结帐选项( git config core.sparsecheckout true
    • .git / info / sparse-checkout中添加要查看的内容
    • code> file
    • 重新读取工作树以仅显示您需要的内容



    • 重新读取工作树:

        $ git read- tree -m -u HEAD 

      这样,你最终会得到一个工作树,包括你想要的东西(即使它只有一个文件)。


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

      解决方案

      As mentioned in the other answers:

      That will save a copy of 'filename' from the HEAD of the remote repository in the current directory.

      The :path/to/directory part is optional. If excluded, the fetched file will be saved to <current working dir>/path/to/directory/filename.

      Fizer Khan mentions below specifying the tar format explicitly:

      git archive --format=tar --remote=origin HEAD -- <file> | tar xf -
      

      It is similar to Paul Brannan's answer:

      git archive --format=tar --remote=origin HEAD:path/to/directory -- filename | tar -O -xf -
      

      As commented by Doron Gold, for git archive --remote=xxx to work, you MUST first run this inside the directory of your repository on your git server (where git daemon runs):

      git config daemon.uploadarch true
      

      See patthoyts' comment of 2009 Jakub Narębski's answer on "git: Retrieve a single file from a repository".

      You could alternatively do the following if you have a local copy of the bare repository as mentioned in this answer,

      git --no-pager --git-dir /path/to/bar/repo.git show branch:path/to/file >file
      


      • or you must clone first the repo, meaning you get the full history:

        • in the .git repo
        • in the working tree.
      • But then you can do a sparse checkout (if you are using Git1.7+),:

        • enable the sparse checkout option (git config core.sparsecheckout true)
        • adding what you want to see in the .git/info/sparse-checkout file
        • re-reading the working tree to only display what you need

      To re-read the working tree:

      $ git read-tree -m -u HEAD
      

      That way, you end up with a working tree including precisely what you want (even if it is only one file)

      这篇关于如何从git仓库只签出一个文件('稀疏结帐')?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆