MediumConcept

What is the difference between git reset and git revert?

Technology:
Git
Experience:
Mid-level,
Senior
Topics:
Git,
Debugging

Quick answer

git revert creates a new commit that undoes an earlier one and is safe on shared branches; git reset moves the branch pointer back to an earlier commit, effectively removing later commits from that branch鈥檚 history.

Detailed explanation

git revert <commit> calculates the inverse of a commit and records it as a new commit. History only grows, so teammates who already pulled are not affected. This is how you undo a bad change that has reached main or production.

git reset moves the current branch to another commit. With --soft the undone changes stay staged, with the default --mixed they stay in your working tree unstaged, and with --hard they are discarded. It is ideal for cleaning up local commits that have not been pushed.

If you reset by mistake, the commits are usually still recoverable for a while through git reflog, which records where your branch pointed before each move.

Example

bash
# undo a pushed commit safely
git revert a1b2c3d

# rewrite local history (not yet pushed)
git reset --soft HEAD~1   # undo commit, keep changes staged
git reset --hard HEAD~1   # undo commit and discard changes

# recover from a bad reset
git reflog
git reset --hard HEAD@{2}

Key points

  • revert: new commit, history preserved, safe when shared
  • reset: moves the branch pointer, rewrites history
  • --soft keeps changes staged, --mixed unstaged, --hard discards them
  • git reflog can recover "lost" commits

Common mistakes

  • Using git reset --hard on a branch others share, then force-pushing.
  • Forgetting that --hard also discards uncommitted work in the working tree.

Follow-up questions

  • GitMedium

    What is the difference between git merge and git rebase?

    Both integrate changes from one branch into another: merge keeps both histories and joins them with a merge commit, while rebase rewrites your commits so they appear on top of the target branch, giving a linear history.

    Junior 路 Mid-level 路 Git
  • GitMedium

    How do you resolve a merge conflict in Git?

    Open the conflicted files, decide what the final code should be between the conflict markers, remove the markers, run the tests, then stage the files and continue the merge or rebase.

    Junior 路 Mid-level 路 Git
  • GitEasy

    What is Git and how is it different from GitHub?

    Git is a distributed version control system that tracks changes to files on your machine; GitHub is a hosting service for Git repositories that adds collaboration features such as pull requests, code review and CI.

    Intern 路 Junior 路 Git
  • GitEasy

    What is the difference between git fetch and git pull?

    git fetch downloads new commits from the remote without changing your working branch; git pull runs a fetch and then merges (or rebases) those commits into your current branch.

    Intern 路 Junior 路 Git
  • ReactEasy

    Why do we need keys in React lists?

    Keys give each list item a stable identity so React can match items between renders and correctly add, remove or reorder them without recreating or mixing up their state.

    Junior 路 Mid-level 路 Performance
  • CSSMedium

    How does CSS specificity work?

    Specificity is the score the browser uses to decide which rule wins: inline styles beat IDs, IDs beat classes/attributes/pseudo-classes, and those beat elements. When scores tie, the last rule in the source wins.

    Junior 路 Mid-level 路 CSS Layout