Adloun

Git : l'historique d'un projet (école d'ingénieur) · Chapitre 4 · Application directe du cours

Changer de branche avec du travail en cours

Les branches · niveau 2

Énoncé

Ada est sur main, dans le dépôt de l'exercice 4.3, avec du travail en cours ; git diff --stat main correctif compare les deux branches :


$ git status --short
 M a.txt
 M b.txt
?? note.txt
$ git diff --stat main correctif
 b.txt | 1 +
 1 file changed, 1 insertion(+)
  • git switch correctif va-t-il réussir ?
  • Ada tient à sa modification de a.txt et à note.txt, pas à celle de b.txt. Passer sur correctif sans rien commiter.

Corrigé

  • Non : b.txt diffère entre les deux branches et est modifié localement ; le changement de branche l'écraserait. a.txt, identique sur les deux branches, et note.txt, non suivi, ne gênent pas.
  • On abandonne la modification de b.txt, puis on change de branche ; les deux autres suivent (ex09-en-cours.sh) :

$ git switch correctif
error: Your local changes to the following files would be overwritten by checkout:
        b.txt
Please commit your changes or stash them before you switch branches.
Aborting
$ git restore b.txt
$ git switch correctif
M       a.txt
Switched to branch 'correctif'
$ git status --short
 M a.txt
?? note.txt

Exercices semblables

Le cours du chapitre · Tous les exercices du chapitre