【问题标题】:Git: How to pull a single file from a server repository in Git?Git:如何从 Git 的服务器存储库中提取单个文件?
【发布时间】:2015-04-07 03:57:20
【问题描述】:

我正在使用运行 Git 的服务器的站点上工作。我正在使用 Git 进行部署(不是 GitHub)。这是在我参与之前使用hook method 设置的,我参考了this question 并输入了以下命令,但它不起作用。

如何从服务器中提取单个文件?例如,如果我想更新我的本地文件 index.php? git pull index.php?

【问题讨论】:

标签: git git-merge git-pull git-fetch


【解决方案1】:

简答

可以这样做(在部署的存储库中):

git fetch --all
// git fetch will download all the recent changes, but it will not put it in your current checked out code (working area).

接着是:

git checkout origin/master -- path/to/file
// git checkout <local repo name (default is origin)>/<branch name> -- path/to/file will checkout the particular file from the downloaded changes (origin/master).

完整示例

$ cd /project/directory

$ git branch
* develop

检查远程名称

$ git remote -v
origin git@github.com:abc/123.git

确认是origin

我在分支 develop 上,需要来自分支 main 的文件

我需要的文件是src/scss/main.scss

git fetch --all
git checkout origin/main -- src/scss/main.scss

【讨论】:

  • 谢谢。那么&lt;revision&gt; 是什么意思?文件名?如果我的文件在根目录中,那是否意味着我必须输入:git checkout -m index.php index.php?
  • 感谢您的解释。
  • 必须是*origin*/master 还是可以来自任何远程?是整个历史记录都被拉进了我的仓库还是文件看起来很神奇?
  • @BernhardDöbler 它可以是远程的任何分支:)
  • 为什么我们不使用git fetch --all 而不是git fetch
【解决方案2】:
git fetch --all
git checkout origin/master -- <your_file_path>
git add <your_file_path>
git commit -m "<your_file_name> updated"

这是假设您从 origin/master 拉取文件。

【讨论】:

  • 简单高效,谢谢
【解决方案3】:

这可能是解决方案:

git fetch

git checkout origin/master -- FolderPathName/fileName

谢谢。

【讨论】:

    【解决方案4】:

    我正在寻找稍微不同的任务,但这看起来像你想要的:

    git archive --remote=$REPO_URL HEAD:$DIR_NAME -- $FILE_NAME |
    tar xO > /where/you/want/to/have.it
    

    我的意思是,如果你想获取path/to/file.xz,你将设置DIR_NAMEpath/toFILE_NAMEfile.xz。 所以,你最终会得到类似的东西

    git archive --remote=$REPO_URL HEAD:path/to -- file.xz |
    tar xO > /where/you/want/to/have.it
    

    当然,除了tar xO 之外,没有人会阻止您进行任何其他形式的拆包(是我需要管道,是的)。

    【讨论】:

      【解决方案5】:

      当您(或比您更强大)损坏了本地存储库中的文件并且您只想从回购。只需使用 /bin/rm (不是 git rm)删除文件或重命名/隐藏它然后发出 git pull 将不起作用:git 注意到文件的缺失并假设您可能希望它从 repo 中删除(git diff 将显示从丢失的文件中删除的所有行)。

      git pull 不恢复本地丢失的文件一直让我对 git 感到沮丧,也许是因为我受到了其他版本控制系统的影响(例如 svn update,我相信 恢复已本地隐藏)。

      git reset --hard HEAD 是恢复感兴趣文件的另一种方法,因为它会丢弃您拥有的任何未提交的更改。但是,正如 here 所述,如果您有任何其他您关心的未提交更改,那么 git reset 是一个潜在危险的命令。

      @chrismillah 上面提到的git fetch ... git checkout 策略是恢复相关文件的一种很好的手术方式。

      【讨论】:

      • 比其他 cmets 的方式要复杂得多。谢谢
      【解决方案6】:

      尝试使用:

      git checkout branchName -- fileName
      

      例如:

      git checkout master -- index.php
      

      【讨论】:

      • 注意:“git checkout master -- index.php”这不是从服务器结帐,而是从上次拉取的本地git数据库中结帐。
      【解决方案7】:
      https://raw.githubusercontent.com/[USER-NAME]/[REPOSITORY-NAME]/[BRANCH-NAME]/[FILE-PATH]
      

      例如。 https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

      通过这种方式,您将获得单个文件的内容作为一行 文本。您可以使用 wget 下载该文本。

      例如。 https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

      【讨论】:

      • OP在问题中特别指出他没有使用github。
      【解决方案8】:

      无论是否在 GitHub 上,此 Windows 批处理都有效。我使用它是因为它显示了一些明显的警告。 您会注意到操作很慢并且需要遍历数百兆字节的数据,因此如果您的要求基于可用带宽/R-W 内存,请不要使用此方法。

      sparse_checkout.bat

      pushd "%~dp0"
      if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs
      pushd .\ms-server-essentials-docs
      git init
      git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git
      git config core.sparseCheckout true
      (echo EssentialsDocs)>>.git\info\sparse-checkout
      git pull origin master
      

      =>

      C:\Users\用户名\Desktop>sparse_checkout.bat

      C:\Users\用户名\Desktop>pushd "C:\Users\用户名\Desktop\"

      C:\Users\用户名\Desktop>如果不存在。\ms-server-essentials-docs mkdir .\ms-server-essentials-docs

      C:\Users\用户名\Desktop>pushd .\ms-server-essentials-docs

      C:\Users\用户名\Desktop\ms-server-essentials-docs>git init 在 C:/Users/user 中初始化空的 Git 存储库 名称/桌面/ms-server-essentials-docs/.git/

      C:\Users\用户名\Desktop\ms-server-essentials-docs>git 远程添加 起源-fhttps://github.com/MicrosoftDocs/windowsserverdocs.git 更新源远程:枚举对象:97,完成。偏僻的: 计数对象:100% (97/97),完成。远程:压缩对象: 100% (44/44),完成。远程:总计 145517(增量 63),重复使用 76(增量 53)、pack-reused 145420 接收对象:100%(145517/145517), 751.33 兆字节 | 32.06 MiB/s,完成。解决增量:100% (102110/102110),完成。从 https://github.com/MicrosoftDocs/windowsserverdocs * [新分支]
      1106-conflict -> origin/1106-conflict * [新分支]
      FromPrivateRepo -> origin/FromPrivateRepo * [新分支]
      PR183 -> origin/PR183 * [新分支]
      冲突修复 -> 起源/冲突修复 * [新分支]
      eross-msft-patch-1 -> origin/eross-msft-patch-1 * [新分支]
      master -> origin/master * [新分支] patch-1
      -> origin/patch-1 * [新分支] repo_sync_working_branch -> origin/repo_sync_working_branch * [新分支]
      shortpatti-patch-1 -> origin/shortpatti-patch-1 * [新分支]
      shortpatti-patch-2 -> origin/shortpatti-patch-2 * [新分支]
      shortpatti-patch-3 -> origin/shortpatti-patch-3 * [新分支]
      shortpatti-patch-4 -> origin/shortpatti-patch-4 * [新分支]
      shortpatti-patch-5 -> origin/shortpatti-patch-5 * [新分支]
      shortpatti-patch-6 -> origin/shortpatti-patch-6 * [新分支]
      shortpatti-patch-7 -> origin/shortpatti-patch-7 * [新分支]
      shortpatti-patch-8 -> 起源/shortpatti-patch-8

      C:\Users\用户名\Desktop\ms-server-essentials-docs>git config core.sparseCheckout true

      C:\Users\用户名\Desktop\ms-server-essentials-docs>(echo EssentialsDocs ) 1>>.git\info\sparse-checkout

      C:\Users\用户名\Desktop\ms-server-essentials-docs>git pull origin 大师
      来自https://github.com/MicrosoftDocs/windowsserverdocs
      * 分支主 -> FETCH_HEAD

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-01-14
        • 1970-01-01
        • 2011-04-08
        • 2016-12-01
        • 2014-10-18
        • 1970-01-01
        • 1970-01-01
        • 2013-05-27
        相关资源
        最近更新 更多