0
0
GitComparisonBeginner · 3 min read

Git reset vs git revert: Key Differences and When to Use Each

The git reset command moves the current branch pointer to a previous commit, effectively removing commits from history, while git revert creates a new commit that undoes changes from a previous commit without altering history. Use git reset for local undo and git revert for safe undo in shared repositories.
⚖️

Quick Comparison

Here is a quick side-by-side comparison of git reset and git revert based on key factors.

Factorgit resetgit revert
Effect on commit historyMoves branch pointer, removes commitsAdds a new commit that reverses changes
Safe for shared repos?No, rewrites historyYes, preserves history
Undo scopeCan undo multiple commitsUndo one commit at a time
Changes working directory?Depends on mode (soft, mixed, hard)No, only creates new commit
Use caseLocal undo before pushingUndo after pushing to shared repo
Command typeHistory rewriteHistory preserving
⚖️

Key Differences

git reset changes the current branch pointer to a specified commit. This means commits after that point are removed from the branch history, which can cause problems if those commits were already shared with others. It has three modes: --soft (keeps changes staged), --mixed (keeps changes unstaged), and --hard (discards changes).

In contrast, git revert does not remove commits. Instead, it creates a new commit that reverses the changes introduced by a previous commit. This keeps the history intact and is safe to use on public branches. It is ideal for undoing changes after they have been pushed.

In summary, git reset rewrites history and is best for local fixes before sharing, while git revert preserves history and is best for undoing changes safely in shared environments.

⚖️

Code Comparison

Example: Undo the last commit using git reset.

bash
git reset --hard HEAD~1
Output
HEAD is now at <commit-hash> <commit-message> Working directory reset to this commit.
↔️

Git revert Equivalent

Example: Undo the last commit using git revert.

bash
git revert HEAD
Output
[main <new-commit-hash>] Revert "<commit-message>" 1 file changed, 1 deletion(-) Created a new commit that reverses the last commit.
🎯

When to Use Which

Choose git reset when you want to undo commits locally before sharing your work, especially if you want to clean up or combine commits. Avoid using it on branches others use because it rewrites history.

Choose git revert when you need to undo changes that have already been shared with others. It safely adds a new commit that reverses changes without affecting the commit history.

Key Takeaways

Use git reset to undo commits locally by moving the branch pointer and optionally changing the working directory.
Use git revert to safely undo changes by creating a new commit that reverses a previous commit.
git reset rewrites history and is unsafe for shared branches; git revert preserves history and is safe for collaboration.
Choose git reset for cleaning up local commits before pushing.
Choose git revert to undo changes after commits are pushed to shared repositories.