How to get your files back when you delete them, or do git reset – -hard without committing
I made a bunch of changes to my UI. But I thought that the UI looked better before making these changes.. So I wanted to go back to the original HEAD and check it out. At this point my current changes were not yet committed, however they were added (git add . ) to the staging area. I thought I was safe to checkout to another branch and then do git reset –hard HEAD on that branch. The misconception was that the reset would only be performed on this branch and not globally… This was a rookie mistake. All my new changes were lost.
If these changes were commited, then it would be fairly simple to get them back.. Luckily for me, I did “git add .” which efficitevely stores changes in git’s internal database.
- Consider the most recently updated files in git’s object store (.git/objects)
gfind .git/objects/ -type f -printf '%TY-%Tm-%Td %TT %p\n' | sort
2. Save all the output in a file or something, and use “tail -n” to get the amount required
3. Using “git show” we can view contents of an object. However, the format above is not what we desire. Here’s some formatting to enable it. Change “tail -n 23 all” where “all” is the name of the file
tail -n 23 all | awk '{print $3}' | sed s/".git\/objects\/"/""/ | sed 's/\///' | xargs -I {} sh -c 'git show {}'
PS: Because I wanted I wasn’t ready to commit yet and wanted to switch to a different branch, I should’ve used git stash to avoid this mess ..