📎 Webclip
Why Use git switch Instead of git checkout?
git switch is presented as a newer command in Git 2.23 that makes branch changes more direct and easier to understand. The page contrasts it with git checkout, which can also handle file operations, and says that separation reduces mistakes when working across branches.
The article also shows common uses such as switching to an existing branch, creating a new one with -c, moving to a commit in detached HEAD with --detach, and forcing or discarding changes when local modifications block the switch. It closes by recommending thoughtful branch changes, git stash, descriptive branch names, and keeping Git updated.
Fichamento#
git switchfoi introduzido no Git 2.23 para trocar de branch de forma mais simples e focada.git checkoutacumula troca de branch, operação em arquivos e criação de branch, o que pode gerar confusão.- A separação de responsabilidades em
git switchtorna o fluxo mais intuitivo e reduz erros na gestão de branches. - A sintaxe básica é
git switch [options] <branch>. -c <branch>cria uma nova branch e já muda para ela.--detachleva para um commit específico sem associar a navegação a uma branch.-fforça a troca e descarta mudanças não commitadas.--discard-changestambém descarta modificações locais ao trocar de branch.- Ao usar
git switchem uma branch existente, as mudanças não commitadas são preservadas se não entrarem em conflito. - Um fluxo prático descrito usa
git stash,git switch main, correção do bug e depois retorno parafeature-branchcomgit stash pop. - A comparação final destaca que
git switchcobre troca de branch, criação de branch e detached HEAD, mas não operações em arquivos. - Os problemas comuns citados são mudanças locais que impedem a troca e branch inexistente.
- As boas práticas listadas são conferir a branch antes de alterar algo, combinar com
git stash, usar nomes descritivos e manter o Git atualizado.
