Sometimes, User commits the code accidently and then he realize that something is wrong or missing in our code. Suppose below is your accidental commit.

What is Git?

$ git commit -m "Committing my code changes"

In such case user can undo his commit without harming existing code. Below are some importance command in order to undo commit.

$ git reset HEAD~  

Above command is responsible for the undo. It will undo your last commit while leaving your working tree (the state of your files on disk) untouched. You’ll need to add them again before you can commit them again).

After running above command, Make corrections to working tree files whatever you want. and then run below command to add anything that you want to include in your new commit.

$ git add .

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 as mentioned in below command.

$ git commit -c ORIG_HEAD

Alternatively, to edit the previous commit (or just its commit message)commit –amend will add changes within the current index to the previous commit.

To remove (not revert) a commit that has been pushed to the server, rewriting history with git push origin master –force is necessary.