Adloun

Git : l'historique d'un projet (école d'ingénieur) · Chapitre 5 · Exercice de TD

Une pile de remises

Réécrire l'historique · niveau 2

Énoncé

Ada a fait deux remises successives :


$ git stash push -m "essai sur a"
Saved working directory and index state On main: essai sur a
$ git stash push -m "essai sur b"
Saved working directory and index state On main: essai sur b
$ git stash list
stash@{0}: On main: essai sur b
stash@{1}: On main: essai sur a

Elle veut réappliquer l'essai sur a.txt, puis retirer cette remise de la pile sans toucher à l'autre. Quelles commandes ? Que montre git stash list après chacune ?

Corrigé

L'essai sur a.txt est le plus ancien : stash@{1}. git stash apply le réapplique sans le retirer ; git stash drop le retire ensuite :


$ git stash apply stash@{1}
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
        modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
$ git stash list
stash@{0}: On main: essai sur b
stash@{1}: On main: essai sur a
$ git stash drop stash@{1}
Dropped stash@{1} (09907a58eb2649ef8c066e5218e18fd84abfe78e)
$ git stash list
stash@{0}: On main: essai sur b

Après le drop, l'autre remise est renumérotée : elle reste stash@{0}, et la pile n'en compte plus qu'une. Si l'on avait retiré stash@{0} d'abord, l'essai sur a.txt serait devenu stash@{0} : mieux vaut relire git stash list avant chaque drop.

Exercices semblables

Le cours du chapitre · Tous les exercices du chapitre