Tidak semua file di proyekmu perlu dilacak oleh Git. File .gitignore memberi tahu Git file atau folder mana yang harus diabaikan sepenuhnya.
Ada banyak jenis file yang tidak boleh masuk ke repositori:
node_modules/ ← ribuan file dependensi, bisa diinstal ulang
.env ← berisi API key, password — RAHASIA!
build/ ← hasil build, bisa di-generate ulang
__pycache__/ ← file cache Python
.DS_Store ← file sistem macOS
*.log ← file log aplikasi
*.tmp ← file temporary
Buat file bernama .gitignore (tanpa ekstensi) di root folder proyekmu:
touch .gitignore
Contoh isi .gitignore untuk proyek web:
# Dependensi
node_modules/
bower_components/
# File lingkungan (JANGAN pernah commit ini!)
.env
.env.local
.env.production
# Hasil build
build/
dist/
out/
# File sistem operasi
.DS_Store # macOS
Thumbs.db # Windows
desktop.ini # Windows
# File editor
.vscode/
.idea/
*.swp # Vim swap file
# File log
*.log
logs/
npm-debug.log*
# Coverage test
coverage/
.nyc_output/
# Ini adalah komentar
# Abaikan file dengan nama persis ini
rahasia.txt
# Abaikan semua file .log di mana saja
*.log
# Abaikan folder (slash di akhir = folder)
node_modules/
build/
# Abaikan semua file .txt di dalam folder docs/
docs/*.txt
# Abaikan semua file .pdf di mana pun, di semua subfolder
**/*.pdf
# Pengecualian — JANGAN abaikan file ini (diawali !)
!penting.log
# Abaikan folder di root saja (bukan subfolder)
/logs
# Abaikan file di dalam folder mana pun bernama temp
**/temp
Node.js / JavaScript:
node_modules/
dist/
build/
.env
.env.*
*.log
.DS_Store
Python:
__pycache__/
*.py[cod]
*.egg-info/
venv/
.env
.pytest_cache/
dist/
build/
Laravel / PHP:
vendor/
.env
storage/logs/
public/storage
*.cache
Jika file yang seharusnya di-ignore sudah terlanjur di-commit, menambahkannya ke .gitignore saja tidak cukup. Perlu dihapus dari tracking Git:
# Hapus dari Git tapi file tetap ada di komputermu
git rm --cached nama-file.env
# Hapus folder dari tracking (rekursif)
git rm -r --cached node_modules/
# Kemudian commit perubahan ini
git add .gitignore
git commit -m "Hapus file yang seharusnya di-ignore"
Untuk mengabaikan file di semua repositori di komputermu (seperti .DS_Store):
# Buat file global .gitignore
touch ~/.gitignore_global
# Daftarkan ke konfigurasi Git global
git config --global core.excludesfile ~/.gitignore_global
Isi ~/.gitignore_global:
.DS_Store
.DS_Store?
Thumbs.db
*.swp
.idea/
.vscode/
# Cek apakah file tertentu di-ignore
git check-ignore -v nama-file.txt
# Output: .gitignore:5:*.txt nama-file.txt
# (baris 5 di .gitignore yang menyebabkan file ini di-ignore)
# Lihat semua file yang di-ignore
git status --ignored
💡 Tips: GitHub menyediakan koleksi template
.gitignoreuntuk berbagai bahasa dan framework di github.com/github/gitignore. Saat membuat repositori baru di GitHub, kamu bisa otomatis men-generate.gitignoreyang sesuai. Selalu commit.gitignoredi awal proyek — jangan sampai file sensitif terlanjur masuk ke remote. 🔐
Kursus
Git Dasar
Kategori
Mengelola Perubahan
Durasi Pelajaran
20 menit