There is a point to this story, but it has temporarily escaped my mind...
Contact Me MyFaceBook MyLinkedIn MyGitHub MyTwitter

Delete Last Commit in GIT

Once in a while late at night when I'm sleepy, I'll commit stuff to my repository that I wish I hadn't. Then I spend the next 10 to 15 minutes searching the Internet how to remove the last commit I made. So after a few times, I decided to record how to do it here so that I refer to it later.

Keep in mind that one you push your repository, you cannot delete the revision… To delete the last commit,

git reset --soft HEAD~1

HEAD~1 is a shorthand for the commit before head. Alternatively you can refer to the SHA-1 of the hash you want to reset to. Note that when using –soft any changes to tracked files in the working tree since the commit before head are still stored in the working copy. If you want to wipe out the work you have done, you can use –hard option that will delete both the commit and all of your changed files “Changes to be committed”, as git status would put it.

If you have already pushed and someone pulled which is usually not my case, you can't use GIT reset. Instead, you'll need to do a GIT revert,

git revert HEAD

This will create a new commit that reverses everything introduced by the accidental commit.

Copyright © 2022 by Julian Easterling. SOME RIGHTS RESERVED.
Privacy Policy              Terms of Use             


Creative Commons License
Except where otherwise noted, content on this site is
licensed under a Creative Common Attribution-Share Alike 4.0 International License.


All of the opinions expressed on this website are those of Julian Easterling and
do not represent the views of any of my current and previous clients or employers in any way.

If you notice an error on the site or content that has not been properly attributed, bring
it to my attention using the contact page and I will endeavor to fix it as soon as I can.

I accept no responsibility or liability for any damages incurred by following any of
my advice or by using any of the information on my site or of those sites that I link to.