Skip to main content

Command Palette

Search for a command to run...

How to undo the last commit in Git?

Published
โ€ข1 min read
How to undo the last commit in Git?
M

Hi, I'm Melvin George! ๐Ÿ–๏ธ. A web developer and a blogger.

Iโ€™ve written code for Mozilla, Qiskit, and an active contributor to many frontend open-source projects that are being used by millions of people around the world. ๐Ÿš€

I love JavaScript, Rust, React.js, and many new exciting technologies that help society to find a solution to an existing problem ๐Ÿ’–.

Come check out my blog to see more of software development posts on these technologies. ๐Ÿฆ„

Originally Published Here ๐Ÿš€!

To undo the last commit in Git, you can use the git reset command.

You have 2 ways to undo the last commit:

  • The first is the
git reset --soft HEAD~1

this command will undo the last commit. For most cases, this command can be used and safe to use. This command will also make sure to preserve the changes in uncommitted files.

  • The second one is the
git reset --hard HEAD~1

Will also undo the last commit but will not preserve the changes to uncommitted files and resets everything. Be cautious when using the above command.

Feel free to share if you found this useful ๐Ÿ˜ƒ.


G
Ghost3y ago

Thanks for making this post! I've been doing a lot of resets lately at work, but I remember being clueless as to what to do when caught in this situation at random ๐Ÿ˜†

More from this blog

MELVIN GEORGE's Blog

626 posts

Hi, I'm Melvin George! ๐Ÿ–๏ธ. A web developer and a blogger.