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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28375418/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:53:54  来源:igfitidea点击:

How to pull a single file from a server repository in Git?

gitgit-mergegit-pullgit-fetch

提问by vsvs

I am working on a site with a server running Git. I am using Git for deployment (not GitHub). This was set up prior to my involvement using a hook method, and I referred to this questionand entered the commands below, but it didn't work.

我正在一个服务器运行 Git 的站点上工作。我使用 Git 进行部署(不是 GitHub)。这是在我使用钩子方法参与之前设置的,我参考了这个问题并输入了下面的命令,但它不起作用。

How do I pull a single file from the server? For instance, if I wanted to update my local file index.php? git pull index.php?

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

回答by chrismillah

It is possible to do (in the deployed repository):

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

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

Followed by:

其次是:

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).

回答by OYORF

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

This is assuming you are pulling the file from origin/master.

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

回答by Y. Joy Ch. Singha

This can be the solution:

这可以是解决方案:

git fetch

git checkout origin/master -- FolderPathName/fileName

Thanks.

谢谢。

回答by Trutane

This scenario comes up when you -- or forces greater than you -- have mangled a filein your local repo and you just want to restore a fresh copy of the latest version of it from the repo. Simply deleting the file with /bin/rm (not git rm) or renaming/hiding it and then issuing a git pullwill not work: git notices the file's absence and assumes you probably want it gone from the repo (git diffwill show all lines deleted from the missing file).

当您 - 或者比您更强大的力量 -在您的本地存储库中损坏了一个文件并且您只想从存储库中恢复它的最新版本的新副本时,就会出现这种情况。简单地用 /bin/rm (不是 git rm)删除文件或重命名/隐藏它然后发出一个git pull将不起作用:git 注意到文件不存在并假设您可能希望它从 repo 中消失(git diff将显示从文件丢失)。

git pullnot restoring locally missing files has always frustrated me about git, perhaps since I have been influenced by other version control systems (e.g. svn updatewhich I believe willrestore files that have been locally hidden).

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

git reset --hard HEADis an alternative way to restore the file of interest as it throws away any uncommitted changes you have. However, as noted here, git reset is is a potentially dangerous command if you have any other uncommitted changes that you care about.

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

The git fetch ... git checkoutstrategy noted above by @chrismillah is a nice surgical way to restore the file in question.

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

回答by jno

I was looking for slightly different task, but this looks like what you want:

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

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

I mean, if you want to fetch path/to/file.xz, you will set DIR_NAMEto path/toand FILE_NAMEto file.xz. So, you'll end up with something like

我的意思是,如果你想获取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

And nobody keeps you from any other form of unpacking instead of tar xOof course (It was me who need a pipe here, yeah).

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

回答by Vipin Bihari Tiwari

https://raw.githubusercontent.com/[USER-NAME]/[REPOSITORY-NAME]/[BRANCH-NAME]/[FILE-PATH]

Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

前任。https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

Through this you would get the contents of an individual file as a row text. You can download that text with wget.

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

Ex. https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

前任。https://raw.githubusercontent.com/vipinbihari/apana-result/master/index.php

回答by Dodda Venkata

Try using:

尝试使用:

git checkout branchName -- fileName

Ex:

前任:

git checkout master -- index.php

回答by kayleeFrye_onDeck

This windows batch works regardless of whether or not it's on GitHub. I'm using it because it shows some stark caveats. You'll notice that the operation is slow and traversing hundreds of megabytes of data, so don't use this method if your requirements are based on available bandwidth/R-W memory.

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

sparse_checkout.bat

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\user name\Desktop>sparse_checkout.bat

C:\Users\user name\Desktop>pushd "C:\Users\user name\Desktop\"

C:\Users\user name\Desktop>if not exist .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs

C:\Users\user name\Desktop>pushd .\ms-server-essentials-docs

C:\Users\user name\Desktop\ms-server-essentials-docs>git init Initialized empty Git repository in C:/Users/user name/Desktop/ms-server-essentials-docs/.git/

C:\Users\user name\Desktop\ms-server-essentials-docs>git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.gitUpdating origin remote: Enumerating objects: 97, done. remote: Counting objects: 100% (97/97), done. remote: Compressing objects: 100% (44/44), done. remote: Total 145517 (delta 63), reused 76 (delta 53), pack-reused 145420 Receiving objects: 100% (145517/145517), 751.33 MiB | 32.06 MiB/s, done. Resolving deltas: 100% (102110/102110), done. From https://github.com/MicrosoftDocs/windowsserverdocs* [new branch]
1106-conflict -> origin/1106-conflict * [new branch]
FromPrivateRepo -> origin/FromPrivateRepo * [new branch]
PR183 -> origin/PR183 * [new branch]
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 * [new branch]
shortpatti-patch-4 -> origin/shortpatti-patch-4 * [new branch]
shortpatti-patch-5 -> origin/shortpatti-patch-5 * [new branch]
shortpatti-patch-6 -> origin/shortpatti-patch-6 * [new branch]
shortpatti-patch-7 -> origin/shortpatti-patch-7 * [new branch]
shortpatti-patch-8 -> origin/shortpatti-patch-8

C:\Users\user name\Desktop\ms-server-essentials-docs>git config core.sparseCheckout true

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

C:\Users\user name\Desktop\ms-server-essentials-docs>git pull origin master
From https://github.com/MicrosoftDocs/windowsserverdocs
* branch master -> FETCH_HEAD

C:\用户\用户名\桌面>sparse_checkout.bat

C:\用户\用户名\桌面>pushd "C:\用户\用户名\桌面\"

C:\用户\用户名\桌面>如果不存在 .\ms-server-essentials-docs mkdir .\ms-server-essentials-docs

C:\用户\用户名\桌面>pushd .\ms-server-essentials-docs

C:\Users\user name\Desktop\ms-server-essentials-docs>git init 在 C:/Users/user name/Desktop/ms-server-essentials-docs/.git/ 初始化空 Git 仓库

C:\Users\user name\Desktop\ms-server-essentials-docs>git remote add origin -f https://github.com/MicrosoftDocs/windowsserverdocs.git更新源远程:枚举对象:97,完成。远程:计数对象:100% (97/97),完成。远程:压缩对象:100% (44/44),完成。远程:总计 145517 (delta 63),重用 76 (delta 53),包重用 145420 接收对象:100% (145517/145517),751.33 MiB | 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 -> 起源/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 -> origin/shortpatti-patch-8

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

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

C:\Users\user name\Desktop\ms-server-essentials-docs>git pull origin master
from https://github.com/MicrosoftDocs/windowsserverdocs
* branch master -> FETCH_HEAD