391

I'm trying to revert my changes in a single file in my feature branch and I want this file to be the same as in master.

I tried:

git checkout -- filename
git checkout filename 
git checkout HEAD -- filename

It seems that none of these made any changes to my feature branch. Any suggestions?

Anton Belev
  • 9,782
  • 19
  • 64
  • 109
  • As of 2022, there is a cleaner way to do that: `git restore --source HEAD filename` – Adam Jun 01 '22 at 23:55
  • For me I wasn't able to use a git command, it would always say file not found. Instead I went to the main branch in my web browser, downloaded the file and replaced the file on my machine. It works as a UI solution in case git isn't working for whatever reason. – SendETHToThisAddress Jun 03 '22 at 21:54

2 Answers2

765

If you want to revert the file to its state in master:

git checkout origin/master [filename]

Milo P
  • 1,285
  • 2
  • 33
  • 37
Dennan Hogue
  • 7,684
  • 1
  • 10
  • 6
  • 1
    Thanks Dennan, you are right here. I just found this answer https://stackoverflow.com/questions/13847425/overwrite-single-file-in-my-current-branch-with-the-same-file-in-the-master-bran. I think this is what I was after. – Anton Belev Jun 22 '16 at 15:49
  • I get this repeating error: "Unlink of file '' failed. Should I try again? (y/n)", then "error: unable to unlink old '': Invalid argument". – Shimmy Weitzhandler Oct 06 '19 at 15:29
  • 2
    If you're on windows, keep in mind that git uses CASE SENSITIVE paths. Always screws me up – Kellen Stuart Mar 26 '20 at 21:01
79

you are almost there; you just need to give the reference to master; since you want to get the file from the master branch:

git checkout master -- filename

Note that the differences will be cached; so if you want to see the differences you obtained; use

git diff --cached
Chris Maes
  • 30,644
  • 6
  • 98
  • 124