如何从Git中的服务器存储库中提取单个文件?

问题描述 投票:80回答:7

我正在使用运行Git的服务器的网站上工作。我正在使用Git进行部署(而不是GitHub)。这是在我参与使用hook method之前建立的,我提到了this question并输入了下面的命令,但它没有用。

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

git git-merge git-pull git-fetch
7个回答
137
投票

可以(在已部署的存储库中):

git fetch
// 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).

16
投票
git fetch --all
git checkout origin/master -- <your_file_path>
git add <your_file_path>
git commit -m "<your_file_name> updated"

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


7
投票

这可以是解决方案:

git fetch

git checkout origin/master -- FolderPathName/fileName

谢谢。


2
投票

当你 - 或者比你更强大的力量 - 在你的本地仓库中破坏了一个文件并且你只想从repo恢复它的最新版本的新副本时,会出现这种情况。简单地用/ 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策略是一种很好的手术方式来恢复有问题的文件。


0
投票

尝试使用:

git checkout branchName -- fileName

例如:

git checkout master -- index.php

0
投票
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


0
投票

无论是否在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 \ user name \ 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 name / Desktop / ms-server-essentials-docs / .git /中初始化的空Git存储库

C:\ Users \用户名\ Desktop \ ms-server-essentials-docs> git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git更新源远程:枚举对象:97,完成。 remote:计数对象:100%(97/97),完成。 remote:压缩对象:100%(44/44),完成。远程:总计145517(delta 63),重用76(delta 53),pack-reused 145420接收对象:100%(145517/145517),751.33 MiB | 32.06 MiB / s,完成了。解决增量:100%(102110/102110),完成。来自https://github.com/MicrosoftDocs/windowsserverdocs * [新分店] 1106-冲突 - >原产地/ 1106冲突* [新分支] FromPrivateRepo - > origin / FromPrivateRepo * [新分支] PR183 - > origin / PR183 * [新分支] conflictfix - > origin / conflictfix * [new branch] eross-msft-patch-1 - > origin / eross-msft-patch-1 * [new branch] master - > origin / master * [new branch] patch-1 - > origin / patch-1 * [new branch] repo_sync_working_branch - > origin / repo_sync_working_branch * [new branch] shortpatti-patch-1 - > origin / shortpatti-patch-1 * [new branch] shortpatti-patch-2 - > origin / shortpatti-patch-2 * [new branch] shortpatti-patch-3 - > origin / shortpatti-patch-3 * [新分支] shortpatti-patch-4 - > origin / shortpatti-patch-4 * [新分支] shortpatti-patch-5 - > origin / shortpatti-patch-5 * [new branch] shortpatti-patch-6 - > origin / shortpatti-patch-6 * [新分支] shortpatti-patch-7 - > origin / shortpatti-patch-7 * [new branch] shortpatti-patch-8 - > origin / 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 master 来自https://github.com/MicrosoftDocs/windowsserverdocs *分支主控 - > FETCH_HEAD

© www.soinside.com 2019 - 2024. All rights reserved.