548

I'm in my local computer's master branch of a cloned master-branch of a repo from a remote server.

I updated a file, and I want to revert back to the original version from the remote master branch.

How can I do this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
mrblah
  • 99,669
  • 140
  • 310
  • 420

7 Answers7

1127

Assuming you did not commit the file, or add it to the index, then:

git checkout -- filename

Assuming you added it to the index, but did not commit it, then:

git reset HEAD filename
git checkout -- filename

Assuming you did commit it, then:

git checkout origin/master filename

Assuming you want to blow away all commits from your branch (VERY DESTRUCTIVE):

git reset --hard origin/master
GP Singh
  • 706
  • 5
  • 12
gahooa
  • 131,293
  • 12
  • 98
  • 101
  • 50
    Your third option is very different from your first two options in that it touches all files and not just the one file. You may want to point this out more explicitly. Also why not recommend `git checkout HEAD filename` and `git checkout origin/master filename` for options one and two, it would be more consistent? – CB Bailey Nov 30 '09 at 06:07
  • 7
    @CharlesBailey: I added the `git checkout origin/master filename` option to gahooa's answer. – Frank Feb 12 '13 at 22:47
  • The last option doesn't specify the filename - how does that work? – cilphex May 31 '13 at 03:23
  • 2
    @cilphex it blows away all of the commits in your current branch – WattsInABox Aug 23 '13 at 19:24
  • 1
    I accidently rebased branchA off of branchB and needed to reset to master local branch. `git reset --hard master` alone was my solution since I didn't want to reach out to origin. Thanks. – taco Mar 13 '15 at 07:13
  • 3
    @gahooa It should probably be git checkout -- filename, what if the file is called "master", then you would get a behaviour that was not intended. – user2602152 Mar 19 '15 at 12:44
  • 1
    I found I have to `rm filename` first. `git checkout filename` won't overwrite the changes in your current directory. – Meekohi Dec 29 '15 at 19:51
  • In "git checkout origin/master filename" what is syntax for multiple paths? Is it semicolon separated or blank space etc? In other words, I am looking for something like: "git checkout origin/master path1;path2;path3" – qqqqq Oct 27 '17 at 22:56
  • What if that does not work because of unmerged files? – Soerendip May 17 '18 at 23:59
  • Interesting. I thought checkout was only for switching branches. What does the -- mean? – sdfsdf Jun 18 '18 at 16:20
  • @MichaelL. - `git checkout` is used for switching branches when a branch is given, or for restoring content to the state of the index, when files are given. The manual gives all the juicy details. – gahooa Jun 28 '18 at 21:16
  • Very clear answer. To the point and precise. I wish my textbooks were like this. – Lokesh Oct 22 '18 at 06:46
  • Note that the "third option" mentioned by CBBailey is the `reset --hard` that has been edited to now be the fourth option. – Teepeemm Jul 22 '23 at 15:32
70

I've faced same problem and came across to this thread but my problem was with upstream. Below git command worked for me.

Syntax

git checkout {remoteName}/{branch} -- {../path/file.js}

Example

git checkout upstream/develop -- public/js/index.js
Venkat.R
  • 7,420
  • 5
  • 42
  • 63
44

Can also be done using git restore

git restore --source origin/master filename

Lorentz Lasson
  • 885
  • 8
  • 26
4

For the sake of adding an alternative (not necessarily a better one, of course), if you have already committed the file before, but now you need to undo the change, here's what you can do:

git diff HEAD..master -- path/to/file.ext | git apply -

This generates a diff to restore the file to the version in the master branch, and then applies it. The minus after git apply tells git to read the patch from standard input.

You can then commit the file as usual.

Here is the same command expressed as a shell function:

# Git Reset File
function grf() {
  git diff HEAD..master -- $1 | git apply -
}

# for example: grf ./someChangedFile.txt
random-forest-cat
  • 33,652
  • 11
  • 120
  • 99
BitParser
  • 3,748
  • 26
  • 42
4

you need to find the latest commit id and the directory of the file you want to revert.

then using the following commands

git checkout [commit ID] -- path/to/file
git commit -m 'commit message'

will help you to revert the file you want to latest version of that file on remote computer.

2

If you didn't commit it to the master branch yet, its easy:

  • get off the master branch (like git checkout -b oops/fluke/dang)
  • commit your changes there (like git add -u; git commit;)
  • go back the master branch (like git checkout master)

Your changes will be saved in branch oops/fluke/dang; master will be as it was.

commonpike
  • 10,499
  • 4
  • 65
  • 58
1

You can restore your files modified or deleted with:

git restore yourFileName

Or for every file:

git restore .

Patapoom
  • 794
  • 1
  • 7
  • 16