DEV Community

pawan deore
pawan deore

Posted on

ultimate guide for git revert, getting your changes back

in Project repository, when you face " oh Shit! " moments πŸ˜‰ .In this article i will tell you how to manage those.

lets start with
1] you added something in staging area ( git add . ), now you want it in working area
-> git rm --cached file_name

2] you committed something which you are not supposed to!
-> git reset HEAD~1
( this will take 1 commit back )

3] you committed something & pushed to (GitHub/Bitbucket) also which you are not supposed to!

-> git pull origin master
-> git log ( check for stable commit )
-> git revert #STABLE_HASH
-> git push origin master
( this will pull latest things from Remote and check for stable commit then revert and push )

4] you want to reset to particular commit but also want to keep current changes
-> git reset --soft #_HASH
( this will reset the head but also will keep the changes 1 - you can cross check with the -> git status)

5] don't do this, but still if you need then ..
-> git reset --hard #_HASH
( this will remove all changes above this hash ( latest too ) now you can check with -> git log )

lets say you did it accidentally, and now you want your changes back !!

6]
-> git reflog
-> git checkout #_REQUIRED_HASH
-> git log ( check for your changes )
-> git branch new_branch ( you need to create branch )
-> git checkout new_branch
-> git log ( check again if it is reflected )

YES!

this last point [ 6 ] if you are trying to do after 90 days ( after git reset hard ) my friend i can't save you nowπŸ˜‚, because git reflogs are saved only for 90 days i guess, then garbage collected. do comment if you know ways to go further this !

Image description

Top comments (0)