Categories
git git-checkout version-control

How do I reset or revert a file to a specific revision?

5377

How do I revert a modified file to its previous revision at a specific commit hash (which I determined via git log and git diff)?

3

  • 18

    After revert, don’t forget --cached when checking git diff. link

    Dec 2, 2015 at 19:20


  • 7

    I found your question when I googled mine. But after I read the solution, I checked my log and found out, that I made thouse changes as a standalone commit, so I made git revert for that commit, and everything else stayed as I wanted it. Not a solution, just another way to do it sometimes.

    – sudo97

    Aug 8, 2017 at 14:08

  • I use this manual solution: $ git revert <commit> then unstash wanted changes then upload this wanted changes into a new commit.

    – Intenzion

    Jul 14, 2021 at 18:19

7285

Assuming the hash of the commit you want is c5f567:

git checkout c5f567 -- file1/to/restore file2/to/restore

The git checkout man page gives more information.

If you want to revert to the commit before c5f567, append ~1 (where 1 is the number of commits you want to go back, it can be anything):

git checkout c5f567~1 -- file1/to/restore file2/to/restore

As a side note, I’ve always been uncomfortable with this command because it’s used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).


There is also a new git restore command that is specifically designed for restoring working copy files that have been modified. If your git is new enough you can use this command, but the documentation comes with a warning:

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

25

  • 14

    @shadowhand: Is there a way to reverse that, so it’s the version right after?

    Apr 29, 2014 at 12:57

  • 19

    @aliteralmind: No, unfortunately the Git history shortcut notation only goes backwards in history.

    Apr 29, 2014 at 18:02

  • 66

    If you’re going to use a branch name for abcde (e.g. develop) you’ll want git checkout develop -- file/to/restore (note the double dash)

    Oct 7, 2014 at 15:14


  • 12

    @aliteralmind: Actually, yes, there’s a way to do it: “git log –reverse -1 –ancestry-path yourgitrev..master” and then use the appropriate options to just get the git rev. –ancestry-path will “draw a line” between two commits and -1 will show you just one version, and –reverse will ensure the first entry emitted is the oldest one.

    Nov 19, 2014 at 19:29


  • 14

    Personally I find HEAD^ easier to type than HEAD~1 🙂

    – juzzlin

    Jul 1, 2016 at 11:38

715

You can quickly review the changes made to a file using the diff command:

git diff <commit hash> <filename>

Then to revert a specific file to that commit use the reset command:

git reset <commit hash> <filename>

You may need to use the --hard option if you have local modifications.

A good workflow for managaging waypoints is to use tags to cleanly mark points in your timeline. I can’t quite understand your last sentence but what you may want is diverge a branch from a previous point in time. To do this, use the handy checkout command:

git checkout <commit hash>
git checkout -b <new branch name>

You can then rebase that against your mainline when you are ready to merge those changes:

git checkout <my branch>
git rebase master
git checkout master
git merge <my branch>

12

  • 8

    ‘git checkout <commit hash>’ command has given me back my older version of the project exactly this for which I was searching Thanks Chris.

    Jan 27, 2013 at 9:26


  • 69

    To revert the file git checkout <commit hash> <filename> worked better for me than git reset

    Mar 7, 2013 at 16:53


  • 4

    I wanted an early version of a single file because I had overwritten 150 lines with a badly chosen copy/paste. git checkout <commit hash> <filename> worked for me. This should not be the accepted answer, IMHO. git reset did not.

    Feb 27, 2014 at 20:58

  • 31

    cannot use git reset to reset single file, you will get an error fatal: Cannot do hard reset with paths

    – slier

    Dec 23, 2014 at 17:11


  • 18

    What slier said: you cannot git reset --hard <commit hash> <filename>. This will error with fatal: Cannot do hard reset with paths. What Motti Strom said: use git checkout <commit hash> <filename>

    Feb 6, 2015 at 5:36

416

You can use any reference to a git commit, including the SHA-1 if that’s most convenient. The point is that the command looks like this:

git checkout [commit-ref] -- [filename]

8

  • 34

    What is the difference between this answer, which has --, and the accepted one which does not?

    – 2rs2ts

    Oct 9, 2014 at 0:20

  • 101

    In git, a ‘ — ‘ before the file list tells git that all the next arguments should be interpreted as filenames, not as branch-names or anything else. It’s a helpful disambiguator sometimes.

    – foxxtrot

    Oct 9, 2014 at 14:32

  • 63

    The ‘–‘ is not only a git convention, but something you find in various places in on the *nix commandline. rm -- -f (remove a file named -f) seems to be the canonical example. More detail here

    Feb 6, 2015 at 5:49

  • 7

    Just add to what @HawkeyeParker said, rm command uses getopt(3) to parse its arguments. getopt is the command to parse command arguments. gnu.org/software/libc/manual/html_node/Getopt.html

    – Devy

    Jul 14, 2015 at 18:11

  • 2

    @Honey Yes, that’s what I mean, and yeah, probably not common at all. I’ve seen that example in various places, maybe just to make it sortof memorable: rm -f is well-known to be scary/dangerous. But, the point is, in *nix a file name can start with a ‘-‘, and this will confuse various commandline interpreters which, when they see a ‘-‘, expect a command option to follow. It could be any file starting with ‘-‘; e.g., “-mySpecialFile”.

    Apr 5, 2017 at 20:35