Git
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.
Exercise 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.
Show example solution
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
Test yourself
-
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
- The git reset command can only undo empty commits
Show answer
git reset rewrites history breaking teammates' repos, while revert safely adds a new commit — Rewriting history with reset on shared branches breaks other developers' histories; revert adds a forward-moving commit without deleting anything.
-
Stage without committing in revert
git revert ____ HEAD
Show answer
git revert -n HEAD
-
Revert and reset features
- 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
Next lesson: Reverting a specific commit