Commit Conventions & Branch Rules + Quiz 2.2
Tujuan Pembelajaran
- Menguasai format Conventional Commits
- Memahami branch protection rules
- Mengevaluasi pemahaman modul 2.2
Materi Inti
A. Conventional Commits
Conventional Commits adalah standar penulisan pesan commit yang konsisten dan terstruktur. Format ini memudahkan pembacaan history dan otomatisasi (seperti changelog generation).
Format:
<tipe>[scope opsional]: <deskripsi>
[body opsional]
[footer opsional]
Tipe-tipe commit:
| Tipe | Kapan Digunakan | Contoh |
|---|---|---|
feat | Fitur baru | feat: tambah halaman login |
fix | Perbaikan bug | fix: perbaiki tombol submit yang tidak responsif |
docs | Dokumentasi | docs: update README dengan panduan instalasi |
style | Formatting (tanpa ubah logika) | style: rapikan indentasi di file CSS |
refactor | Refactoring kode | refactor: pisahkan fungsi validasi dari controller |
test | Tambah/perbaiki test | test: tambah unit test untuk fungsi kalkulasi |
chore | Maintenance | chore: update dependency ke versi terbaru |
Contoh pesan commit lengkap:
feat(auth): tambah fitur reset password
Menambahkan halaman reset password yang mengirim
email verifikasi ke user. Menggunakan library
nodemailer untuk pengiriman email.
Closes #42
Tips: Gunakan bahasa yang konsisten di seluruh project. Boleh pakai bahasa Indonesia atau Inggris, tapi jangan dicampur.
B. Konvensi Penamaan Branch
Selain commit, nama branch juga perlu konsisten:
<tipe>/<deskripsi-singkat>
| Prefix | Contoh | Fungsi |
|---|---|---|
feature/ | feature/halaman-login | Fitur baru |
bugfix/ | bugfix/perbaiki-header | Perbaikan bug |
hotfix/ | hotfix/security-patch | Perbaikan critical |
docs/ | docs/update-readme | Dokumentasi |
refactor/ | refactor/optimasi-query | Refactoring |
Warning: Hindari nama branch yang ambigu seperti
fix,update, ataunew-branch. Nama branch harus menjelaskan APA yang dikerjakan.
C. Branch Protection Rules
Branch protection rules mencegah perubahan langsung ke branch penting (seperti main atau develop) tanpa melalui proses review.
Cara mengaktifkan di GitHub:
- Buka repository di GitHub
- Settings lalu Branches
- Klik βAdd branch protection ruleβ
- Branch name pattern:
main - Centang aturan yang diinginkan
Aturan umum yang disarankan:
| Aturan | Fungsi |
|---|---|
| Require pull request before merging | Tidak bisa push langsung ke main |
| Require approvals | Minimal 1 orang harus approve PR |
| Require status checks | CI/CD harus pass sebelum merge |
| Require conversation resolution | Semua komentar review harus resolved |
| Do not allow bypassing | Aturan berlaku untuk semua, termasuk admin |
Info: Branch protection rules adalah salah satu cara terpenting untuk menjaga kualitas kode di tim. Dengan aturan ini, setiap perubahan di-review sebelum masuk ke production.
Demonstrasi Live
Setup Conventional Commits di Project
mkdir proyek-konvensi && cd proyek-konvensi && git init
# Commit awal
echo "# Proyek Konvensi" > README.md
git add . && git commit -m "init: setup project awal"
# Fitur baru
git switch -c feature/halaman-profil
echo "<h1>Profil</h1>" > profil.html
git add . && git commit -m "feat(profil): tambah halaman profil user"
echo "<form>Nama: <input></form>" >> profil.html
git add . && git commit -m "feat(profil): tambah form edit profil"
git switch main && git merge feature/halaman-profil
git branch -d feature/halaman-profil
# Perbaikan bug
git switch -c bugfix/typo-readme
echo "# Proyek Konvensi - Tim Hebat" > README.md
git add . && git commit -m "fix(docs): perbaiki typo di README"
git switch main && git merge bugfix/typo-readme
git branch -d bugfix/typo-readme
# Dokumentasi
git switch -c docs/panduan-instalasi
echo "## Instalasi\n1. Clone repo\n2. npm install" >> README.md
git add . && git commit -m "docs: tambah panduan instalasi di README"
git switch main && git merge docs/panduan-instalasi
git branch -d docs/panduan-instalasi
# Lihat history yang rapi dan konsisten
git log --oneline
Perhatikan bahwa setiap commit memiliki tipe yang jelas, sehingga mudah dipahami apa yang berubah.
Latihan Interaktif
Latihan Terminal: Praktik Conventional Commits
Buat project dengan minimal 10 commit menggunakan berbagai tipe Conventional Commits:
mkdir latihan-konvensi && cd latihan-konvensi && git init
# init
echo "# Toko Buku" > README.md
git add . && git commit -m "init: setup project toko buku"
# feat
git switch -c feature/katalog
echo "<div>Katalog Buku</div>" > katalog.html
git add . && git commit -m "feat(katalog): tambah halaman katalog"
git switch main && git merge feature/katalog && git branch -d feature/katalog
# feat lagi
git switch -c feature/keranjang
echo "<div>Keranjang</div>" > keranjang.html
git add . && git commit -m "feat(cart): tambah halaman keranjang belanja"
git switch main && git merge feature/keranjang && git branch -d feature/keranjang
# fix
git switch -c bugfix/typo
echo "<div>Katalog Buku Online</div>" > katalog.html
git add . && git commit -m "fix(katalog): perbaiki typo di judul katalog"
git switch main && git merge bugfix/typo && git branch -d bugfix/typo
# docs
echo "## Cara Pakai\nBuka index.html di browser" >> README.md
git add . && git commit -m "docs: tambah panduan penggunaan"
# style
echo "body { font-family: Arial; }" > style.css
git add . && git commit -m "style: tambah CSS dasar untuk font"
# refactor
echo "<main><div>Katalog Buku Online</div></main>" > katalog.html
git add . && git commit -m "refactor(katalog): bungkus konten dengan tag main"
# chore
echo "node_modules/\n.env\n.DS_Store" > .gitignore
git add . && git commit -m "chore: tambah file .gitignore"
# Lihat history
git log --oneline
Quiz Modul 2.2
Quiz ini menguji pemahaman tentang branching strategy, Conventional Commits, dan branch protection rules.
Passing grade: 70% (minimal 6 dari 8 benar)
Topik yang Diuji
| No | Topik | Jumlah Soal |
|---|---|---|
| 1 | Git Flow | 2 soal |
| 2 | GitHub Flow | 1 soal |
| 3 | Trunk-Based Development | 1 soal |
| 4 | Conventional Commits | 2 soal |
| 5 | Branch naming & protection | 2 soal |
Materi yang Perlu Dikuasai
Git Flow:
- Branch
developdigunakan sebagai tempat integrasi fitur - Git Flow tidak cocok untuk tim kecil yang deploy setiap hari
- Branch
mainhanya berisi kode production yang stabil
GitHub Flow:
- Paling cocok untuk tim yang melakukan continuous deployment
- Hanya ada
main+ feature branches + Pull Requests - Main harus selalu dalam kondisi deployable
Trunk-Based Development:
- Menggantikan feature branch dengan feature flags
- Semua commit langsung ke main
- Membutuhkan CI/CD yang matang
Conventional Commits:
- Format:
<tipe>[scope]: <deskripsi> featuntuk fitur baru,fixuntuk bug,refactoruntuk perubahan strukturrefactorberarti mengubah struktur kode tanpa mengubah perilaku
Branch Protection:
- Mencegah push langsung ke branch penting tanpa PR dan review
- Penamaan branch yang baik menggunakan prefix:
feature/,bugfix/,hotfix/
Tugas Mandiri
- Terapkan Conventional Commits di semua commit selanjutnya pada project kamu
- Buat 1 repo dengan branch protection rule di branch
main(require pull request + require approval) - Praktikkan workflow lengkap: buat branch dengan nama konvensi, commit dengan format conventional, buat PR, merge
Quiz: Review Modul 2.2
Jawab 8 pertanyaan berikut untuk menguji pemahaman kamu.
Pada Git Flow, fitur baru diintegrasikan ke branch mana terlebih dahulu?