Referensi cepat Git commands. Dari basic commit sampe advanced branching. Must-have buat developer yang kerja tim.
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"
# Set editor default
git config --global core.editor "code"
# Cek config
git config --list
git config user.name
# Level config
git config --global # Level user
git config --local # Level repository
git config --system # Level systemCara 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
# Log satu baris
git log --oneline
# Log berbentuk graph
git log --graph --oneline --all
# Tampilkan history file tertentu
git log -- file.txt
# Tampilkan siapa ngubah apa
git blame file.txtCara 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/
# Tambah pake pattern
git add *.js
# Add interaktif
git add -pCara 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
# Commit kosong (berguna buat CI)
git commit --allow-empty -m "Trigger CI"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)
git reset --soft HEAD~1
# Reset ke commit tertentu (buang perubahan)
git reset --hard HEAD~1
# Revert commit (bikin commit baru)
git revert abc123Branching 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 -b feature-login
git switch -c feature-login
# Bikin dari commit tertentu
git checkout -b feature abc123Cara 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 branch -D feature-login
# Hapus remote branch
git push origin --delete feature-loginRebasing 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 origin
# Rename remote
git remote rename origin upstream
# Info remote
git remote show originCara 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 pull origin developCara 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!)
git push --force origin main
# Force push versi lebih aman
git push --force-with-lease origin main
# Hapus remote branch
git push origin --delete feature-oldStashing 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 show
git stash show -p
# Apply stash terakhir
git stash apply
# Apply stash tertentu
git stash apply stash@{2}
# Pop stash (apply + drop)
git stash pop
# Drop stash
git stash drop
git stash drop stash@{2}
# Clear semua stash
git stash clearTags 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 -l "v1.*"
# Detail tag
git show v1.0.0Cara 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 checkout v1.0.0Cara 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 log -5
# Berdasarkan author
git log --author="John"
# Berdasarkan tanggal
git log --since="2 weeks ago"
git log --after="2024-01-01"
git log --before="2024-12-31"
# Berdasarkan pesan
git log --grep="fix"
# Tampilkan stats
git log --stat
# Format custom
git log --pretty=format:"%h - %an, %ar : %s"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 file.txt
# Tampilkan perubahan between dates
git diff '@{1 month ago}' '@{yesterday}'
# Tampilkan diff level kata
git diff --word-diffCara 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
# Selesai bisect
git bisect resetCara 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
git submodule update --remoteWorkflow 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
git checkout feature-login
git rebase main
# 4. Push feature
git push -u origin feature-login
# 5. Merge to main (after PR approved)
git checkout main
git merge feature-login
git push origin main
# 6. Delete feature branch
git branch -d feature-login
git push origin --delete feature-loginWorkflow 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 merge hotfix-bug
git push origin main
# 4. Tag release
git tag -a v1.0.1 -m "Hotfix release"
git push --tags
# 5. Merge to develop too
git checkout develop
git merge hotfix-bug
git push origin develop
# 6. Delete hotfix branch
git branch -d hotfix-bugWorkflow 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
# Start release
git checkout develop
git checkout -b release/1.0.0
# Finish release
git checkout main
git merge release/1.0.0
git tag -a v1.0.0
git checkout develop
git merge release/1.0.0
git branch -d release/1.0.0Alias 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 --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.visual 'log --graph --oneline --all'
git config --global alias.amend 'commit --amend --no-edit'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 visual # pretty log
git amend # amend without changing messageSolusi 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
# 2. Mark as resolved
git add file.txt
# 3. Continue merge
git commit
# Batalin merge
git merge --abort
# Discard all local changes
git reset --hard origin/main
# Hapus file from git but keep locally
git rm --cached file.txt
# Change last commit message
git commit --amend -m "New message"
# Undo git add
git reset file.txtPraktik 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
# Use stash instead of commit
git stash
# Test before cleaning
git clean -nPanduan 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 "refactor: simplify user service"
git commit -m "test: add user model tests"
git commit -m "chore: update dependencies"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
bfg --replace-text passwords.txtLogin atau daftar akun gratis untuk membaca cheat sheet ini.