Version Control System
2 Weeks - Complete Guide
Version control system untuk tracking perubahan kode dan kolaborasi tim
Repository - Tempat menyimpan projectCommit - Snapshot perubahanBranch - Garis pengembangan terpisahMerge - Menggabungkan branchClone - Mendownload repoPush/Pull - Upload/download ke remotesudo apt update
sudo apt install git
git --version
brew install git
# atau: xcode-select --install
# Download: git-scm.com/download/windows
# atau: choco install git
# Setup username
git config --global user.name "Nama Anda"
# Setup email
git config --global user.email "[email protected]"
# Cek config
git config --list
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm commit
git config --global alias.lg "log --oneline --graph --all"
# Default branch
git config --global init.defaultBranch main
# Inisialisasi folder sebagai Git repo
git init
# Clone repository yang sudah ada
git clone https://github.com/user/repo.git
git clone https://github.com/user/repo.git my-folder
git status
# Status:
# - Untracked: belum dikenal Git
# - Modified: sudah diubah
# - Staged: siap di-commit
# - Committed: sudah tersimpan
# Tambah satu file
git add filename.txt
# Tambah semua file
git add .
# Tambah dengan pattern
git add *.js
git add -A
# Commit dengan pesan
git commit -m "Pesan commit"
# Commit langsung (modified files)
git commit -am "Pesan commit"
# Lihat commit history
git log
# Format ringkas
git log --oneline
# Dengan graph
git log --oneline --graph --all
# Lihat perubahan
git show commit-id
git diff
# Unstage file
git reset HEAD filename
# Batal perubahan (belum commit)
git checkout -- filename
# Ubah commit terakhir
git commit --amend -m "Pesan baru"
# Lihat branch
git branch
# Buat branch baru
git branch nama-branch
# Pindah ke branch
git checkout nama-branch
# atau (Git 2.23+)
git switch nama-branch
# Buat dan pindah sekaligus
git checkout -b nama-branch
git switch -c nama-branch
# Hapus branch
git branch -d nama-branch
# Simpan perubahan sementara
git stash
# Lihat stash list
git stash list
# Kembalikan stash
git stash pop
# Kembalikan stash tertentu
git stash apply stash@{0}
# Pindah branch
git checkout main
git switch feature-login
# Buat dan switch
git switch -c feature baru
# Pindah ke branch tujuan
git checkout main
# Merge branch lain
git merge feature-login
# Merge dengan rebase
git rebase feature-login
# Jika conflict:
# 1. Edit file yang conflict
# 2. Hapus tanda <<< === >>>
# 3. git add file
# 4. git commit
# Abort merge
git merge --abort
# Lihat remote
git remote -v
# Tambah remote
git remote add origin https://github.com/user/repo.git
# Ubah remote URL
git remote set-url origin new-url.git
# Push ke remote
git push origin main
git push -u origin feature-login
# Pull perubahan
git pull origin main
# Fetch tanpa merge
git fetch origin
# Ignore node_modules
node_modules/
# Ignore environment
.env
.env.local
# Ignore build files
dist/
build/
*.o
# Ignore logs
*.log
logs/
# Ignore sistem
.DS_Store
.vscode/
# Hapus dari tracking
git rm --cached filename
# Lightweight tag
git tag v1.0.0
# Annotated tag
git tag -a v1.0.0 -m "Version 1.0.0"
# Lihat tags
git tag
git tag -l "v1.*"
# Push tag ke remote
git push origin v1.0.0
git push --tags
# Delete tag
git tag -d v1.0.0
git push origin --delete v1.0.0
# Soft: retain di staging
git reset --soft HEAD~1
# Mixed: retain di working dir
git reset --mixed HEAD~1
# Hard: HAPUS semua
git reset --hard HEAD~1
# Revert commit
git revert commit-id
# Revert tanpa auto-commit
git revert -n commit-id
# Rebase feature ke main
git checkout feature
git rebase main
# Ubah 3 commits terakhir
git rebase -i HEAD~3
# Commands:
# pick - gunakan commit
# reword - ubah pesan
# squash - gabung
# drop - hapus
# Tambah upstream
git remote add upstream original-url
# Fetch upstream
git fetch upstream
# Merge ke main
git checkout main
git merge upstream/main
# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"
# Start agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
git clone [email protected]:user/repo.git
feature/login
bugfix/fix-payment
hotfix/security-patch
release/v1.0.0
# Bad
fixed it
update
# Good
Add user login functionality
Fix bug in payment processing
feat: add new feature
fix: bug fix
docs: documentation changes
refactor: code refactoring
test: add tests
# Setup
git config --global user.name "Name"
# Mulai
git init
git clone url
# Perubahan
git add .
git commit -m "message"
# Branching
git checkout -b feature
git merge feature
# Remote
git push origin main
git pull origin main
# History
git log --oneline
git diff
# Undo
git reset --soft HEAD~1
| Masalah | Solusi |
|---|---|
| Merge conflict | Edit file, git add, commit | Wrong message | git commit --amend |
| Push rejected | git pull --rebase |
| Detached HEAD | git checkout branch |
# Lihat reflog
git reflog
# Pulihkan
git checkout HEAD@{number}
# Force push
git push --force-with-lease
Next Steps
1. Practice: buat repo dan experiment
2. Pelajari GitHub Flow
3. Coba Git dengan tim
4. Explore: Git hooks, CI/CD
main ββββββββββββββββββββββββββββββββββ
β β β
feature-a feature-b hotfix
mainmain ββββββββββββββββββββββββ
β β
develop ββββββββββββββ
β β
feature feature
main: productiondevelop: integrationfeature/*: feature developmentrelease/*: release preparationhotfix/*: production fixes| Situation | Rebase | Merge |
|---|---|---|
| Feature branch ke main | Clean history | Preserve context |
| Sudah di-push | Jangan! | OK |
| Pull changes | git pull --rebase | git pull |
# Mulai bisect
git bisect start
# Tandai commit saat ini sebagai BAD
git bisect bad
# Tandai commit yang working sebagai GOOD
git bisect good v1.0.0
# Git akan checkout commit di tengah
# Test aplikasi, lalu:
git bisect good # atau git bisect bad
# Ulangi sampai ketemu!
# Git akanζ₯εζΎη€Ί "commit abc123 is first bad commit"
# Selesai - kembali ke state awal
git bisect reset
git bisect start + git bisect good/bad untuk binary search bug dalam hitungan menit!# Stash dengan message
git stash save "WIP: new feature login"
# Stash termasuk untracked files
git stash -u
# Stash dengan message (modern)
git stash push -m "WIP: feature X"
# Apply stash ke branch lain
git stash apply stash@{0}
# Create branch dari stash
git stash branch new-feature stash@{0}
# Drop stash tertentu
git stash drop stash@{0}
# Clear all stash
git stash clear
# Lihat semua aktivitas
git reflog
# Output:
# abc123 HEAD@{0}: commit: Add feature X
# def456 HEAD@{1}: rebase: onto main
# 789012 HEAD@{2}: checkout: moving to feature-x
# Pulihkan commit yang "hilang"
git checkout abc123
# Atau buat branch baru
git branch recovery abc123
Apa bedanya git merge dan git rebase?
Apa fungsi git stash?
Apa itu git bisect?