关于git merge:如何从Git中的服务器存储库中提取单个文件?

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

我在一个运行git的服务器上工作。我正在使用git进行部署(而不是github)。这是在我使用hook方法参与之前设置的,我提到了这个问题并输入了下面的命令,但它没有起作用。

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


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

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

然后:

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


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

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


这可能是解决方案:

1
2
3
git fetch

git checkout origin/master -- FolderPathName/fileName

谢谢。


当您(或强制大于您)在本地repo中损坏了一个文件,并且您只想从repo中恢复该文件的最新版本时,就会出现这种情况。只删除带有/bin/rm(而不是git rm)的文件或重命名/隐藏文件,然后发出git pull将不起作用:git会注意到文件不在,并假定您可能希望它从repo中消失(git diff将显示从丢失文件中删除的所有行)。

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

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

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


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

稀疏签出.bat

1
2
3
4
5
6
7
8
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.git
Updating 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


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

例如: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.

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


尝试使用:

1
git checkout branchName -- fileName

前任:

1
git checkout master -- index.php