ShakaCode | ShakaCode Blog | Rails On Maui Blog | Rails | ReactJs | JavaScript | Webpack | Productivity |

Time Machine for Rails Devs? How to undo git reset --hard?

I just had an experience where Time Machine saved me when doing some work on React on Rails for PR 1240.

A long time ago, I had pushed a commit as WIP, then later reset so I could edit the files.

To go back to editing all the files on the branch, I’ll sometimes do a

git reset HEAD~

Many weeks later, I then went into my terminal and figured that if I was working on something that I didn’t commit, maybe it wasn’t important. So I did:

git reset --hard

OOPS! I then recalled that I probably had some WIP work!

Could a backup help me?

I used Time Machine to restore my react_on_rails folder to my ~/Downloads directory.

I fired up the terminal there, and magically I was right where I was before my big git mistake. Nice!

Well, I really wanted to see how much I would have lost. So I made a new branch and committed my changes there. This gave me a commit that I could cherry-pick.

git checkout -b temp
git commit -am WIP2
git checkout justin808/updates-for-hmr-with-webpacker-v4
git cherry-pick some-commit-sha

I resolved the conflicts, and here they are in this commit.

It’s nice to feel that backups sometimes help!