How Git Revert Works Under the Hood
Unlike git reset, which removes commits from the branch timeline, git revert inspects the exact diff introduced by a specific commit and creates a new commit that applies the inverse diff.
For example, if commit c7e8a1f added 20 lines to auth.ts, running git revert c7e8a1f creates a new commit that deletes those 20 lines. Because history moves strictly forward, no team members are disrupted when pulling the branch.
Frequently Asked Questions
Why should I use git revert instead of git reset for pushed commits?
Git reset rewrites commit history. If you force-push an altered history to a shared branch like 'main', other developers will encounter diverged branch errors. Git revert creates a forward-moving rollback commit that preserves history safely.
How do I revert a merge commit or merged Pull Request?
Run 'git revert -m 1 <commit-sha>'. The '-m 1' flag tells Git to treat the target branch (usually main or develop) as the mainline parent to keep.
Can I revert multiple commits at once?
Yes, you can specify a range using 'git revert <oldest-sha>~1..<newest-sha>' or run 'git revert --no-commit HEAD~3..HEAD' to stage all inversions in a single commit.
What happens if git revert has merge conflicts?
Git will pause and let you resolve conflicts in the affected files. Once resolved, run 'git add .' and 'git revert --continue' to finalize the rollback.