Home > Blockchain >  How to recover lost git commits from a few weeks ago?
How to recover lost git commits from a few weeks ago?

Time:01-29

I am trying to recover changes that were made to a file using git.

I used reset --hard on my branch which lost a bunch of commits, and these commits contained changes to a text file.

I understand there are functions like reflog in git that can be used to get the hash of commits you thought were lost. In my case, the commits I am trying to recover were from about 3 weeks ago so I am worried that I won't be able to get them back.

Is it possible for me to somehow recover the changes that were made to this file?

CodePudding user response:

You could try the following:

  1. git reflog --pretty=oneline --date=short --before=2022-01-09
    • --pretty=oneline --date=short will add the date to the output and it should make it easier to look through the output
    • --before should make it easier to only look through relevant days
  2. Find the entry that looks like it could have your lost work
  3. git checkout -b my_lost_work (or git switch -c my_lost_work) - create a branch with lost work
  4. git push
  •  Tags:  
  • git
  • Related