I nearly lost a day’s worth of work the other day! Thankfully, I learned about the git reflog command which saved me though.
I had three local commits ready to be pushed. I did a pull –rebase to pull in the latest changes. The first two of my commits were successfully applied during the rebase but the third one failed. Git alerted me to two files with conflicts which I fixed and staged, but it did not mention the third file. I assumed the third file was fine and ran git rebase –continue. I then realised that the third file was not committed and had been deleted from my working directory. What? I’m still not sure how this happened.
Git reflog to the rescue
I know many uncomfortable source control situations can be averted with Git but the commands to recover my file were a mystery to me. A bit of research and I came across git reflog which can be used to show a record of the commits the tip of a branch points too over time.
git reflog # show the commit history of HEAD
git reflog show some-branch # show the commit history of the tip of some-branch
Running git reflog showed me the hash of the commit I had made with all three of my files before the rebase. I was able to checkout this commit and then recover the file - phew!