DEV Community

Eva
Eva

Posted on

Reverting to a Previous Commit with Git

| tech |

Working with Git and GitHub using Visual Studio Code

| goal |

Reverting to a previous commit that was already pushed to GitHub and potentially getting rid of any updates I made since the commit

| solution |

In your terminal on VSCode, enter git log to see your previous commits:

Image description

Locate the commit that you'd like to revert to and copy its ID:

Image description

Then enter git stash to stash away any changes you've done since the last commit, and enter git rest --hard 7a217627b999b54b6e60d8a2c7f61e7054da8f7b (the long string is the commit ID).

Your files should revert to whichever commit you choose after these steps. If you wish to retrieve the work that you stashed away, use the command git stash pop and they'll show up, but in my case, I don't need the work anymore. I've seen articles suggesting using the command git rest --hard 7a217627b999b54b6e60d8a2c7f61e7054da8f7b right away without stashing your work, but that is a less preferred approach to me.

Happy coding!

source: stackoverflow

Top comments (0)