Aprender / Cheat sheet
Cheat sheet de Git para testers
Os comandos Git de que um tester precisa para trabalhar em um framework de testes junto com os desenvolvedores: criar branch, commitar, acompanhar a main, desfazer erros sem perder trabalho e encontrar o commit que introduziu um bug. Os comandos que descartam trabalho estão sinalizados.
Conferido com Git 2.40+
Configuração
| Tarefa | Código |
|---|---|
| Sua identidade | git config --global user.name "Mei Lin" git config --global user.email "mei@example.com" |
| Nome da branch padrão | git config --global init.defaultBranch main |
| Clonar um repositório | git clone https://github.com/org/tests.git |
| Pull com rebase por padrão | git config --global pull.rebase true |
Dia a dia
| Tarefa | Código |
|---|---|
| O que mudou? | git status |
| Nova branch a partir de onde você está | git switch -c fix/login-flaky-wait |
| Adicionar arquivos ao stage | git add tests/login.spec.ts |
| Adicionar partes de um arquivo | git add -p |
| Commitar | git commit -m "Wait for login response instead of sleep" |
| Enviar uma branch nova | git push -u origin fix/login-flaky-wait |
| Baixar as últimas mudanças | git pull |
| Trocar de branch | git switch main |
Investigar
| Tarefa | Código |
|---|---|
| Histórico compacto | git log --oneline --graph --decorate -20 |
| Histórico de um arquivo | git log --follow -p -- tests/login.spec.ts |
| Mudanças fora do stage | git diff |
| Mudanças no stage | git diff --staged |
| Sua branch vs main | git diff main...HEAD |
| Um commit | git show 3f2a9c1 |
| Quem alterou estas linhas | git blame -L 20,40 tests/login.spec.ts |
| Todas as branches | git branch -a |
Acompanhar a main
| Tarefa | Código |
|---|---|
| Baixar sem alterar arquivos | git fetch origin |
| Reaplicar seus commits sobre a main | git rebase origin/main |
| Ou fazer merge da main na sua branch | git merge origin/main |
| Depois de resolver um conflito | git add <file> then git rebase --continue |
| Desistir do rebase | git rebase --abort |
| Enviar uma branch rebaseada com segurança | git push --force-with-lease |
| Copiar um commit para esta branch | git cherry-pick 3f2a9c1 |
Só faça rebase de branches que são suas. --force-with-lease se recusa a sobrescrever commits que outra pessoa enviou.
Desfazer
| Situação | Comando |
|---|---|
| Descartar edições em um arquivo (perde as edições) | git restore tests/login.spec.ts |
| Tirar um arquivo do stage, mantendo as edições | git restore --staged tests/login.spec.ts |
| Corrigir a mensagem do último commit ou adicionar um arquivo | git commit --amend |
| Desfazer o último commit, mantendo as mudanças no stage | git reset --soft HEAD~1 |
| Desfazer um commit já enviado (cria um novo commit) | git revert 3f2a9c1 |
| Jogar fora todas as mudanças locais (perde as mudanças) | git reset --hard HEAD |
| Encontrar um commit "perdido" | git reflog |
Em branches compartilhadas, use revert. reset e amend reescrevem o histórico e obrigam todo mundo a corrigir a própria cópia.
Guardar trabalho
| Tarefa | Código |
|---|---|
| Guardar mudanças no stash | git stash push -m "half-done locator refactor" |
| Incluir arquivos não rastreados | git stash push -u |
| Listar stashes | git stash list |
| Reaplicar e descartar o mais recente | git stash pop |
Encontrar o commit que quebrou
| Tarefa | Código |
|---|---|
| Iniciar | git bisect start |
| Marcar o commit atual como ruim | git bisect bad |
| Marcar um commit sabidamente bom | git bisect good v2.3.0 |
| Deixar um teste decidir cada passo | git bisect run npx playwright test tests/checkout.spec.ts |
| Terminar e voltar | git bisect reset |
O bisect run trata o exit code 0 como bom e de 1 a 127 como ruim (125 significa pular este commit).