0
0
Gitdevops~7 mins

Difference between reset and revert in Git - CLI Comparison

Choose your learning style9 modes available
Introduction
Sometimes you want to undo changes in your project. Git offers two ways: reset and revert. They undo changes differently and affect your project history in unique ways.
When you want to completely remove recent commits from your project history before sharing with others.
When you want to undo a commit but keep the history intact for others to see what happened.
When you accidentally committed something and want to erase it as if it never happened.
When you want to fix a mistake by adding a new commit that cancels out the bad one.
When you want to clean your working directory and staging area to a previous state.
Commands
Shows the last 3 commits in short form to identify which commit to reset or revert.
Terminal
git log --oneline -3
Expected OutputExpected
a1b2c3d Fix typo in README 9f8e7d6 Add user login feature 4d5e6f7 Initial commit
--oneline - Shows each commit in one line for easy reading
-3 - Limits output to last 3 commits
Moves the current branch pointer back to commit 9f8e7d6 and discards all changes after it, including in working directory and staging area.
Terminal
git reset --hard 9f8e7d6
Expected OutputExpected
No output (command runs silently)
--hard - Resets working directory and staging area to match the commit
Verifies that the branch history now ends at commit 9f8e7d6 after reset.
Terminal
git log --oneline -3
Expected OutputExpected
9f8e7d6 Add user login feature 4d5e6f7 Initial commit
--oneline - Shows each commit in one line
-3 - Limits output to last 3 commits
Creates a new commit that undoes the changes introduced by commit a1b2c3d, preserving history.
Terminal
git revert a1b2c3d
Expected OutputExpected
[main 7g8h9i0] Revert "Fix typo in README" 1 file changed, 1 deletion(-)
Shows the last 3 commits including the new revert commit.
Terminal
git log --oneline -3
Expected OutputExpected
7g8h9i0 Revert "Fix typo in README" a1b2c3d Fix typo in README 9f8e7d6 Add user login feature
--oneline - Shows commits in one line
-3 - Limits output to last 3 commits
Key Concept

If you remember nothing else, remember: reset changes history by moving branch pointer and can erase commits, while revert adds a new commit to undo changes without changing history.

Common Mistakes
Using git reset --hard on a shared branch
It rewrites history and can cause problems for others who already pulled those commits.
Use git revert to safely undo changes on shared branches.
Expecting git revert to remove commits from history
Revert does not remove commits; it adds a new commit that undoes changes.
Understand revert preserves history and is safe for public branches.
Not specifying --hard with git reset when wanting to discard changes
Without --hard, reset only moves the branch pointer but leaves working files unchanged.
Use git reset --hard to reset both history and working directory.
Summary
git reset moves the branch pointer and can erase commits and changes.
git revert creates a new commit that undoes a previous commit without changing history.
Use reset for local undo before sharing; use revert for safe undo on shared branches.