Cheatsheet Git
Ringkasan perintah penting untuk bekerja dengan Git. Salin sesuai kebutuhan.
Setup & Konfigurasi
git config --global user.name "Nama Anda"
git config --global user.email "[email protected]"
git config --global init.defaultBranch main
git config --global core.editor "code --wait" # editor VS Code
# Inisialisasi atau klon repo
git init
git clone <URL>
Snapshot (Status, Add, Commit)
# Lihat perubahan
git status
git diff # perbedaan working tree
git diff --staged # perbedaan staged (index)
# Stage dan commit
git add <file>
git add .
git commit -m "pesan commit"
git commit --amend --no-edit # ubah commit terakhir (hindari jika sudah dipush)
# Batalkan perubahan file
git restore --source=HEAD -- <file> # kembalikan file ke HEAD
git restore --staged <file> # unstage
Branch & Merge
# Branch dasar
git branch # daftar branch lokal
git branch -a # semua branch (termasuk remote)
git switch -c feature/xyz # buat dan pindah branch
git switch main # pindah branch
# Merge
git merge feature/xyz # merge ke branch aktif
git branch -d feature/xyz # hapus branch (sudah di-merge)
git branch -D feature/xyz # paksa hapus
Remote, Fetch, Pull, Push
# Remote origin
git remote add origin <URL>
git remote -v
git remote set-url origin <URL-baru>
# Sinkronisasi
git fetch origin
git pull --rebase origin main
git push -u origin main
Stash
git stash push -m "wip: deskripsi"
git stash list
git stash show -p stash@{0}
git stash pop # ambil + hapus dari daftar
git stash apply # ambil tanpa menghapus
git stash drop stash@{0}
git stash clear
Rewriting (Reset, Revert, Rebase)
# Reset (hati-hati)
git reset --soft HEAD~1 # geser HEAD, simpan perubahan di index
git reset --mixed HEAD~1 # default, simpan perubahan di working tree
git reset --hard HEAD~1 # hapus perubahan (irreversible)
# Revert (membuat commit pembalik)
git revert <hash>
# Rebase (rapikan riwayat)
git rebase -i HEAD~5
Log & Diff
# Log berbagai format
git log --oneline --graph --decorate --all
git log --since="2 weeks ago" -- <path>
git show <hash>
git blame <file>
git diff <hash1>..<hash2>
Advanced
# Cherry-pick
git cherry-pick <hash>
# Bisect (cari commit penyebab bug)
git bisect start
git bisect bad
git bisect good <hash_terakhir_bagus>
# ... tandai good/bad sampai ketemu
git bisect reset
# Submodule
git submodule add <URL> path/submodule
git submodule update --init --recursive
.gitignore (contoh pola)
# Abaikan folder build & dependency
node_modules/
dist/
build/
# File sementara/editor
*.log
*.tmp
.DS_Store
.idea/
.vscode/
# Lingkungan
.env
.env.local