Sign up with your email address to be the first to know about new products, VIP offers, blog features & more.

 




How to undo Git Commits

Undo a git commit and redo
  1. So, you’ve made a commit and now it has to be removed.
  2. This leaves your working tree (the state of your files on disk) unchanged but undoes the commit and leaves the changes you committed unstaged (so they’ll appear as “ Changes not staged for commit ” in git status  and you’ll need to add them again before committing). If you only want to add more changes to the previous commit, or change the commit message [1], you could use git reset --soft HEAD~  instead, which is like git reset HEAD~  but leaves your existing changes staged.
  3. Make corrections to working tree files.
  4. git add  anything that you want to include in your new commit.
  5. Commit the changes, reusing the old commit message. reset copied the old head to .git/ORIG_HEAD;  commit with -c ORIG_HEAD  will open an editor, which initially contains the log message from the old commit and allows you to edit it. If you do not need to edit the message, you could use the -C option.

1 Note, however, that you don’t need to reset to an earlier commit if you just made a mistake in your commit message. The easier option is to git reset (to unstage any changes you’ve made since) and then git commit --amend , which will open your default commit message editor pre-populated with the last commit message.

Beware however that if you have added any new changes to the index, using commit –amend will add them to your previous commit.

How to undo a local git commit

Let’s say a commit is made locally, but now that local commit has to be removed:

To restore everything back to the way it was prior to the last commit, we need to reset to the commit before HEAD:

Now git log will show that the last commit has been removed.

How to undo a public commit

If you have already made your commits public, you will want to create a new commit which will “revert” the changes you made in your previous commit (current HEAD).

Your changes will now be reverted and ready for you to commit:

 

Authored by: StackOverflow/CommunityMark Amery , Andrew StackOverflow User

Refrence: How to undo last commit

Thank you for your love.
share

No Comments Yet.

What do you think?

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