0

let's say that i have file called getData as the following

class getData{
   public function retriveData() {
     echo 'data retrived';
   } 
   public function checkData() {
    echo 'data checked'
   }
}

then i edit it to be like this

class getData{                                                                                    
      public function retriveData() {
      echo 'data retrived';
     }                                          
}

then i commit the last change and after while i want to get checkData() back what is the best method to do that without damage any of my project files and make my file back to previous form

  • Do you know which commit had the old version? if so, `git checkout commitid` then copy it. – Michael Mano Nov 18 '21 at 22:45
  • Does this answer your question? [How to sparsely checkout only one single file from a git repository?](https://stackoverflow.com/questions/2466735/how-to-sparsely-checkout-only-one-single-file-from-a-git-repository) – Nadar Nov 19 '21 at 05:24
  • i used git revert to get missing lines thx guys – Abdalrhman Hussin Nov 22 '21 at 15:06

1 Answers1

1

You can "checkout" a file from a previous commit by runnning:

git checkout abc123 -- path/to/file/to/restore

Where abc123 is the commit hash where the file was in the state you wanted it to be in.

You can also swap the commit ID with HEAD~1 where the 1 represents the number of commits to go back from.

Update; As per @Roman Valeri, the newer, equivalent syntax would be:

git restore —source=abc123 — path/to/file
James
  • 15,754
  • 12
  • 73
  • 91