ChatGPT解决这个技术问题 Extra ChatGPT

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

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 question and entered the commands below, but it didn't work.

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?


c
chrismillah

Short Answer

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

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

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

Full Example

$ cd /project/directory

$ git branch
* develop

check the remote name

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

Confirmed it's origin and

I am on branch develop and need a file from branch main

File i need is src/scss/main.scss

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

Thank you. So what do you mean by <revision>? the file name? And if my file is in the root directory would that mean i have to type: git checkout -m index.php index.php?
Thank you for the explanation.
Does it hast to be*origin*/master or can it be from any remote? Is the whole history pulled into my repo or does the file seem to appear magically?
@BernhardDöbler it can be any branch from remote :)
Why we are not doing git fetch --all instead of git fetch?
O
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.


Simple and efficient, thanks
Y
Y. Joy Ch. Singha

This can be the solution:

git fetch

git checkout origin/master -- FolderPathName/fileName

Thanks.


j
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_NAME to path/to and FILE_NAME to file.xz. So, you'll end up with something like

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 xO of course (It was me who need a pipe here, yeah).


T
Trutane

This scenario comes up when you -- or forces greater than you -- have mangled a file in 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 pull will not work: git notices the file's absence and assumes you probably want it gone from the repo (git diff will show all lines deleted from the missing file).

git pull not restoring locally missing files has always frustrated me about git, perhaps since I have been influenced by other version control systems (e.g. svn update which I believe will restore files that have been locally hidden).

git reset --hard HEAD is 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.

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


way much esplicative than others comments. Thank you
s
simhumileco

Try using:

git checkout branchName -- fileName

Ex:

git checkout master -- index.php

Attention: "git checkout master -- index.php" this dont do checkout from server, but from local git database from last pull.
V
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

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

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


The OP specifically indicated in the question that he is not using github.
k
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.

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