tutorial, git,

Panduan Lengkap .gitignore: File Mana yang Tidak Perlu Di-commit

Kiki/🎮🍉⌨️🍩💻 Kiki/🎮🍉⌨️🍩💻 Sep 22, 2026 · 4 mins read
Panduan Lengkap .gitignore: File Mana yang Tidak Perlu Di-commit
Share this

Tidak semua file di project perlu di-commit ke repository. File hasil kompilasi, dependency yang bisa diinstall ulang, credential sensitif, file log, dan cache tidak perlu (bahkan tidak boleh) masuk ke Git. .gitignore adalah cara kamu memberitahu Git untuk mengabaikan file-file tersebut.


Apa itu .gitignore?

.gitignore adalah file teks yang berisi daftar pola file dan direktori yang tidak akan dilacak oleh Git. File ini harus di-commit ke repository agar berlaku untuk semua anggota tim.


Sintaks .gitignore

# Ini adalah komentar

# File spesifik
secret.env
passwords.txt

# Ekstensi file
*.log
*.tmp
*.pyc
*.class

# Folder (dengan slash di akhir)
node_modules/
dist/
build/
.cache/
__pycache__/

# Folder di level manapun
**/logs/
**/*.log

# File di folder tertentu saja
/config/local.json    # hanya di root
docs/*.pdf            # semua PDF di folder docs

# Negasi — jangan abaikan file ini meskipun cocok pola di atas
!important.log
*.log
!error.log    # error.log tetap dilacak meskipun *.log diabaikan

# Wildcard
*.swp         # file swap vim
.DS_Store     # file macOS
Thumbs.db     # file Windows thumbnail

Template .gitignore Berdasarkan Stack

Node.js / JavaScript

# Dependencies
node_modules/
.pnp
.pnp.js

# Build output
dist/
build/
out/
.next/
.nuxt/

# Environment variables (PENTING!)
.env
.env.local
.env.development.local
.env.test.local
.env.production.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
*.log

# Runtime
.node_repl_history

# Cache
.eslintcache
.stylelintcache
.cache/
.parcel-cache/
*.tsbuildinfo

# Testing
coverage/
.nyc_output/

# Editor
.vscode/settings.json
.idea/
*.swp
*.swo
.DS_Store
Thumbs.db

Python

# Bytecode
__pycache__/
*.py[cod]
*$py.class
*.pyc

# Virtual environments
venv/
env/
.venv/
ENV/

# Distribution
dist/
build/
*.egg-info/
*.egg

# Environment
.env

# Testing
.pytest_cache/
.coverage
htmlcov/

# Type checking
.mypy_cache/
.dmypy.json
dmypy.json

# Jupyter
.ipynb_checkpoints/

PHP / Laravel

/vendor/
/node_modules/

# Laravel
.env
.env.backup
.phpunit.result.cache
storage/*.key
public/hot
public/storage

# Composer
composer.phar
/composer.lock   # opsional — beberapa tim commit ini

# Cache
bootstrap/cache/*.php

Docker

# Jangan commit data volume lokal
postgres_data/
mysql_data/
redis_data/

# File env production
.env.production
docker-compose.override.yml

Tiga Level .gitignore

1. Repository Level (Paling Umum)

File .gitignore di root repository — berlaku untuk semua kontributor:

# File ini di-commit ke repo
/path/ke/proyek/.gitignore

2. Global Level — Berlaku di Semua Repository Kamu

Untuk file yang spesifik ke environment kamu (editor, OS):

# Buat global gitignore
git config --global core.excludesFile ~/.gitignore_global

# Isi dengan file editor/OS kamu
cat ~/.gitignore_global
# macOS
.DS_Store
.AppleDouble
.LSOverride

# Windows
Thumbs.db
ehthumbs.db
Desktop.ini

# Linux
*~

# VS Code
.vscode/
*.code-workspace

# JetBrains (IntelliJ, WebStorm, PyCharm)
.idea/
*.iml

# Vim
*.swp
*.swo
Session.vim

# Emacs
*~
\#*\#

3. Local Level — Tidak Di-commit

Untuk pengecualian yang sangat personal, tersimpan di .git/info/exclude:

echo "my-notes.txt" >> .git/info/exclude

.gitignore Tidak Berlaku untuk File yang Sudah Di-track

Ini adalah jebakan yang sangat umum! Jika file sudah pernah di-commit, menambahkannya ke .gitignore tidak akan menghentikan Git dari melacak perubahannya.

# Hapus file dari tracking Git (tapi biarkan file tetap ada di disk)
git rm --cached nama-file.env
git rm --cached -r node_modules/

# Setelah itu commit perubahan
git commit -m "chore: hapus file sensitif dari tracking Git"

Cek Apakah File Di-ignore

# Cek apakah file tertentu di-ignore
git check-ignore -v nama-file.env
# .gitignore:3:.env   nama-file.env

# Lihat semua file yang di-ignore
git status --ignored

Generate .gitignore Otomatis

Gunakan layanan gitignore.io untuk generate template berdasarkan stack:

# Dengan curl
curl -o .gitignore https://www.toptal.com/developers/gitignore/api/node,react,vscode

# Dengan gh CLI
gh api /gitignore/templates/Node > .gitignore

Atau dari VS Code: install extension gitignore lalu tekan Ctrl+Shift+P → “Add gitignore”.


Kesimpulan

.gitignore yang baik adalah baris pertahanan pertama untuk menjaga repository tetap bersih dari file yang tidak perlu — dan yang lebih penting, mencegah credential sensitif tidak sengaja di-push ke GitHub. Selalu buat .gitignore di awal project sebelum commit pertama, bukan setelahnya.

Di artikel berikutnya, kita mulai seri kolaborasi dengan membahas Git Workflow untuk Tim — GitHub Flow, Git Flow, dan cara memilih workflow yang tepat.

Kiki/🎮🍉⌨️🍩💻
Written by Kiki/🎮🍉⌨️🍩💻
hello I'm friend K.