Git & GitHub Interview Question #50

What is the difference between git reset and git revert?

Remotes, Fetch, Pull, Push, Merge, Conflicts, Rebase & Stash Mid-Level Intermediate

Quick Interview Answer

git reset moves a branch reference and can also change the index and working tree depending on the selected mode. It is commonly used to adjust local history or staging, but resetting published history can disrupt collaborators.

Detailed Explanation

git reset moves a branch reference and can also change the index and working tree depending on the selected mode. It is commonly used to adjust local history or staging, but resetting published history can disrupt collaborators.

git revert does not remove an existing commit from history. Instead, it creates a new commit that applies the inverse of a selected commit's changes. For already shared history, revert is often the safer collaboration-friendly way to undo a change.

Code Example

# Create a new commit that reverses an old commit
git revert <commit-id>

# Move a local branch back one commit while
# keeping changes staged
git reset --soft HEAD~1

# Be very careful with destructive reset modes
# such as --hard.