git checkout
git
Uso
git checkout <branch> | git checkout -b <new> | git checkout -- <file>O comando antigo que troca de branch e restaura arquivos, dividido em switch e restore no git 2.23; checkout -- <arquivo> apaga de vez as edições não commitadas.
Opções comuns
| Opção | Significado |
|---|---|
| -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 |
| --detach | sit on a commit with no branch attached |
| -f | throw away local changes to switch anyway |
Exemplos
git checkout -b feature/loginbranches off the current commit and moves there
git checkout mainswitches branch, same as git switch main
git checkout -- src/app.tsdiscards uncommitted edits to that file; they are gone
O confuso é que desfazer tem várias formas: mover para onde um branch aponta não é o mesmo que criar um commit que reverte.
Como ler
- Os colchetes [ ] marcam uma parte que você pode omitir.
- As reticências … indicam que você pode listar vários.
- As opções diferenciam maiúsculas: em alguns comandos -r e -R fazem coisas diferentes.
Perguntas
Q. O que git checkout faz?
O comando antigo que troca de branch e restaura arquivos, dividido em switch e restore no git 2.23; checkout -- <arquivo> apaga de vez as edições não commitadas.
Q. Como se digita?
git checkout <branch> | git checkout -b <new> | git checkout -- <file> — os colchetes marcam o que você pode omitir.
Q. Quantas opções vale a pena saber?
Aqui estão 6; a lista completa está em man git. Este comando fica em git.
Comandos relacionados
página man: man git-checkout