Skip to content

Git Reset, Revert et Checkout Expliqués

Tableau Comparatif

CommandeRéécrit HistoriquePortéeUsage
resetOui (local)HEAD / index / WDRefaire commits locaux
revertNon (ajoute commit)HEADAnnuler publiquement
checkoutNonHEAD ou fichiersNaviguer / restaurer

git reset

bash
git reset --soft <hash>
git reset --mixed <hash>   # défaut
git reset --hard <hash>
ModeEffet
softDéplace HEAD seulement
mixedHEAD + index
hardHEAD + index + WD

git revert

Crée un commit inverse.

bash
git revert <hash>

Merge revert :

bash
git revert -m 1 <merge_hash>

git checkout

Detached HEAD :

bash
git checkout <hash>

Restaurer fichier :

bash
git checkout HEAD -- src/app.js

Visual Mental

reset  → repositionne pointeurs
revert → ajoute anti-commit
checkout → déplace HEAD / récupère contenu

Sécurité

  • Éviter --hard sans sauvegarde
  • Revert préserve traçabilité

Reflog Sauvetage

bash
git reflog
git branch rescue <hash>

Alternatives Modernes

bash
git switch <branch>

Résumé

Choisir selon visibilité : reset (réécriture), revert (trace), checkout (navigation). Minimiser opérations destructives.