Restore file from old commit in git

前端 未结 4 1809
悲哀的现实
悲哀的现实 2020-12-02 04:18

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?

4条回答
  •  不思量自难忘°
    2020-12-02 05:02

    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:

    1. 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 (e.g. b6b94f2c19c456336d60b9409fb1e373036d3d71) for the commit version you want.

    2. 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 is the simplest one I can remember.

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

提交回复
热议问题