foresth
foresth

Reputation: 6183

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

The scenario:

  1. I make some changes in a single file locally and run git add, git commit and git push
  2. The file is pushed to the remote origin master repository
  3. I have another local repository that is deployed via Capistrano with the "remote_cache" method from that remote repository
  4. 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.

Upvotes: 600

Views: 967001

Answers (9)

Feb CodeGeek
Feb CodeGeek

Reputation: 61

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

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.

Upvotes: 6

Mathivanan
Mathivanan

Reputation: 381

Simply It works for me

git checkout origin/develop file_name.php

Upvotes: 28

Shagun Pruthi
Shagun Pruthi

Reputation: 2051

Following code worked for me:

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

Upvotes: 82

VonC
VonC

Reputation: 1324268

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

Upvotes: 88

Viswadeep Sarangi
Viswadeep Sarangi

Reputation: 1

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

Upvotes: -17

qzio
qzio

Reputation: 13113

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.

Upvotes: 1291

Dmitry R
Dmitry R

Reputation: 3176

git archive --format=zip --remote=ssh://<user>@<host>/repos/<repo name> <tag or HEAD> <filename> > <output file name>.zip

Upvotes: 19

jahrichie
jahrichie

Reputation: 1235

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.

Upvotes: 2

jag
jag

Reputation: 908

What you can do is:

  1. Update your local git repo:

    git fetch

  2. Build a local branch and checkout on it:

    git branch pouet && git checkout pouet

  3. Apply the commit you want on this branch:

    git cherry-pick abcdefabcdef

    (abcdefabcdef is the sha1 of the commit you want to apply)

Upvotes: 9

Related Questions