How to Remove Last Commit From Local & Remote Git Repository

  • Sometimes by mistake we push commits on remote branch.
  • I’ll show you how to remove/delete last commit from local & remote branch.
[mitesh@shah ~]$ git log --pretty=oneline
037d772391792ff85b394e1e43f5f04bb7515e11 Add PrintFriendly Button
8f2cc3d5b585076d6001117703d677e24f07247f Minor Update on Resume
9e57e45fa58caef1eb95d804997bb5f70708d8c8 HTTPS version in Author Bio
  • Now I want to delete last commit 037d772391792ff85b394e1e43f5f04bb7515e11
# Delete last commit
[mitesh@shah ~]$ git reset --hard HEAD^
HEAD is now at 8f2cc3d Minor Update on Resume

# Delete last two commits
[mitesh@shah ~]$  git reset --hard HEAD~2
HEAD is now at 9e57e45 HTTPS version in Author Bio

[mitesh@shah ~]$ git push -f
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:MiteshShah/edytk.github.io.git
 + 037d772...8f2cc3d master -> master (forced update)


Newsletter

Get updated when I create new content.
Unsubscribe whenever. Never any spam.

Note

I'm Tama. I turn down every ad, affiliate, and sponsor request I get. I write free resources that help people. If you enjoy my content, please consider supporting what I do.

Support my work