dana
dana

Reputation: 18145

git - get latest file after initially cloning

I am pretty new to Git, so apologies if I sound like a total noob.

I initially ran the following command:

git clone https://github.com/userx/projecty.git

I have been making changes on my workstation, none of which have been uploaded. There is one file that has been updated on github that I would like to sync to my workstation:

https://github.com/userx/projecty/blob/master/whatever/whatever.py

I have made some modifications to it locally, but they could/should be overwritten. I only want that one file refreshed on my workstation without touching anything else that I have done. Any ideas on how to do this would be appreciated.

Thanks!

Upvotes: 6

Views: 12479

Answers (3)

dana
dana

Reputation: 18145

This question was asked almost 10 years ago when I was first dabbling with Git.

Today, I would do the following:

  1. Using your web-browser of choice, find the file in the remote repo
  2. Click the "Raw" view
  3. Ctrl-S to save over local copy

This works in both GitHub and GitLab with slight nuances between systems.

Upvotes: -1

Raman
Raman

Reputation: 19605

Assuming you just want to overwrite the contents of that file in your working copy (so that it will show up as "modified" in the output of git status, then you can do it this way:

  1. Update the remote tracking branches of your local repository using git fetch. This does not modify any local branches, nor your working copy:

    git fetch --all

  2. Use git show to obtain a copy of the file from the remote branch, and write that to disk by redirecting standard output:

    git show origin/master:whatever/whatever.py > whatever/whatever.py

By specifying origin/master you are copying the file from the remote tracking branch, which will be the latest version of that file as of the git fetch. You can of course redirect the standard output to overwrite the existing file as shown above, or to write the output elsewhere.

The git show command accepts other formats as well: see "SPECIFYING REVISIONS" in man gitrevisions.

Note that, as @bluefeet commented on their answer, if you commit this file to your local branch, and your local branch is intended to be merged with the remote master later, then your locally copied whatever/whatever.py will be in conflict with the changes from the remote tracking branch. So generally, you would only do this as a temporary measure to test something, or if you have no intent of merging your local branch with the remote master later.

Upvotes: 3

bluefeet
bluefeet

Reputation: 391

I'd suggest this approach:

# Undo your changes to whatever.py.
git checkout whatever/whatever.py

# Pack up all your other changes, saving them to a special stash area.
git stash

# Update your repo, getting the latest, including the latest whatever.py.
git pull

# Now restore you changes from the stash.
git stash pop

That should do it, and puts you in a good working state.

Upvotes: 4

Related Questions