How to git reset to go back in time

If you’re ever stuck with your coding project and want to go back to a time when it was working, try:

git reset --soft <commit version>

or

git reset --soft HEAD~1

where <commit version> is the first 7 hexadecimal letters of your git commit e.g. 88f0528, which can be found in your GitHub repository when you click on the # commits button.

Or reset the HEAD by 1 commit.

For example: I was stuck on my Art-Explorer project and I scoured my code-base looking for why I was automatically logging in as `undefined` and getting a jwt malformed error in my console.

I couldn’t pin-point the exact location of the bug(s) so I decided to check Netlify to see when the code was working.

It pointed to this commit version (88f0528), and I decided to make my local repository go back to that specific version.

Lo and behold it starts working again locally..

* Another option is to git reset --hard <commit version> which will revert to an earlier time but will not keep your current edits. If you want to keep the changes you’ve made try git stash and git stash pop before hard resetting.

Leave a Reply

Your email address will not be published. Required fields are marked *