The Git Revert Command
Table of contents
- Difference between Reset and Revert
- Basic syntax
- Revert a single commit
- Revert several commits
- Revert a merge commit
- Handling conflicts
- Best practices
1 - Difference between Reset and Revert
Comparison table
| Aspect | Reset | Revert |
|---|---|---|
| History | Rewritten | Preserved |
| New commit | No | Yes |
| Shared branch | ⚠️ Dangerous | ✅ Safe |
| Traceability | Lost | Complete |
Visualization
Reset: Removes the commits from the history
Avant: A---B---C---D (HEAD)
Après: A---B (HEAD)
C et D sont supprimés
Revert: Creates a new commit that undoes them
Avant: A---B---C---D (HEAD)
Après: A---B---C---D---D' (HEAD)
D' annule les changements de D
When to use which?
| Situation | Command |
|---|---|
| Local branch, not yet shared | reset |
| Shared branch (main, develop) | revert |
| You want to keep a trace | revert |
| You want a clean history | reset (local only) |
🔝 Back to table of contents
2 - Basic syntax
Revert a commit
git revert <commit-hash>
Common options
| Option | Description |
|---|---|
-n or --no-commit | Do not create a commit automatically |
-e or --edit | Edit the message (default) |
--no-edit | Keep the default message |
-m <parent> | Specify the parent for a merge commit |
🔝 Back to table of contents
3 - Revert a single commit
Example
# Voir l'historique
git log --oneline
# abc1234 feat: add broken feature
# def5678 docs: update readme
# ghi9012 Initial commit
# Revert le commit problématique
git revert abc1234
What happens
- Git opens the editor for the commit message
- Default message:
Revert "feat: add broken feature" - A new commit is created that undoes the changes
Result
git log --oneline
# xyz7890 Revert "feat: add broken feature"
# abc1234 feat: add broken feature
# def5678 docs: update readme
# ghi9012 Initial commit
Revert without the editor
git revert abc1234 --no-edit
Revert with a custom message
git revert abc1234 -m "fix: revert broken feature due to bug #123"
🔝 Back to table of contents
4 - Revert several commits
Method 1: One by one
git revert abc1234
git revert def5678
git revert ghi9012
Creates 3 revert commits.
Method 2: Range of commits
# Revert les commits de A à C (A non inclus, C inclus)
git revert A..C
# Exemple : revert les 3 derniers commits
git revert HEAD~3..HEAD
Method 3: Without intermediate commits
# Préparer tous les reverts sans commiter
git revert --no-commit abc1234
git revert --no-commit def5678
git revert --no-commit ghi9012
# Vérifier les changements
git status
git diff --staged
# Un seul commit pour tout
git commit -m "revert: remove features X, Y, Z"
🔝 Back to table of contents
5 - Revert a merge commit
The problem
A merge commit has two parents. Git doesn't know which one to consider "main".
A---B---C feature
/ \
D---E---F---G---M main
↑ ↑
parent1 parent2
Solution: Specify the parent
# -m 1 = garder la ligne principale (main)
git revert -m 1 <merge-commit-hash>
# -m 2 = garder la branche mergée (feature)
git revert -m 2 <merge-commit-hash>
Practical example
# Voir les parents d'un merge commit
git log --oneline <merge-commit>
# Merge: abc1234 def5678
# Revert en gardant main (généralement -m 1)
git revert -m 1 <merge-commit>
Caution!
After reverting a merge, if you want to re-merge the same branch later, you will first have to revert the revert:
# 1. Merge initial
git merge feature # Crée commit M
# 2. Problème, on revert
git revert -m 1 M # Crée commit R
# 3. Plus tard, on veut re-merger feature
# Mais feature semble déjà mergée !
git merge feature # "Already up to date"
# 4. Solution : revert le revert
git revert R # Crée commit R'
# 5. Maintenant on peut continuer avec feature
git merge feature # Fonctionne !
🔝 Back to table of contents
6 - Handling conflicts
Why conflicts?
If the code has evolved since the commit being reverted, Git may not know how to "undo" it cleanly.
Resolve the conflicts
# Le revert génère un conflit
git revert abc1234
# CONFLICT (content): Merge conflict in file.txt
# 1. Voir les fichiers en conflit
git status
# 2. Éditer les fichiers et résoudre les conflits
code file.txt # ou votre éditeur
# 3. Marquer comme résolu
git add file.txt
# 4. Continuer le revert
git revert --continue
Abort a revert in progress
git revert --abort
Conflict example
The file before resolution:
<<<<<<< HEAD
Code actuel après évolutions
=======
Code original (avant le commit à revert)
>>>>>>> parent of abc1234
You must choose which version to keep or combine the two.
🔝 Back to table of contents
7 - Best practices
✅ Use revert when...
- The branch has been shared (pushed)
- Other developers have based their work on it
- You want to keep a clear trace of what happened
- You need to be able to undo the undo later
❌ Do NOT use revert when...
- You are on a private local branch (use reset)
- You want to clean up the history before a merge
Commit messages
A good revert message:
Revert "feat: add payment processing"
This reverts commit abc1234.
Reason: Critical bug causing duplicate charges.
Will be reimplemented after fix in #456.
Recommended workflow
# 1. Identifier le commit problématique
git log --oneline
# 2. Vérifier ce qu'il a changé
git show abc1234
# 3. Faire le revert
git revert abc1234
# 4. Tester que tout fonctionne
npm test # ou vos tests
# 5. Pousser
git push origin main
Summary
| Command | Usage |
|---|---|
git revert <hash> | Undo a commit |
git revert --no-commit <hash> | Prepare without committing |
git revert -m 1 <hash> | Revert a merge |
git revert --abort | Abort if there's a problem |