Home > Git
Slide 1 / 22
πŸ“‚

Git

Version Control System

2 Weeks - Complete Guide

Version control system untuk tracking perubahan kode dan kolaborasi tim

Overview

Tujuan Pembelajaran

Kenapa Git?

Minggu 1

Apa itu Git?

Git adalah distributed VCS dibuat Linus Torvalds tahun 2005 untuk Linux kernel.

Istilah Penting

Minggu 1 - Install

Install Git

Linux (Ubuntu/Debian)

sudo apt update
sudo apt install git
git --version

macOS

brew install git
# atau: xcode-select --install

Windows

# Download: git-scm.com/download/windows
# atau: choco install git
Minggu 1 - Setup

Git Config

# Setup username
git config --global user.name "Nama Anda"

# Setup email
git config --global user.email "[email protected]"

# Cek config
git config --list

Useful Aliases

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
Minggu 1

Mulai dengan Git

# 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

Cek Status

git status

# Status:
# - Untracked: belum dikenal Git
# - Modified: sudah diubah
# - Staged: siap di-commit
# - Committed: sudah tersimpan
git init membuat folder .git di directory
Minggu 1

Staging & Commit

Menambahkan ke Staging

# Tambah satu file
git add filename.txt

# Tambah semua file
git add .

# Tambah dengan pattern
git add *.js
git add -A

Commit

# Commit dengan pesan
git commit -m "Pesan commit"

# Commit langsung (modified files)
git commit -am "Pesan commit"
Best practice: Commit sering dengan pesan jelas!
Minggu 1

Lihat History

# 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

Batal Perubahan

# Unstage file
git reset HEAD filename

# Batal perubahan (belum commit)
git checkout -- filename

# Ubah commit terakhir
git commit --amend -m "Pesan baru"
Minggu 1

Branching

# 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
Minggu 1

Stash & Switch

Stash (Simpan Sementara)

# Simpan perubahan sementara
git stash

# Lihat stash list
git stash list

# Kembalikan stash
git stash pop

# Kembalikan stash tertentu
git stash apply stash@{0}

Switch Branch

# Pindah branch
git checkout main
git switch feature-login

# Buat dan switch
git switch -c feature baru
Stash berguna saat perlu switch branch tapi belum mau commit!
Minggu 1

Merging

# Pindah ke branch tujuan
git checkout main

# Merge branch lain
git merge feature-login

# Merge dengan rebase
git rebase feature-login

Resolve Conflict

# Jika conflict:
# 1. Edit file yang conflict
# 2. Hapus tanda <<< === >>>
# 3. git add file
# 4. git commit

# Abort merge
git merge --abort
Minggu 2

Remote Repository

# 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 & Pull

# 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
Minggu 2

.gitignore

.gitignore menentukan file yang diabaikan Git
# 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/

Untrack File

# Hapus dari tracking
git rm --cached filename
Minggu 2

Tags

# 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
Tags untuk release versions!
Minggu 2

Reset & Revert

Reset

# 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 (Aman untuk Remote)

# Revert commit
git revert commit-id

# Revert tanpa auto-commit
git revert -n commit-id
JANGAN reset --hard jika sudah di-push!
Minggu 2

Rebase

# Rebase feature ke main
git checkout feature
git rebase main

Interactive Rebase

# Ubah 3 commits terakhir
git rebase -i HEAD~3

# Commands:
# pick - gunakan commit
# reword - ubah pesan
# squash - gabung
# drop - hapus

Golden Rules

Minggu 2

GitHub Basics

Pull Request Workflow

  1. Fork repository
  2. Clone forked repo
  3. Buat branch baru
  4. Buat perubahan
  5. Commit & push
  6. Buka PR di GitHub

Sync Fork

# Tambah upstream
git remote add upstream original-url

# Fetch upstream
git fetch upstream

# Merge ke main
git checkout main
git merge upstream/main
Minggu 2

SSH Keys

# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"

# Start agent
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Add ke GitHub

  1. Copy: cat ~/.ssh/id_ed25519.pub
  2. GitHub β†’ Settings β†’ SSH Keys
  3. Add New β†’ Paste

Use SSH

git clone [email protected]:user/repo.git
Best Practices

Git Workflow

Branch Naming

feature/login
bugfix/fix-payment
hotfix/security-patch
release/v1.0.0

Commit Messages

# Bad
fixed it
update

# Good
Add user login functionality
Fix bug in payment processing

Conventional Commits

feat: add new feature
fix: bug fix
docs: documentation changes
refactor: code refactoring
test: add tests
Cheat Sheet

Ringkasan

# 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
Troubleshooting

Troubleshooting

MasalahSolusi
Merge conflictEdit file, git add, commit
Wrong messagegit commit --amend
Push rejectedgit pull --rebase
Detached HEADgit checkout branch

Emergency

# Lihat reflog
git reflog

# Pulihkan
git checkout HEAD@{number}

# Force push
git push --force-with-lease

πŸŽ‰ Selesai!

Next Steps

1. Practice: buat repo dan experiment

2. Pelajari GitHub Flow

3. Coba Git dengan tim

4. Explore: Git hooks, CI/CD

Advanced

Git Workflows

Trunk-Based Development

main ──────────────────────────────────
         β”‚    β”‚         β”‚
    feature-a  feature-b   hotfix

GitFlow

main ─────●─────●─────●───●──
              β”‚              β”‚
         develop β”€β”€β—β”€β”€β—β”€β”€β—β”€β”€β—β”€β”˜
              β”‚    β”‚
         feature  feature

Kapan Rebase vs Merge?

SituationRebaseMerge
Feature branch ke mainClean historyPreserve context
Sudah di-pushJangan!OK
Pull changesgit pull --rebasegit pull
Golden Rule: Jangan rebase commits yang sudah di-push ke shared branch!
Advanced

Git Bisect & Stash

Git Bisect - Bug Hunting

# 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
Tips: Gunakan git bisect start + git bisect good/bad untuk binary search bug dalam hitungan menit!

Git Stash Advanced

# 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

Git Reflog - Recovery

# 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
Reflog penyelamat: Meskipun reset --hard atau rebase salah, reflog bisa pulihkan!

Kuis: Git

Apa bedanya git merge dan git rebase?

Apa fungsi git stash?

Apa itu git bisect?