Cos'e un commit atomico?
Un commit atomico cattura esattamente un cambiamento logico. Compila, supera i propri test, e puo essere revertito in isolamento senza rompere altro.
Il test dell'odore
Se ci si trova a scrivere un messaggio con la parola "e" — "Add login and fix typo and refactor helper" — si stanno guardando tre commit stipati in uno.
Stage selettivo con git add -p
git add -p src/checkout.js
# y - stage this hunk
# n - skip this hunk
# s - split into smaller hunks
# e - manually edit the hunk
Un esempio pratico
git add src/checkout.js
git commit -m "Fix off-by-one in cart total calculation"
git add tests/checkout.test.js
git commit -m "Add regression test for cart total bug"
git add README.md
git commit -m "Fix typo in installation section"
Dividere commit gia fatti
git rebase -i HEAD~3
git reset HEAD^
git add -p
git commit -m "First atomic piece"
git add -p
git commit -m "Second atomic piece"
git rebase --continue
Combinare frammentazione accidentale
Il problema opposto si risolve con squash. Durante il rebase interattivo, marcare i commit come squash o fixup.
Tenere i test con il loro codice
I test per una feature dovrebbero vivere nello stesso commit della feature stessa, cosi git bisect non atterra mai su un commit dove la feature esiste ma i suoi test no.
Commit atomici e bisect
git bisect start
git bisect bad HEAD
git bisect good v2.4.0