revert vs reset
- Git course
- Intermediate
- Lesson 38/82
- 4 min
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 HEADWhile 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 surumKeeping 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
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 basSample 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 HEADExpected 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
git revert ____ HEAD
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 |