Git Revert Commit — Undo Last Commit

amol pawar
softAai Blogs
Published in
2 min readJan 27, 2023

--

Let’s say we are working on your code in Git and something didn’t go as planned. So now we need to revert our last commit. How do we do it? Let’s find out!

There are two possible ways to undo your last commit

  1. revert Command — The revert command will create a commit that reverts the changes of the commit being targeted. means here git will create a new commit that contains reverted changes so that we will maintain commit history in the shared repository.
git revert <commit name to revert>

Example :

— -> commit 1 — → commit2 — → commit c1

git revert c1

— -> commit 1 — → commit2 — → commit3 — → commit reverting c1

2. reset Command — the reset command to undo your last commit. So be careful. it will change the commit history, it will move the HEAD of the working branch indicating commit and discard anything after.

we use the reset command with two options

a. The --soft option means that you will not lose the uncommitted changes you may have.

git reset --soft HEAD~1

--

--

amol pawar
softAai Blogs

Senior Android Developer @ softAai Apps 👨‍💻 Tech Blogger @ softAai Blogs✍️https://softaai.com