Undoing Changes in Git: Reset, Revert, Checkout

Even the best of developers can go wrong sometimes, but what makes them great is knowing undoing changes in Git quickly and safely. Committed wrong code, merged too soon, or accidentally edited the wrong file, and suddenly, you feel lost and stuck. Git offers reset, revert, and checkout to help.

And in this article, you will get to learn places these commands differ, their real-world use cases, and how you can recover your code using Git version control with confidence. By the end of it, you will learn how to undo the last commit in Git, roll back merges, and checkout previous commits without risks.

Why Do You Need to Undoing Changes in Git?

Simply speaking, version control is the way to keep a check on changes while having a good compromise on collaborating. But mistakes happen. You may:

  • Commit the wrong file
  • Merge the wrong branch
  • Push broken code
  • Modify or delete important lines

This is where all that undo magic of GIT comes in. But before everyone knows them, understanding their purpose and limitations becomes pretty important.

Understanding the Basics: Reset vs Revert vs Checkout

We’ll quickly run through what each one does, and then look at some examples.

➤ git reset
Changes the commit history of your branch: either unstaging the changes or removing some commits.

➤ git revert
Adds a new commit that undoes the effects of a past commit and is safe for public/sharing branches.

➤ git checkout
A way to view or go back to a previous commit or branch; also for discarding changes.

How to Undo Last Commit in Git

Depending on whether or not you’ve pushed the last commit and whether you want to keep the changes on your working directory or not, you’ll have different ways to undo the last commit in Git.

➤ Option 1: Soft Reset (Keeps changes)
git reset –soft HEAD~1
This goes back by one commit but keeps the changes in the staging area. This is a great option for situations in which you forgot to change a message or add something on a commit.

➤ Option 2: Mixed Reset (Unstages but keeps code)
git reset –mixed HEAD~1
It unstages the files but does not delete them from the working directory.

➤ Option 3: Hard Reset (Dangerous!)
git reset –hard HEAD~1
This rolls back the last commit along with its changes—use with caution.

Pro Tip: Understand what a git reset soft or hard can do for you before you actually use it. A soft reset is safe and reversible while a hard reset is irreversible once run.
Use these options wisely when figuring out how to undo last commit in Git.

Undo Merge in Git

Suppose you’ve accidentally merged a feature branch into your main branch- what do you do now?

➤ Option 1: Revert the merge commit
If you’ve already pushed the merge:
git revert -m 1 <merge-commit-hash>

It is the safest method to undo merge in Git on shared branches as it keeps the history and reverts the changes by an additional commit. 

➤ Option 2: Reset to previous commit
If the merge hasn’t been pushed:
git reset –hard HEAD~1

This will delete the merge commit and all changes from it. However be cautious since this may affect those who have already pulled it down.
Knowing how to undo merge in Git can save hours of head scratching and confusion among team members.

Git Checkout Previous Commit: When and How

“Checking out” a previous commit means to go back in time and see your project as it was in that specific moment without changing anything in history. This is where the checkout command shines.

➤ View Previous Commit
git checkout <commit-hash>

This puts your working copy into a previous commit snapshot. Your repo has now entered the so-called “detached HEAD” state.

➤ Restore a File from Previous Commit
git checkout <commit-hash> — path/to/file

This one allows you to recover deleted or modified files from an older commit without checking out another branch.

➤ Create a Branch from Old Commit
If you want to work from that older state:
git checkout -b old-state <commit-hash>

You’ll use git checkout previous commit often for debugging, hotfixes, and time-travel-style development.
Always create a new branch if you plan to make changes after checking out a previous commit.

Git Undo Commit Without Losing Code

Sometimes you just want to undo a commit but not lose your code. That’s where reset –soft shines again.
git reset –soft HEAD~1

This undoes the last commit, sending your changes to the staging area. This is the ideal command to use when fixing typos, adding forgotten files, or changing commit messages.”

You can also use:
git commit –amend

This edits the last commit without touching your code.If you’re wondering how to git undo commit cleanly, these are your go-to tools.

Git Reset Soft vs Hard: Know the Risk

Let’s compare the two directly:

CommandKeeps Code?Keeps Staging?Safe to Use?
–soft✅ Yes✅ Yes✅ Yes
–mixed✅ Yes❌ No✅ Yes
–hard❌ No❌ No⚠️ Dangerous

The choice between git reset soft vs hard depends on how much you want to remove. Never use –hard unless you’re absolutely sure!

Summary: Which Command to Use When?

TaskBest Command
Undo last commit (keep code)git reset –soft HEAD~1
Undo last commit (delete code)git reset –hard HEAD~1
Undo commit on shared branchgit revert <commit>
View previous commitgit checkout <commit>
Undo file changesgit checkout — file
Undo accidental mergegit revert -m 1 <hash> or reset

Understanding how to undo changes in Git is essential for clean workflows, bug fixes, and confident collaboration.

Leave a Reply

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