How to revert back to the actual commit after a hard reset..

11th August 2016

Today morning I was required to do a commit and push but sadly I commited two wrong files out of carelessness oviously. SO I DID the most uncool thing to do in a git instead of a checkout on a git I did

git reset — hard . NEver should have done that now i had my git messed up the files gone and it was messy if u had 10 files u have worked on now gone blank.poof !!! vanished in thin air !!!

So let me give snap shots of the what i did and how i got back to original back shot

The git log command showed some lists like

commit 4ecd55f25c38362d79832935fb95cd9374018731
Author: prasenjit.chowdhury@ebutor.com <prasenjit.chowdhury@ebutor.com>
Date: Thu Aug 11 11:58:50 2016 +0530
Including The comments and dmpi console class
commit b86f2b2f74ce63d70b2c3521fec8886870e5ae6d
Author: Raju Aavudoddi <300103@suneraindia.com>
Date: Thu Aug 11 11:55:57 2016 +0530
SE-585 #comment Create PO Changes

I needed to go back to the last commit so i did the most dreaded thing in git ..

git reset — hard b86f2b2f74ce63d70b2c3521fec8886870e5ae6d

All my commited files were gone I need it back so I issued this command .

git reflog

Click on the link to read more about git reflog

It will return you back the reflected history of all the action in git repo mind it its your local repo not the remote.

4ecd55f HEAD@{1}: commit: Including The comments and dmpi console class
bf9f81d HEAD@{2}: pull origin dev: Merge made by the 'recursive' strategy.
a820865 HEAD@{3}: reset: moving to HEAD@{1}
dda027e HEAD@{4}: reset: moving to dda027eff8235a9546c8fbddf51ac1b474614d3f
a820865 HEAD@{5}: commit: Modifying the resque lib @prasenjit Do not change this one
dda027e HEAD@{6}: commit: Making json default to int in the structure for api calls
044b894 HEAD@{7}: pull origin dev: Merge made by the 'recursive' strategy.

so this will list the heads the git have been now the savior command

git reset — hard HEAD@{n}

n beign where ever you wan to go mine was

git reset — hard HEAD@{1}

You will get back the files Voila !!! now copy back and do the excat you you should roll back a change on git …

It is done using git checkout way …

How to undo a local commit ..

git log     commit 101: bad commit    # latest commit, this would be called 'HEAD'     commit 100: good commit   # second to last commit, this is the one we want..
git reset --soft HEAD^     # use --soft if you want to keep your changes git reset --hard HEAD^     # use --hard if you don't care about keeping the changes you made

How to undo a public commit

git revert HEAD

Your changes will now be reverted and ready for you to commit:

git commit -m 'restoring the file I removed by accident' git log     commit 102: restoring the file I removed by accident     commit 101: removing a file we dont need     commit 100: adding a file that we need

For more info, check out Git Basics — Undoing Things

Hope this one saves someone time ….