git revert
Create a new commit that safely undoes a previous commit
Practical Example
git revert HEAD # undo the latest commit safely git revert abc1234 # undo a specific commit git revert HEAD~3..HEAD # revert a range of commits
Command Overview & How It Works
Creates a brand new commit that inverses all the changes from a target commit, effectively undoing it without rewriting history. This is the safe undo command for commits that have already been pushed to shared branches. Unlike git reset, revert does not move branch pointers — it adds a new commit on top, so the existing history is preserved. Use git revert HEAD to undo the most recent commit, git revert HEAD~3 to revert the commit three steps back, or git revert <commit-hash> to target a specific commit. If the revert causes conflicts, resolve them normally and git revert --continue. Use git revert --no-commit to stage the inverse changes without automatically committing — useful for reverting multiple commits in a single commit. Revert is the default strategy for undoing changes in collaborative workflows.
Practical Tips
- •Prefer revert over reset for any commit that has been pushed to a shared branch — it adds a safe inverse commit.
- •Revert a range with git revert HEAD~3..HEAD, or stack multiple --no-commit reverts into a single commit.
- •If a revert conflicts, resolve the conflict, stage the files, and run git revert --continue.