Restore file from old commit in git

I have an old commit that I did a few weeks ago. I want to restore only a single file from that commit. What do I do?


git checkout 'master@{7 days ago}' -- path/to/file.txt

This will not alter HEAD, it will just overwrite the local file path/to/file.txt

See man git-rev-parse for possible revision specifications there (of course a simple hash (like dd9bacb ) will do nicely)

Don't forget to commit the change (after a review...)


  • Check out the file from your old commit via git checkout [Revision_Key] -- path/to/file .
  • Add, commit, push as appropriate.

  • I needed to restore a recent file committed into git. So just to reiterate and give another perspective, you need to do this by running the following two steps:

  • git log -3
    This shows the three most recent commits. Read the comments and the author's name so you narrow down what exact version you want. Write down that long commit id (ie b6b94f2c19c456336d60b9409fb1e373036d3d71) for the commit version you want.

  • git checkout b6b94f2c19c456336d60b9409fb1e373036d3d71 -- myfile.java
    Pass the commit id AND the file name you want to restore. Make sure you have a space before and after the double hyphen.

  • There are many other ways to do it. But this one is the simpler one I can remember. Hope that helps.

    NOTE: If you are inside your project path/folder then is not necessary to type the full file's path in the checkout command.

    链接地址: http://www.djcxy.com/p/8984.html

    上一篇: 在提交中将更改还原为文件

    下一篇: 从git中的旧提交恢复文件