About an hour ago I wrote a few new features and since I didn't do a git pull in the meantime, it gave an error while doing a git push. While trying to save them, the changes I made were lost. In such scenarios, I would say there was nothing to do and start writing again. But this time, I went to Google thinking there might be a way.
If you did a git add
on change you made, the files are hashed. This means that git is keeping it for you. And isn't that what git is all about? Then something happened that you didn't understand, you did it and some events happened. Your changes are lost. Calm down, take a deep breath and continue reading the article.
use this code to revert the previous hashed change. After writing this code, it creates an output like the one below. In fact, the same output is saved in the .git/lost-found
folder.
Checking object directories: 100% (256/256), done.
dangling commit 42fd85940eb2f76f32d4be5ac2d4b***********
dangling commit 831acdb7115a6284f6e969ac90c7f***********
dangling tree 8cc268a3f48c08cd95329fcd73cc306***********
dangling commit ef7fe1814a47fbf89cbcbb1ce39e4***********
Very nice, we have reached the hashes of the changes we made. Now we will get help from a command called git cat-file
. This code briefly provides the content, type and size information of the changes made.
First, we want to find out which change we want to revert. For this, you can write the code below and verify that it is a correct hash by looking at your commit message.
The expression ef7fe181 is the first 7 digits of the hash output above.
git cat-file -p ef7fe181
tree dafb6a5fabb658210e2fa1534670f6***********
parent 84da27f33676589e46711ad35468***********
author Berat Bozkurt <beratbozkurt1999@gmail.com> 1614455618 +0300
committer Berat Bozkurt <beratbozkurt1999@gmail.com> 1614455618 +0300
upload files
Then we continue the same process, this time writing the tree hash.
git cat-file -p dafb6a5f
100755 blob 1437c53f70bc211ec65739ec4a8c*********** .gitignore
100755 blob 4b412a3cfa4cabbb3a3d8175d265*********** README.md
040000 tree eaeb7e5f2932b079d4837ca65395*********** components
100644 blob 23c81abc6d8bb55de4bfb9567c8a*********** data.js
100755 blob 36aa1a4dc28f1a7d72c037a4ef0d*********** jsconfig.json
100755 blob 8b94735e5d6a222a721b72f9d543*********** package.json
040000 tree e615f7e6d7712b0e91e484b70529*********** pages
040000 tree 45eb3bc0909b493adf37d7bcb11a*********** public
100755 blob 9f7c9dcc239336c67b8f024a91fe*********** site.config.js
040000 tree 9d0b469049594f9921be80ee33e3*********** styles
100755 blob 52c5fa66a84de8b4387803ec20c8*********** yarn.lock
Now we can easily retrieve the contents of the section where the changes were lost. That's why we use the same code again.
git cat-file -p 4b412a3
### Hello World
Hello World!
As you can see, we get to view the file contents. This has been my experience. There might be a better and cleaner way, but until I discover that, this is the best way for me :)
Now, git (it means "go" in Turkish) and bring back the changes you made!
Resources