BelajarKoding Logobelajarkoding

Platform belajar web development Indonesia. Artikel, cheat sheets, roadmap, dan code challenges untuk developer Indonesia.

Navigasi

  • Artikel
  • Cheat Sheets
  • Roadmap
  • Challenges
  • Pricing
  • Search

Produk Lain

  • JagoHermes
  • KelasClaude
  • KilatKoding
  • BelajarVibeCoding
  • JualanKoding

Support

  • Privacy Policy
  • Terms of Service
  • Email

© 2026 BelajarKoding. All rights reserved.

Galih PratamaBagian dari ekosistem Galih Pratama
belajarkoding LogobyGalih Pratama
RoadmapArtikelCheat SheetsChallengesUpgrade
belajarkoding LogobyGalih Pratama
RoadmapArtikelCheat SheetsChallengesUpgrade
belajarkoding LogobyGalih Pratama
RoadmapArtikelCheat SheetsChallengesUpgrade

Daftar Isi

Getting StartedConfigurationInitialize RepositoryHelpBasic CommandsStatus & InfoAdding FilesCommittingRemoving & MovingUndoing ChangesBranchingCreating & SwitchingMerging & DeletingRebasingRemote RepositoriesManaging RemotesFetching & PullingPushingStashingSave & ApplyTagsCreating TagsManaging TagsViewing HistoryLog CommandsDiff CommandsShow CommandAdvanced CommandsCherry PickBisectCleanReflogWorking with SubmodulesAdding SubmodulesWorkflowsFeature Branch WorkflowHotfix WorkflowGitflow WorkflowUseful AliasesSetup AliasesCommon AliasesTroubleshootingCommon IssuesSafe OperationsTips & Best PracticesCommit MessagesIgnore FilesSecurity
GitDevOps

Git Cheat Sheet

Referensi cepat Git commands. Dari basic commit sampe advanced branching. Must-have buat developer yang kerja tim.

Git13 min read2.460 kata
Silakan login atau daftar untuk membaca cheat sheet ini.

#Getting Started

Panduan awal untuk mulai menggunakan Git, termasuk konfigurasi dasar dan inisialisasi repository.

#Configuration

Pengaturan Git seperti nama, email, dan editor default untuk lingkungan pengembangan Anda.

bash
# 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 system

#Initialize Repository

Cara membuat repository Git baru atau meng-clone repository yang sudah ada dari remote.

bash
# 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.git

#Help

Cara mendapatkan bantuan dan dokumentasi lengkap untuk perintah Git.

bash
# Minta bantuan
git help
git help commit
git commit --help
 
# Referensi cepat
git commit -h

#Basic Commands

Perintah-perintah dasar Git yang sering digunakan untuk mengelola file dan commit.

#Status & Info

Cara memeriksa status repository, melihat perubahan, dan menampilkan history commit.

bash
# 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.txt

#Adding Files

Cara menambahkan file ke staging area sebelum melakukan commit.

bash
# 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 -p

#Committing

Cara menyimpan perubahan yang sudah di-stage ke dalam history repository.

bash
# 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"

#Removing & Moving

Cara menghapus atau memindahkan file dari repository Git.

bash
# 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.txt

#Undoing Changes

Cara membatalkan perubahan yang belum di-commit atau reset ke commit sebelumnya.

bash
# 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 abc123

#Branching

Branching memungkinkan kita mengembangkan fitur secara paralel tanpa mengganggu kode utama.

#Creating & Switching

Cara membuat branch baru dan berpindah antar branch.

bash
# 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 abc123

#Merging & Deleting

Cara menggabungkan branch dan menghapus branch yang sudah tidak diperlukan.

bash
# 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-login

#Rebasing

Rebasing adalah cara untuk menggabungkan commit dengan cara yang lebih bersih daripada merge.

bash
# 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 --abort

#Remote Repositories

Remote repositories memungkinkan kolaborasi dengan tim dan backup kode ke server.

#Managing Remotes

Cara mengelola koneksi ke repository remote seperti GitHub atau GitLab.

bash
# 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 origin

#Fetching & Pulling

Cara mengambil perubahan terbaru dari remote repository.

bash
# 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 develop

#Pushing

Cara mengirim perubahan lokal ke remote repository.

bash
# 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-old

#Stashing

Stashing memungkinkan menyimpan perubahan sementara tanpa commit, untuk beralih ke branch lain.

#Save & Apply

Cara menyimpan dan mengambil kembali perubahan yang di-stash.

bash
# 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 clear

#Tags

Tags digunakan untuk menandai versi tertentu dari kode, seperti release version.

#Creating Tags

Cara membuat tag untuk menandai versi atau milestone penting.

bash
# 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.0

#Managing Tags

Cara mengelola tag yang sudah dibuat, termasuk push dan delete.

bash
# 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.0

#Viewing History

Cara melihat dan menganalisis history perubahan dalam repository.

#Log Commands

Berbagai cara untuk melihat log commit dengan format dan filter yang berbeda.

bash
# 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"

#Diff Commands

Cara melihat perbedaan antara file atau commit untuk memahami perubahan yang terjadi.

bash
# 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-diff

#Show Command

Cara melihat detail dari commit tertentu atau file pada commit tertentu.

bash
# Detail commit
git show abc123
 
# Tampilkan file dari commit
git show abc123:file.txt
 
# Tampilkan perubahan in commit
git show --stat abc123

#Advanced Commands

Perintah-perintah Git tingkat lanjut untuk kasus-kasus khusus.

#Cherry Pick

Cara mengambil commit tertentu dari branch lain dan menerapkannya ke branch saat ini.

bash
# 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 --abort

#Bisect

Cara mencari commit yang menyebabkan bug menggunakan binary search.

bash
# 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 reset

#Clean

Cara membersihkan file yang tidak ter-track dari working directory.

bash
# 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 -i

#Reflog

Reflog menyimpan history dari semua operasi Git, berguna untuk recover commit yang hilang.

bash
# 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 abc123

#Working with Submodules

Submodules memungkinkan menyertakan repository Git lain sebagai subdirektori.

#Adding Submodules

Cara menambahkan dan mengelola submodule dalam repository.

bash
# 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 --remote

#Workflows

Workflow adalah pola kerja tim yang menggunakan Git untuk kolaborasi yang efektif.

#Feature Branch Workflow

Workflow dimana setiap fitur dikembangkan di branch terpisah.

bash
# 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-login

#Hotfix Workflow

Workflow untuk memperbaiki bug kritis di production dengan cepat.

bash
# 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-bug

#Gitflow Workflow

Workflow komprehensif dengan branch main, develop, feature, release, dan hotfix.

bash
# 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.0

#Useful Aliases

Alias mempersingkat perintah Git yang sering digunakan untuk meningkatkan produktivitas.

#Setup Aliases

Cara mengatur alias Git untuk perintah yang sering dipakai.

bash
# 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'

#Common Aliases

Contoh alias yang berguna untuk operasi Git sehari-hari.

bash
# 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 message

#Troubleshooting

Solusi untuk masalah-masalah umum yang sering terjadi saat menggunakan Git.

#Common Issues

Cara mengatasi masalah-masalah umum seperti undo changes dan resolve conflicts.

bash
# 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.txt

#Safe Operations

Praktik aman untuk menghindari kehilangan data saat menggunakan Git.

bash
# 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 -n

#Tips & Best Practices

Panduan praktik terbaik untuk menggunakan Git secara efektif dan aman.

#Commit Messages

Format dan panduan untuk menulis commit message yang baik.

bash
# 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"

#Ignore Files

Cara mengatur file yang tidak ingin di-track oleh Git menggunakan .gitignore.

bash
# Create .gitignore file
touch .gitignore
 
# Common patterns:
# node_modules/
# .env
# *.log
# dist/
# build/
# .DS_Store
# *.swp
 
# Ignore globally
git config --global core.excludesfile ~/.gitignore_global

#Security

Cara menjaga keamanan repository dengan menghapus file sensitif dari history.

bash
# 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.txt

Baca Cheat Sheet Lengkap

Login atau daftar akun gratis untuk membaca cheat sheet ini.

LoginDaftar Gratis
Share: