Login atau daftar akun gratis untuk membaca cheat sheet ini.
Panduan awal untuk mulai menggunakan Git, termasuk konfigurasi dasar dan inisialisasi repository.
Pengaturan Git seperti nama, email, dan editor default untuk lingkungan pengembangan Anda.
# Set nama user
git config --global user.name "Nama Lu"
# Set email
git config --global user.email "email@example.com"
Cara membuat repository Git baru atau meng-clone repository yang sudah ada dari remote.
# Bikin repo baru
git init
# Clone repo yang udah ada
git clone https://github.com/user/repo.git
# Clone ke folder tertentu
git clone https://github.com/user/repo.git my-folder
# Clone branch tertentu
git clone -b develop https://github.com/user/repo.gitCara mendapatkan bantuan dan dokumentasi lengkap untuk perintah Git.
# Minta bantuan
git help
git help commit
git commit --help
# Referensi cepat
git commit -hPerintah-perintah dasar Git yang sering digunakan untuk mengelola file dan commit.
Cara memeriksa status repository, melihat perubahan, dan menampilkan history commit.
# Cek status
git status
# Status singkat
git status -s
# Tampilkan perubahan
git diff
# Tampilkan perubahan yang di-stage
git diff --staged
git diff --cached
# Tampilkan history commit
git log
Cara menambahkan file ke staging area sebelum melakukan commit.
# Tambah file tertentu
git add file.txt
# Tambah banyak file
git add file1.txt file2.txt
# Tambah semua file
git add .
git add --all
git add -A
# Tambah semua file in directory
git add src/
Cara menyimpan perubahan yang sudah di-stage ke dalam history repository.
# Commit dengan pesan
git commit -m "Add new feature"
# Commit semua perubahan yang di-track
git commit -am "Update files"
# Ubah commit terakhir
git commit --amend -m "New message"
# Ubah tanpa ganti pesan
git commit --amend --no-edit
Cara menghapus atau memindahkan file dari repository Git.
# Hapus file
git rm file.txt
# Hapus dari git, tetep di filesystem
git rm --cached file.txt
# Hapus direktori
git rm -r folder/
# Pindah/rename file
git mv old-name.txt new-name.txtCara membatalkan perubahan yang belum di-commit atau reset ke commit sebelumnya.
# Unstage file
git reset file.txt
git restore --staged file.txt
# Buang perubahan di working directory
git checkout -- file.txt
git restore file.txt
# Buang semua perubahan
git reset --hard
# Reset ke commit tertentu (tetep simpan perubahan)
Branching memungkinkan kita mengembangkan fitur secara paralel tanpa mengganggu kode utama.
Cara membuat branch baru dan berpindah antar branch.
# List branch
git branch
git branch -a # Termasuk remote branch
# Bikin branch baru
git branch feature-login
# Pindah ke branch
git checkout feature-login
git switch feature-login
# Bikin dan pindah
git checkout
Cara menggabungkan branch dan menghapus branch yang sudah tidak diperlukan.
# Merge branch ke current
git merge feature-login
# Merge tanpa fast-forward
git merge --no-ff feature-login
# Batalin merge
git merge --abort
# Hapus branch
git branch -d feature-login
# Force delete (ada perubahan yang belum di-merge)
git
Rebasing adalah cara untuk menggabungkan commit dengan cara yang lebih bersih daripada merge.
# Rebase branch sekarang
git rebase main
# Rebase interaktif
git rebase -i HEAD~3
# Lanjut setelah resolve conflict
git rebase --continue
# Skip commit sekarang
git rebase --skip
# Batalin rebase
git rebase --abortRemote repositories memungkinkan kolaborasi dengan tim dan backup kode ke server.
Cara mengelola koneksi ke repository remote seperti GitHub atau GitLab.
# List remote
git remote
git remote -v
# Tambah remote
git remote add origin https://github.com/user/repo.git
# Ganti URL remote
git remote set-url origin https://github.com/user/new-repo.git
# Hapus remote
git remote remove
Cara mengambil perubahan terbaru dari remote repository.
# Fetch dari remote
git fetch origin
# Fetch semua remote
git fetch --all
# Pull perubahan (fetch + merge)
git pull origin main
# Pull dengan rebase
git pull --rebase origin main
# Pull branch tertentu
git pullCara mengirim perubahan lokal ke remote repository.
# Push ke remote
git push origin main
# Push dan set upstream
git push -u origin main
# Push semua branch
git push --all origin
# Push tag
git push --tags
# Force push (hati-hati!)
Stashing memungkinkan menyimpan perubahan sementara tanpa commit, untuk beralih ke branch lain.
Cara menyimpan dan mengambil kembali perubahan yang di-stash.
# Stash perubahan
git stash
# Stash dengan pesan
git stash save "Work in progress"
# Stash termasuk untracked files
git stash -u
# List stash
git stash list
# Tampilkan isi stash
git stash
Tags digunakan untuk menandai versi tertentu dari kode, seperti release version.
Cara membuat tag untuk menandai versi atau milestone penting.
# Tag ringan
git tag v1.0.0
# Tag ber-anotasi
git tag -a v1.0.0 -m "Version 1.0.0"
# Tag commit tertentu
git tag -a v1.0.0 abc123 -m "Release"
# List tag
git tag
git tag
Cara mengelola tag yang sudah dibuat, termasuk push dan delete.
# Push tag ke remote
git push origin v1.0.0
# Push semua tag
git push --tags
# Hapus tag lokal
git tag -d v1.0.0
# Hapus tag remote
git push origin --delete v1.0.0
# Checkout tag
git checkoutCara melihat dan menganalisis history perubahan dalam repository.
Berbagai cara untuk melihat log commit dengan format dan filter yang berbeda.
# Log dasar
git log
# Satu baris per commit
git log --oneline
# Tampilan graph
git log --graph --oneline --all
# Tampilkan patch
git log -p
# Batasi jumlah
git
Cara melihat perbedaan antara file atau commit untuk memahami perubahan yang terjadi.
# Tampilkan perubahan yang belum di-stage
git diff
# Tampilkan perubahan yang di-stage
git diff --staged
# Bandingkan branch
git diff main feature
# Bandingkan commit
git diff abc123 def456
# Tampilkan perubahan for specific file
git diff
Cara melihat detail dari commit tertentu atau file pada commit tertentu.
# Detail commit
git show abc123
# Tampilkan file dari commit
git show abc123:file.txt
# Tampilkan perubahan in commit
git show --stat abc123Perintah-perintah Git tingkat lanjut untuk kasus-kasus khusus.
Cara mengambil commit tertentu dari branch lain dan menerapkannya ke branch saat ini.
# Apply commit ke branch sekarang
git cherry-pick abc123
# Cherry pick banyak commit
git cherry-pick abc123 def456
# Cherry pick tanpa commit
git cherry-pick -n abc123
# Batalin cherry-pick
git cherry-pick --abortCara mencari commit yang menyebabkan bug menggunakan binary search.
# Mulai bisect
git bisect start
# Tandain sekarang sebagai bad
git bisect bad
# Tandain commit sebagai good
git bisect good abc123
# Git will checkout commits for testing
# Test and mark as good/bad
git bisect good
git bisect bad
Cara membersihkan file yang tidak ter-track dari working directory.
# Tampilkan yang bakal dihapus
git clean -n
# Hapus untracked files
git clean -f
# Hapus untracked files and directories
git clean -fd
# Hapus ignored files juga
git clean -fdx
# Clean interaktif
git clean -iReflog menyimpan history dari semua operasi Git, berguna untuk recover commit yang hilang.
# Tampilkan reflog
git reflog
# Tampilkan reflog for branch
git reflog show main
# Recover commit yang hilang
git checkout abc123
# Recover branch yang dihapus
git checkout -b recovered-branch abc123Submodules memungkinkan menyertakan repository Git lain sebagai subdirektori.
Cara menambahkan dan mengelola submodule dalam repository.
# Tambah submodule
git submodule add https://github.com/user/repo.git path/to/submodule
# Inisialisasi submodule
git submodule init
# Update submodule
git submodule update
# Clone dengan submodule
git clone --recurse-submodules https://github.com/user/repo.git
# Update semua submodule
Workflow adalah pola kerja tim yang menggunakan Git untuk kolaborasi yang efektif.
Workflow dimana setiap fitur dikembangkan di branch terpisah.
# 1. Create feature branch
git checkout -b feature-login
# 2. Work on feature
git add .
git commit -m "Add login form"
# 3. Keep updated with main
git checkout main
git pull origin main
Workflow untuk memperbaiki bug kritis di production dengan cepat.
# 1. Create hotfix from main
git checkout main
git checkout -b hotfix-bug
# 2. Fix bug
git add .
git commit -m "Fix critical bug"
# 3. Merge to main
git checkout main
git
Workflow komprehensif dengan branch main, develop, feature, release, dan hotfix.
# Main branches: main, develop
# Start new feature
git checkout develop
git checkout -b feature/new-feature
# Finish feature
git checkout develop
git merge feature/new-feature
git branch -d feature/new-feature
Alias mempersingkat perintah Git yang sering digunakan untuk meningkatkan produktivitas.
Cara mengatur alias Git untuk perintah yang sering dipakai.
# Add to ~/.gitconfig or use git config
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config
Contoh alias yang berguna untuk operasi Git sehari-hari.
# After setup, use like:
git co main # checkout main
git br # list branches
git ci -m "message" # commit
git st # status
git unstage file.txt # unstage file
git last # show last commit
git
Solusi untuk masalah-masalah umum yang sering terjadi saat menggunakan Git.
Cara mengatasi masalah-masalah umum seperti undo changes dan resolve conflicts.
# Undo last commit (tetep simpan perubahan)
git reset --soft HEAD~1
# Undo last commit (buang perubahan)
git reset --hard HEAD~1
# Recover deleted file
git checkout HEAD -- file.txt
# Resolve merge conflicts
# 1. Edit files to resolve conflicts
Praktik aman untuk menghindari kehilangan data saat menggunakan Git.
# Always pull before push
git pull origin main
git push origin main
# Check before force push
git push --force-with-lease
# Create backup branch
git branch backup-main
# Work on copy
git checkout -b experiment
Panduan praktik terbaik untuk menggunakan Git secara efektif dan aman.
Format dan panduan untuk menulis commit message yang baik.
# Good commit message format:
# <type>: <subject>
#
# <body>
#
# <footer>
# Examples:
git commit -m "feat: add user authentication"
git commit -m "fix: resolve login bug"
git commit -m "docs: update README"
git commit -m
Cara mengatur file yang tidak ingin di-track oleh Git menggunakan .gitignore.
# Create .gitignore file
touch .gitignore
# Common patterns:
# node_modules/
# .env
# *.log
# dist/
# build/
# .DS_Store
# *.swp
# Ignore globally
git config --global core.excludesfile ~/.gitignore_globalCara menjaga keamanan repository dengan menghapus file sensitif dari history.
# Remove sensitive file from history
git filter-branch --force --index-filter \
"git rm --cached --ignore-unmatch .env" \
--prune-empty --tag-name-filter cat -- --all
# Better way: use BFG Repo-Cleaner
# Download from: https://rtyley.github.io/bfg-repo-cleaner/
# Hapus file
bfg --delete-files .env
# Replace passwords