Undo last Git commit to a previous commit?

Git temporarily switch to a different commit:
 

to checkout any other commit you only need to get commit ID and use checkout command to checkout at specific branch

git checkout 0d1d7fc32

 

How to delete last pushed commit:

Although it is not a good thing to do and git does not provide any tool to delete pushed commit. but you can do it by using following commands.

Use the following steps.

  1. Checkout previous commit using checkout command.
  2. Do a empty commit and push that commit to git repository. By doing this your current branch goes this state at which you are checked 
#Checkout previuos commit using checkout command.
git checkout 0d1d7fc32

#Do a empty commit and push that commit.
git commit -m "git update remote repositoryat thsi state"
git push

 

Hard delete unpublished commits:

You can delete unpushed changes by doing simple following commands 

git stash "some stash"
git reset --hard 0d1d7fc32
git stash pop

Tags:

git

Share:

Related posts