Impara / Cheat sheet
Cheat sheet Git per tester
I comandi Git che servono a un tester per lavorare su un framework di test insieme agli sviluppatori: creare branch, fare commit, restare allineati a main, rimediare agli errori senza perdere lavoro e trovare il commit che ha introdotto un bug. I comandi che scartano modifiche sono segnalati.
Verificato su Git 2.40+
Configurazione
| Attività | Codice |
|---|---|
| La tua identità | git config --global user.name "Mei Lin" git config --global user.email "mei@example.com" |
| Nome del branch predefinito | git config --global init.defaultBranch main |
| Clona un repository | git clone https://github.com/org/tests.git |
| Pull con rebase di default | git config --global pull.rebase true |
Lavoro quotidiano
| Attività | Codice |
|---|---|
| Cosa è cambiato? | git status |
| Nuovo branch dal punto in cui sei | git switch -c fix/login-flaky-wait |
| Aggiungi file all’area di stage | git add tests/login.spec.ts |
| Aggiungi parti di un file | git add -p |
| Commit | git commit -m "Wait for login response instead of sleep" |
| Pubblica un nuovo branch | git push -u origin fix/login-flaky-wait |
| Scarica le ultime modifiche | git pull |
| Cambia branch | git switch main |
Esplorare
| Attività | Codice |
|---|---|
| Cronologia compatta | git log --oneline --graph --decorate -20 |
| Cronologia di un file | git log --follow -p -- tests/login.spec.ts |
| Modifiche non in stage | git diff |
| Modifiche in stage | git diff --staged |
| Il tuo branch rispetto a main | git diff main...HEAD |
| Un singolo commit | git show 3f2a9c1 |
| Chi ha modificato queste righe | git blame -L 20,40 tests/login.spec.ts |
| Tutti i branch | git branch -a |
Restare allineati a main
| Attività | Codice |
|---|---|
| Scarica senza modificare i file | git fetch origin |
| Riapplica i tuoi commit sopra main | git rebase origin/main |
| Oppure fai il merge di main nel tuo branch | git merge origin/main |
| Dopo aver risolto un conflitto | git add <file> then git rebase --continue |
| Abbandona il rebase | git rebase --abort |
| Pubblica in sicurezza un branch ribasato | git push --force-with-lease |
| Copia un commit su questo branch | git cherry-pick 3f2a9c1 |
Fai rebase solo dei branch che sono tuoi. --force-with-lease si rifiuta di sovrascrivere commit pubblicati da altri.
Annullare
| Situazione | Comando |
|---|---|
| Scarta le modifiche a un file (le perdi) | git restore tests/login.spec.ts |
| Togli un file dallo stage, mantieni le modifiche | git restore --staged tests/login.spec.ts |
| Correggi il messaggio dell’ultimo commit o aggiungi un file | git commit --amend |
| Annulla l’ultimo commit, mantieni le modifiche in stage | git reset --soft HEAD~1 |
| Annulla un commit già pubblicato (aggiunge un nuovo commit) | git revert 3f2a9c1 |
| Butta via tutte le modifiche locali (le perdi) | git reset --hard HEAD |
| Ritrova un commit "perso" | git reflog |
Sui branch condivisi usa revert. reset e amend riscrivono la cronologia e costringono tutti gli altri a sistemare la propria copia.
Mettere da parte il lavoro
| Attività | Codice |
|---|---|
| Metti da parte le modifiche (stash) | git stash push -m "half-done locator refactor" |
| Includi i file non tracciati | git stash push -u |
| Elenca gli stash | git stash list |
| Riapplica e rimuovi l’ultimo | git stash pop |
Trovare il commit che ha rotto tutto
| Attività | Codice |
|---|---|
| Avvio | git bisect start |
| Segna il commit corrente come cattivo | git bisect bad |
| Segna un commit noto come buono | git bisect good v2.3.0 |
| Lascia decidere a un test ogni passo | git bisect run npx playwright test tests/checkout.spec.ts |
| Termina e torna indietro | git bisect reset |
bisect run considera buono l’exit code 0 e cattivi quelli da 1 a 127 (125 significa salta questo commit).