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

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

我正在使用服务器 运行 Git 开发一个站点。我正在使用 Git 进行部署(而不是 GitHub)。这是在我参与之前使用 hook method, and I referred to this question 设置的,并输入了下面的命令,但没有用。

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

简答

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

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
git fetch --all
git checkout origin/master -- <your_file_path>
git add <your_file_path>
git commit -m "<your_file_name> updated"

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

尝试使用:

git checkout branchName -- fileName

例如:

git checkout master -- index.php

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

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

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

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

这可能是解决方案:

git fetch

git checkout origin/master -- FolderPathName/fileName

谢谢。

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

这个 windows 批处理无论是否在 GitHub 上都有效。我使用它是因为它显示了一些明显的警告。 您会注意到该操作很慢并且要遍历数百兆字节的数据,因此如果您的要求基于可用的bandwidth/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\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

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

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

我的意思是,如果你想获取path/to/file.xz,你需要将DIR_NAME设置为path/to,将FILE_NAME设置为file.xz。 所以,你最终会得到类似

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

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