CodeBlanks Logo Icon CodeBlanks

revert vs reset

Learn that revert preserves history while reset removes it.

Difference between revert and reset

git reset moves the branch pointer backward, effectively erasing commits that came after the target point. In contrast, git revert does not delete any commits; it leaves past history untouched and adds an inverting commit on top. This distinction is vital on shared branches.

# Reset: moves history back (history erased)
git reset --hard HEAD~1
# Revert: adds forward commit (history preserved)
git revert --no-edit HEAD

While reset is suited for private cleanup, revert is always required for published commits.

History integrity and audit trail

When using git revert, the exact moments when a bug was introduced and reversed remain preserved as an audit trail. git reset obliterates the bug as if it never existed, which can erase valuable clues during debugging. In team environments, git revert is the standard for production branches.

git log --oneline
# Revert "Hatali kod"
# Hatali kod
# Kararli surum

Keeping history transparent makes software evolution auditable and accountable.

Practice Verify commit count after revert

Revert the latest commit with git revert --no-edit -q HEAD and display the total commit count with git rev-list --count HEAD.

Starter code

terminal
git init -q -b main
git config user.name "Cem"
git config user.email "cem@example.com"
echo "stabil" > sistem.txt
git add sistem.txt
git commit -qm "Stabil surum"
echo "hata" >> sistem.txt
git add sistem.txt
git commit -qm "Hatali commit"

# Revert uygula ve toplam commit sayisini ekrana bas

Sample solution

terminal
git init -q -b main
git config user.name "Cem"
git config user.email "cem@example.com"
echo "stabil" > sistem.txt
git add sistem.txt
git commit -qm "Stabil surum"
echo "hata" >> sistem.txt
git add sistem.txt
git commit -qm "Hatali commit"

git revert --no-edit -q HEAD
git rev-list --count HEAD

Expected output

3

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Why should git revert be preferred over git reset to undo a commit on a shared main branch?

  • git revert opens a GUI in terminal to prevent conflicts
  • git reset always throws an error and fails in local repositories
  • git reset rewrites history breaking teammates' repos, while revert safely adds a new commit Correct answer
  • The git reset command can only undo empty commits

Explanation: Rewriting history with reset on shared branches breaks other developers' histories; revert adds a forward-moving commit without deleting anything.

2 Stage without committing in revert

Question
git revert ____ HEAD
Answer
git revert -n HEAD

Explanation: The -n (or --no-commit) option stages the inverted changes without automatically creating a commit.

3 Revert and reset features

Term Meaning
Revert yaklaşımı Adds a new forward inverse commit
git reset Moves branch pointer backwards
Ortak dal kuralı Revert is used on shared branches
Denetim izi Documents errors and their reverts

All lessons in Intermediate

20 Intermediate final

  1. Branch management
  2. Rebase
  3. Conflict
  4. PR flow