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

Heroku

Deliver your unique apps, your own way.

Heroku tackles the toil β€” patching and upgrading, 24/7 ops and security, build systems, failovers, and more. Stay focused on building great data-driven applications.

Learn More

Top comments (0)

Jetbrains image

Build Secure, Ship Fast

Discover best practices to secure CI/CD without slowing down your pipeline.

Read more

πŸ‘‹ Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay