Categories
dvcs file git undo version-control

Undo working copy modifications of one file in Git?

1906

After the last commit, I modified a bunch of files in my working copy, but I want to undo the changes to one of those files, as in reset it to the same state as the most recent commit.

However, I only want to undo the working copy changes of just that one file alone, nothing else with it.

How do I do that?

0

    2539

    You can use

    git checkout -- file
    

    You can do it without the -- (as suggested by nimrodm), but if the filename looks like a branch or tag (or other revision identifier), it may get confused, so using -- is best.

    You can also check out a particular version of a file:

    git checkout v1.2.3 -- file         # tag v1.2.3
    git checkout stable -- file         # stable branch
    git checkout origin/master -- file  # upstream master
    git checkout HEAD -- file           # the version from the most recent commit
    git checkout HEAD^ -- file          # the version before the most recent commit
    

    15

    • 43

      what’s the difference between HEAD and HEAD^?

      – hasen

      Mar 28, 2009 at 22:06

    • 72

      HEAD is the most recent commit on the current branch, and HEAD^ is the commit before that on the current branch. For the situation you describe, you could use git checkout HEAD — filename.

      – Paul

      Mar 28, 2009 at 22:21

    • 17

      In short “git checkout sha-reference — filename” where the sha-reference is a reference to the sha of a commit, in any form (branch, tag, parent, etc.)

      – lprsd

      Mar 2, 2010 at 15:46

    • 33

      NOTE: If the file is already staged, you need to reset it, first. git reset HEAD <filename> ; git checkout -- <filename>

      – Olie

      Jun 13, 2013 at 21:56

    • 14

      @gwho Yes, you can do HEAD^^ for 2 commits from the most recent, or HEAD^^^ for 3 commits back. You can also use HEAD~2, or HEAD~3, which gets more convenient if you want to go more commits back, while HEAD^2 means “the second parent of this commit”; because of merge commits, a commit can have more than one previous commit, so with HEAD^ a number selects which of those parents, while with HEAD~ a number always selects the first parent but that number of commits back. See git help rev-parse for more details.

      May 13, 2014 at 15:34

    183

    Just use

    git checkout filename
    

    This will replace filename with the latest version from the current branch.

    WARNING: your changes will be discarded — no backup is kept.

    4

    • 25

      @duckx it’s to disambiguate branch names from filenames. if you say git checkout x and x happens to be a branch name as well as a file name, I’m not sure what the default behavior is but I think git will assume you want to switch to branch x. When you use -- you’re saying that what follows is file name(s).

      – hasen

      Mar 4, 2015 at 16:15

    • 2

      ic thanks for clearing that up. everyone just assumes you know what — means when they show you examples. and its not something you can google easily too.

      Mar 4, 2015 at 18:05

    • 2

      Looks like the answer was edited to remove the -- from it. While still correct, as @hasen points out, if there is an ambiguity between filename and branch names you may end up with very undesired behavior here!

      Mar 14, 2015 at 18:14

    • 3

      I like it the way it is, without --, nice and easy. When you name branches using file names, there must be bad thinking somewhere…

      Apr 21, 2017 at 16:00

    147

    git checkout <commit> <filename>
    

    I used this today because I realized that my favicon had been overwritten a few commits ago when I upgrated to drupal 6.10, so I had to get it back. Here is what I did:

    git checkout 088ecd favicon.ico
    

    4

    • 1

      How do I get the commit (of a previously deleted file) except of scrolling throw tons of “git log –stat” output?

      – Alex

      Mar 1, 2012 at 9:40


    • 4

      IMO it’s kind of difficult via the commandline to scan through gits log and find the right file. It’s much easier with a GUI app, such as sourcetreeapp.com

      – neoneye

      Mar 2, 2012 at 6:46

    • 6

      git log --oneline <filename> will give you a more compact log, and only include changes to the specific file

      – rjmunro

      Feb 5, 2014 at 11:51

    • 1

      alternatively, you can use git reflog <filename>

      – ygesher

      Aug 5, 2015 at 7:08