20

Say you just want to get rid of the changes you've made to one file, and get back to whatever is in the repository. I used to do this in svn:

rm a-file.txt
svn update a-file.txt

What is the equivalent in Git? I know how to fetch/pull evrything from the repository, but how about one single file?

Martin
  • 37,119
  • 15
  • 73
  • 82
Paul Casal
  • 203
  • 1
  • 2
  • 4

1 Answers1

33

To undo your (uncommitted) changes:

git checkout a-file.txt

If you have committed changes and want to undo them back to a certain previous commit:

git checkout [some-older-commit-ref] a-file.txt

Btw, with Subversion you should have done:

svn revert a-file.txt
Martin
  • 37,119
  • 15
  • 73
  • 82
  • 1
    you can also do "git reset --hard [commit/branch]". To find out the commits "git log --oneline", --oneline so it doesn't write you all the unneeded information. – Lilian A. Moraru Mar 28 '12 at 12:54
  • +1 and to clarify on the "certain previous commit": It refers to the `SHA1 ID` that can easily be found via `gitk`. If I only need to "checkout" that file to a temporary location (i.e. not reverting), then I would use the `show` subcommand: `git show 82e54378856215ef96c5db1ff1160a741b5dcd70:MyProj/proguard/mapping.txt > myproj_mapping.txt` – ef2011 Oct 14 '12 at 23:45