Skip to content
Commencer

Secourir son code et réparer ses erreurs

SlidesPDFPPTX
Niveau : Intermediate
Durée : 1h45

Prérequis

  • Maîtriser les commits, branches et fusions (Ateliers 04-06)
  • Savoir faire git status, git log, git diff
  • Avoir déjà poussé sur GitHub (Atelier 07)

Objectifs d'apprentissage

  • Annuler des modifications locales (Working Directory & Staging) avec git restore
  • Choisir entre revert, reset --soft/mixed/hard et comprendre leur danger
  • Mettre de côté avec stash (push/pop, --include-untracked) et nettoyer avec clean
  • Sauver un commit perdu avec reflog, cherry-pick et bisect

Chaque erreur vit dans une zone — chaque zone a son antidote :

Zone État du fichier Commande Impact Historique partagé ?
Working Directory Modifié, non indexé git restore <file> Écrase le local ✅ Sûr
Staging Area Indexé git restore --staged <file> Retire de l’index ✅ Sûr
Local Repository Commité (pas poussé) git reset Recule HEAD ✅ Sûr si pas poussé
Distant Commité & poussé git revert Nouveau commit inverse ✅ Seul sûr après push
<pre dir="ltr" class="mermaid">flowchart TD A[Erreur] --&gt; B{Zone ?} B --&gt;|Working| C[git restore] B --&gt;|Staging| D[git restore --staged] B --&gt;|Local non pushé| E[git reset --soft/mixed/hard] B --&gt;|Déjà pushé| F[git revert]</pre>
Terminal window
git restore index.html
# ou legacy :
git checkout -- index.html
Terminal window
git restore --staged index.html
# équivalent :
git reset HEAD index.html

Le fichier repasse en modified mais reste sur disque.

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

2. revert vs reset — choisir le bon outil

Section titled “2. revert vs reset — choisir le bon outil”

revert — pour l’historique partagé (pushé)

Section titled “revert — pour l’historique partagé (pushé)”

Crée un nouveau commit qui inverse le précédent — ne réécrit pas l’historique :

Terminal window
git log --oneline
# a1b2c3 feat: add notifications (à annuler)
# b0c9d8 fix: typo
git revert a1b2c3
# → commit "Revert feat: add notifications"

À utiliser si a1b2c3 est déjà sur origin/main.

reset — pour l’historique local (pas poussé)

Section titled “reset — pour l’historique local (pas poussé)”

Recule HEAD. 3 modes :

<pre dir="ltr" class="mermaid">graph TD A[Dernier Commit] --&gt;|git reset| B{Mode} B --&gt;|--soft| C[Index gardé&lt;br/&gt;prêt à re-committer] B --&gt;|--mixed| D[Working gardé&lt;br/&gt;à re-ajouter] B --&gt;|--hard| E[Tout détruit&lt;br/&gt;irréversible]</pre>
Terminal window
git reset --soft HEAD~1 # annule commit, garde index
git reset --mixed HEAD~1 # défaut : garde Working, vide index
git reset --hard HEAD~1 # tout jette
git reset --hard a1b2c3 # vers un hash
Mode Commit Index Working Cas
soft Reculé Gardé Gardé Reformuler message, squasher
mixed Reculé Vidé Gardé Refaire add proprement
hard Reculé Vidé Jeté Annuler tout, danger

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

3. Mettre de côté : stash & nettoyer : clean

Section titled “3. Mettre de côté : stash & nettoyer : clean”

Idéal pour changer de branche vite sans committer :

Terminal window
git stash push -m "wip: login form"
git stash list
# stash@{0}: wip: login form
git stash pop # applique + supprime
git stash apply # applique sans supprimer
git stash drop stash@{0}
git stash clear

Avec untracked :

Terminal window
git stash push --include-untracked -m "avec nouveaux fichiers"
git stash push --staged -m "seulement l'index" # Git ≥ 2.35 : stash UNIQUEMENT ce qui est stagé
git stash push --keep-index -m "tout sauf l'index" # stache TOUT mais garde l'index intact dans le working tree
Terminal window
git clean -n # dry-run : que va-t-il effacer ?
git clean -f # efface fichiers non suivis
git clean -fd # + dossiers
git clean -fdx # + ignorés par .gitignore (danger)

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

reset --hard a mangé un commit ? Branche supprimée ? reflog vous sauve — 90 jours par défaut avant garbage collection (30 jours pour les commits orphelins).

Terminal window
git reflog
# a1b2c3 HEAD@{0}: reset: moving to HEAD~1
# e4f5a6b HEAD@{1}: commit: feat: add notifications
# c7d8e9f HEAD@{2}: commit: fix: oauth

Restaurer :

Terminal window
git reset --hard HEAD@{1}
# ou
git reset --hard e4f5a6b
git log --oneline -3

Voir le reflog d’une branche :

Terminal window
git reflog show main
git log -g --oneline # parcourt le reflog

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

cherry-pick — prendre un commit ailleurs

Section titled “cherry-pick — prendre un commit ailleurs”
Terminal window
git switch main
git cherry-pick e4f5a6b
# applique e4f5a6b sur main
git cherry-pick --abort # en cas de conflit

Git coupe l’historique en binaire pour trouver le commit coupable :

Terminal window
git bisect start
git bisect bad # commit actuel bugué
git bisect good a1b2c3 # commit ancien OK
# → Git checkout un milieu, testez, puis :
git bisect good # ou bad
# → répète jusqu'à trouver
git bisect reset

Question

Quelle commande annule un fichier non indexé ?

Cliquer pour révéler la réponse
Réponse

git restore <file> (ou checkout --)

Cliquer pour voir la question
Question

Comment déstager sans perdre le fichier ?

Cliquer pour révéler la réponse
Réponse

git restore --staged <file> ou git reset HEAD <file>

Cliquer pour voir la question
Question

revert vs reset ?

Cliquer pour révéler la réponse
Réponse

revert = nouveau commit inverse (sûr si pushé). reset = recule HEAD (seulement si pas pushé).

Cliquer pour voir la question
Question

3 modes reset ?

Cliquer pour révéler la réponse
Réponse

--soft (garde index), --mixed (garde Working), --hard (tout jette).

Cliquer pour voir la question
Question

À quoi sert stash ?

Cliquer pour révéler la réponse
Réponse

Mettre de côté le Working + index temporairement sans committer (pop/apply).

Cliquer pour voir la question
Question

Que fait clean -fd ?

Cliquer pour révéler la réponse
Réponse

Efface fichiers/dossiers non suivis (non indexés).

Cliquer pour voir la question
Question

Comment sauver un commit après reset --hard ?

Cliquer pour révéler la réponse
Réponse

git reflog → repérer HEAD@{n} → git reset --hard HEAD@{n}.

Cliquer pour voir la question
Question

cherry-pick et bisect ?

Cliquer pour révéler la réponse
Réponse

cherry-pick = appliquer un commit précis. bisect = recherche binaire du commit bugué.

Cliquer pour voir la question

1. Quelle commande déstage sans perdre le fichier ?

2. Quel reset détruit Working ?

3. Quelle commande restaure et vide la pile stash ?

4. Commit pushé à annuler → que faire ?


Terminal window
# Local
git restore <file>
git restore --staged <file>
git clean -n; git clean -fd
# Revert vs Reset
git revert <hash> --no-edit
git reset --soft HEAD~1
git reset --mixed HEAD~1
git reset --hard HEAD~1
# Stash
git stash push -m "wip" --include-untracked
git stash list; git stash pop; git stash apply; git stash drop
# Reflog
git reflog; git reflog --oneline -10
git reset --hard HEAD@{1}
git reset --hard <hash>
# Avancé
git cherry-pick <hash>
git cherry-pick --abort
git bisect start; git bisect bad; git bisect good <hash>; git bisect reset

Votre Progression0%


Vous savez réparer. Passons à l’open source : Fork, gh CLI, gouvernance et sécurité.

Et maintenant ?

HashCode Workshops
Part of the JoinHashCode ecosystem

Ctrl + K Rechercher dans les workshops