ChatGPT解决这个技术问题 Extra ChatGPT

Git: How to update/checkout a single file from remote origin master?

The scenario:

I make some changes in a single file locally and run git add, git commit and git push The file is pushed to the remote origin master repository I have another local repository that is deployed via Capistrano with the "remote_cache" method from that remote repository Now I don't want to deploy the whole application but just update/checkout that single file.

Is this somehow possible with git? I wasn't able to find anything that would work nor was I able to figure it out. With SVN I just did svn up file and voila.

You might want to consider changing the accepted answer to the one that actually answers your question. ;)
After more than 6 years, I believe we can safely assume that this will not happen @steps...
With Git 2.23 (August 2019), it is git restore -s origin/master -- path/to/file. See my answer below.

J
JMM

It is possible to do (in the deployed repository)

git fetch
git checkout origin/master -- path/to/file

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

The checkout will update the working tree with the particular file from the downloaded changes (origin/master).

At least this works for me for those little small typo fixes, where it feels weird to create a branch etc just to change one word in a file.


Super handy, this worked great. I needed to get a composer.json file and run an update before I updated the rest of the site in production. If I had manually put the composer.json/lock files in place, when I did a pull, it would conflict saying the files already existed. By doing it this way, git recognized the files without a complaint.
This is the answer i was looking for.
@Mymozaaa The double dash designates that what follows is a file name. It's to prevent git from interpreting your file name as a branch in the unfortunate case you've got two with the same name.
The problem is that you're still fetching, and if it's a large repo, that will be the expensive operation. I fear that the only alternative would be to install gitweb on the remote and then access it to retrieve the file or such.
If you want to checkout all files in a folder, just do git checkout origin/master -- path/to/folder/* (note the * in the end).
v
vsync

Following code worked for me:

git fetch
git checkout <branch from which file needs to be fetched> <filepath> 

git fetch <remote>; git checkout <remote>/<branch> -- <file> for non-origin remotes
V
VonC

With Git 2.23 (August 2019) and the new (still experimental) command git restore, seen in "How to reset all files from working directory but not from staging area?", that would be:

git fetch
git restore -s origin/master -- path/to/file

The idea is: git restore only deals with files, not files and branches as git checkout does.
See "Confused by git checkout": that is where git switch comes in)

codersam adds in the comments:

in my case I wanted to get the data from my upstream (from which I forked). So just changed to: git restore -s upstream/master -- path/to/file


What a relief that this command finally exists... What were git-cluefull people doing before? I was restoring the whole thing and copying the individual files I needed, but it was painful.
This worked for me, but in my case I wanted to get the data from my upstream (from which I forked). So just changed to git restore -s upstream/master -- path/to/file
@coderSam Than you for this feedback. I have included your comment in the answer for more visibility.
restore is better from my testing, because it puts files in the working area instead of the staging area as checkout does.
D
Dmitry R
git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip

This is a good solution for repos cloned over ssh, but seems this is not supported over https: git archive --remote=https://github.com/git/git.git master:git/contrib/completion git-completion.bash | tar -x Gives me error message: fatal: Operation not supported by protocol.
was nice combined with tar:s --to-stdout, git archive --remote="gitolite3@<host>:<repo>" <tag> <file> | tar xf - --to-stdout
M
Mathivanan

Simply It works for me

git checkout origin/develop file_name.php

j
jag

What you can do is:

Update your local git repo: git fetch Build a local branch and checkout on it: git branch pouet && git checkout pouet Apply the commit you want on this branch: git cherry-pick abcdefabcdef (abcdefabcdef is the sha1 of the commit you want to apply)


As an aside, your second step can also be done in one command as git checkout -b pouet.
'pouet' is the best branch name for this example.
j
jahrichie

Or git stash (if you have changes) on the branch you're on, checkout master, pull for the latest changes, grab that file to your desktop (or the entire app). Checkout the branch you were on. Git stash apply back to the state you were at, then fix the changes manually or drag it replacing the file.

This way is not sooooo cool but it def works if you guys can't figure anything else out.


B
BillowJiang

我的英文很烂,我给你列出步骤: (My English is terrible. I'll lay out the steps for you):

git checkout master
git pull --rebase
git checkout dev
git checkout master ./app/file.py

Machine translation: If you are developing in the dev branch, merge the master branch after replacing the file and push it. For example, replace app/file.py.


My English is really very bad, I hope you can see.... It's all machine translated.
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
V
Viswadeep Sarangi

I think I have found an easy hack out.

Delete the file that you have on the local repository (the file that you want updated from the latest commit in the remote server)

And then do a git pull

Because the file is deleted, there will be no conflict


That removes all eventual changes made to that file locally, and also pulls all the other files, which is specifically what the OP doesn't want to do.
Instead of updating the remote branch, removing local is very bad idea.