Accueil·Commandes du terminal

git checkout

git

Syntaxe

git checkout <branch> | git checkout -b <new> | git checkout -- <file>

L'ancienne commande qui change de branche et restaure des fichiers, scindée en switch et restore dans git 2.23 ; checkout -- <fichier> efface définitivement les modifications non commitées.

Options courantes

OptionSignification
-b <new>create the branch and switch to it
-B <new>create or reset it to the current commit
-- <file>overwrite the file with the staged version, losing your edits
--track <remote>/<branch>start a local branch that follows a remote one
--detachsit on a commit with no branch attached
-fthrow away local changes to switch anyway

Exemples

git checkout -b feature/login

branches off the current commit and moves there

git checkout main

switches branch, same as git switch main

git checkout -- src/app.ts

discards uncommitted edits to that file; they are gone

Ce qui embrouille, c’est qu’annuler a plusieurs formes : déplacer là où pointe une branche n’est pas ajouter un commit qui l’inverse.

Comment lire

  • Les crochets [ ] marquent une partie facultative.
  • Les points de suspension … signifient qu’on peut en mettre plusieurs.
  • Les options sont sensibles à la casse : sur certaines commandes, -r et -R ne font pas la même chose.

Questions fréquentes

Q. Que fait git checkout ?

L'ancienne commande qui change de branche et restaure des fichiers, scindée en switch et restore dans git 2.23 ; checkout -- <fichier> efface définitivement les modifications non commitées.

Q. Comment l’écrire ?

git checkout <branch> | git checkout -b <new> | git checkout -- <file> — les crochets marquent ce qui est facultatif.

Q. Combien d’options valent la peine ?

Il y en a 6 ici ; la liste complète est dans man git. Cette commande relève de git.

Commandes liées

page man: man git-checkout