Tutorial Git & GitHub
Dari pengenalan version control hingga deploy website ke GitHub Pages
1. Pengenalan Git & GitHub
Apa itu Git?
Git adalah sistem version control terdistribusi yang melacak setiap perubahan pada kode — siapa mengubah apa, kapan, dan kenapa.
- Dibuat oleh Linus Torvalds (pencipta Linux) tahun 2005
- Menyimpan seluruh riwayat perubahan proyek
- Memungkinkan kembali ke versi sebelumnya kapan saja
- Bekerja secara lokal — tidak perlu koneksi internet
- Distributed: setiap developer punya salinan penuh repository
Apa itu GitHub?
GitHub adalah platform hosting repository Git berbasis cloud dengan fitur kolaborasi, CI/CD, dan deployment.
- Tempat menyimpan repository Git secara online
- Alat kolaborasi tim: Pull Request, Code Review, Issues
- GitHub Actions untuk otomasi (CI/CD)
- GitHub Pages untuk hosting website gratis
- Portofolio publik untuk developer
Konsep Dasar: Tiga Area Git
Directory
Area
Repository
Repository
WORKING DIRECTORY
Folder proyek di komputer kamu.
Tempat kamu edit, buat, atau hapus file.
Status file: Untracked / Modified / Deleted
STAGING AREA (Index)
Area persiapan sebelum commit.
Kamu memilih perubahan mana yang akan dimasukkan commit.
Perintah: git add
LOCAL REPOSITORY (.git folder)
Database Git di komputermu.
Menyimpan semua commit dan riwayat.
Perintah: git commit
REMOTE REPOSITORY (GitHub, GitLab, dll)
Repository yang ada di server/cloud.
Tempat berbagi kode dengan tim.
Perintah: git push / git pull
Mengapa Harus Belajar Git?
- Digunakan di hampir 100% perusahaan teknologi dunia
- Skill wajib untuk developer profesional
- Bisa kembali ke versi lama jika kode rusak
- Kerja tim tanpa saling menimpa pekerjaan satu sama lain
- GitHub sebagai portofolio yang dilihat recruiter
- Dasar untuk CI/CD, DevOps, dan deployment modern
2. Instalasi & Konfigurasi
Install Git
WINDOWS:
1. Download: https://git-scm.com/download/win
2. Jalankan installer, centang "Git Bash Here"
3. Pilihan editor: pilih VS Code jika tersedia
4. Line ending: pilih "Checkout Windows, commit Unix"
5. Verifikasi:
git --version
# Output: git version 2.x.x
MACOS:
# Via Homebrew (direkomendasikan)
brew install git
# Atau via Xcode Command Line Tools
xcode-select --install
LINUX (Ubuntu/Debian):
sudo apt update
sudo apt install git
LINUX (Fedora/RHEL):
sudo dnf install git
# Verifikasi semua OS:
git --version
Konfigurasi Awal (Wajib!)
Sebelum menggunakan Git, set identitas kamu. Informasi ini akan muncul di setiap commit.
# Set nama dan email (WAJIB — sesuaikan dengan akun GitHub)
git config --global user.name "Ahmad Hidayat"
git config --global user.email "ahmad@email.com"
# Set editor default (pilih salah satu)
git config --global core.editor "code --wait" # VS Code
git config --global core.editor "nano" # Nano
git config --global core.editor "vim" # Vim
# Set nama branch default ke 'main' (standar modern)
git config --global init.defaultBranch main
# Konfigurasi line ending (Windows)
git config --global core.autocrlf true
# Konfigurasi line ending (Mac/Linux)
git config --global core.autocrlf input
# Tampilkan semua konfigurasi
git config --list
# Cek konfigurasi tertentu
git config user.name
git config user.email
Setup SSH Key untuk GitHub
SSH Key memungkinkan koneksi ke GitHub tanpa memasukkan password setiap saat.
# 1. Generate SSH key baru
ssh-keygen -t ed25519 -C "email@kamu.com"
# Tekan Enter untuk semua pertanyaan (atau set passphrase)
# 2. Tampilkan public key
cat ~/.ssh/id_ed25519.pub
# Salin output yang muncul
# 3. Tambah ke GitHub:
# GitHub → Settings → SSH and GPG keys
# → New SSH key → paste public key → Save
# 4. Test koneksi
ssh -T git@github.com
# Output: Hi username! You've successfully authenticated...
# 5. Cek SSH agent berjalan (opsional)
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
3. Perintah Dasar Git
Inisialisasi & Status Repository
# Buat repository baru di folder saat ini
git init
# Buat repository + langsung buat folder baru
git init nama-proyek
# Cek status repository (paling sering dipakai!)
git status
# Versi ringkas status
git status -s
# Output:
# M file-dimodifikasi.txt (M = modified)
# A file-baru.txt (A = added/staged)
# ?? file-untracked.txt (?? = belum di-track)
# Lihat isi .git folder
ls -la .git
Staging dan Commit
# Tambah satu file ke staging area
git add nama-file.txt
# Tambah semua perubahan di folder saat ini
git add .
# Tambah semua file bertipe tertentu
git add *.js
git add src/
# Tambah secara interaktif (pilih per-bagian)
git add -p
# Hapus file dari staging (batalkan git add)
git restore --staged nama-file.txt
# Commit dengan pesan singkat
git commit -m "feat: tambah halaman login"
# Commit dengan pesan panjang (buka editor)
git commit
# Shortcut: add semua file yang sudah di-track + commit
git commit -am "fix: perbaiki bug validasi form"
# Ubah pesan commit terakhir (sebelum push!)
git commit --amend -m "pesan baru yang benar"
Riwayat Commit (Log)
# Lihat riwayat commit lengkap
git log
# Log ringkas satu baris per commit
git log --oneline
# Log dengan grafik branch
git log --oneline --graph --all
# Log N commit terakhir
git log -5
# Log dengan perubahan file
git log --stat
# Log commit oleh orang tertentu
git log --author="Ahmad"
# Cari commit berdasarkan pesan
git log --grep="login"
# Log perubahan baris kode
git log -p
# Format kustom
git log --pretty=format:"%h %an %s %ar"
# %h = hash pendek, %an = nama author
# %s = subject, %ar = waktu relatif
Melihat Perbedaan (Diff)
# Lihat perubahan yang BELUM di-staging
git diff
# Lihat perubahan yang SUDAH di-staging
git diff --staged
# atau
git diff --cached
# Bandingkan dua commit
git diff abc1234 def5678
# Bandingkan dua branch
git diff main feature-login
# Diff file tertentu saja
git diff HEAD -- src/index.js
# Statistik perubahan (berapa baris +/-)
git diff --stat
Membatalkan dan Mengembalikan Perubahan
# Batalkan perubahan di working directory (belum di-add)
git restore nama-file.txt
git checkout -- nama-file.txt # cara lama
# Keluarkan file dari staging (sudah git add, belum commit)
git restore --staged nama-file.txt
# Buat commit baru yang membatalkan commit sebelumnya (AMAN)
git revert abc1234
git revert HEAD # batalkan commit terakhir
# Reset ke commit tertentu — HATI-HATI!
git reset --soft HEAD~1 # batalkan commit, perubahan tetap di staging
git reset --mixed HEAD~1 # batalkan commit, perubahan kembali ke working dir
git reset --hard HEAD~1 # batalkan commit + HAPUS semua perubahan!
# Simpan perubahan sementara tanpa commit (stash)
git stash # simpan
git stash save "pesan stash" # simpan dengan label
git stash list # lihat daftar stash
git stash pop # ambil stash terbaru + hapus dari list
git stash apply stash@{0} # ambil stash tanpa hapus dari list
git stash drop stash@{0} # hapus stash
git stash clear # hapus semua stash
.gitignore — Mengabaikan File
# Buat file .gitignore di root proyek
touch .gitignore
# ── Contoh isi .gitignore ──
# Folder node_modules
node_modules/
# File environment (berisi password/API key!)
.env
.env.local
.env.production
# Build output
dist/
build/
out/
# OS files
.DS_Store # macOS
Thumbs.db # Windows
desktop.ini
# Log files
*.log
logs/
# IDE / Editor
.vscode/
.idea/
*.suo
*.swp
# Dependency caches
.npm
.yarn
# Coverage reports
coverage/
# ── Cara kerja pattern ──
# *.txt — abaikan semua file .txt
# !readme.txt — KECUALI readme.txt
# /secret.txt — hanya di root
# folder/ — abaikan folder
# **/logs — abaikan folder 'logs' di mana saja
# Generator .gitignore otomatis:
# https://www.toptal.com/developers/gitignore
4. Branching & Merging
Konsep Branch
Branch adalah "jalur pengembangan" terpisah. Kamu bisa membuat fitur baru tanpa mengganggu kode utama.
main (branch utama — kode stabil, di-deploy)
│
├── feature/login ← develop fitur login
├── feature/dashboard ← develop fitur dashboard
├── bugfix/navbar-error ← perbaiki bug navbar
└── hotfix/payment-bug ← perbaiki bug kritis production
Setelah selesai, branch di-merge kembali ke main.
Manajemen Branch
# Lihat semua branch (lokal)
git branch
# Lihat semua branch termasuk remote
git branch -a
# Buat branch baru
git branch feature/login
# Pindah ke branch
git checkout feature/login
git switch feature/login # cara modern (Git 2.23+)
# Buat branch baru + langsung pindah (shortcut)
git checkout -b feature/login
git switch -c feature/login # cara modern
# Rename branch
git branch -m nama-lama nama-baru
git branch -m main # rename branch saat ini
# Hapus branch (setelah merge)
git branch -d feature/login # aman (gagal jika belum merge)
git branch -D feature/login # paksa hapus
# Lihat branch mana yang sudah/belum di-merge
git branch --merged
git branch --no-merged
Merge Branch
# Workflow merge:
# 1. Pastikan branch tujuan (main) ter-update
git checkout main
git pull origin main
# 2. Merge branch fitur ke main
git merge feature/login
# Jenis merge:
# Fast-Forward merge (jika main tidak berubah sejak branch dibuat)
# 3-Way merge (jika keduanya berubah → buat commit baru)
# Merge dengan selalu buat commit baru (no fast-forward)
git merge --no-ff feature/login
# Merge sekaligus squash semua commit jadi satu
git merge --squash feature/login
# Batalkan merge yang sedang berjalan
git merge --abort
# ── Resolve Merge Conflict ──
# Saat ada konflik, Git tandai di file:
<<<<<<< HEAD
kode dari branch saat ini (main)
=======
kode dari branch yang di-merge (feature/login)
>>>>>>> feature/login
# Cara resolve:
# 1. Buka file yang konflik
# 2. Pilih / gabung kode yang benar
# 3. Hapus marker <<<, ===, >>>
# 4. git add file-yang-sudah-difix
# 5. git commit
Rebase
Rebase memindahkan/menyusun ulang commit sehingga history lebih linear dan bersih.
# Rebase branch feature ke atas main terbaru
git checkout feature/login
git rebase main
# Interactive rebase — edit, squash, hapus commit
git rebase -i HEAD~3 # edit 3 commit terakhir
git rebase -i abc1234 # dari commit tertentu
# Di dalam interactive rebase:
# pick = pakai commit apa adanya
# reword = pakai commit, edit pesannya
# edit = pakai commit, berhenti untuk edit
# squash = gabung dengan commit sebelumnya
# fixup = gabung tanpa simpan pesan
# drop = hapus commit ini
# Batalkan rebase
git rebase --abort
# Lanjutkan rebase setelah resolve konflik
git rebase --continue
# ATURAN PENTING:
# Jangan rebase branch yang sudah di-push ke remote
# (kecuali force push dengan koordinasi tim)
Cherry-pick
# Ambil satu commit dari branch lain ke branch saat ini
git cherry-pick abc1234
# Cherry-pick beberapa commit sekaligus
git cherry-pick abc1234 def5678
# Cherry-pick tanpa langsung commit
git cherry-pick -n abc1234
# Cherry-pick range commit
git cherry-pick abc1234^..def5678
# Contoh use case:
# Ada bugfix di branch 'develop' yang perlu segera di-apply
# ke branch 'main' tanpa merge seluruh branch 'develop'
git checkout main
git cherry-pick abc1234 # ambil hanya commit bugfix itu
5. Remote Repository & GitHub
Membuat Repository di GitHub
CARA 1 — Buat di GitHub dulu, lalu clone:
1. Buka github.com → New repository
2. Isi nama, deskripsi, centang "Add README"
3. Klik "Create repository"
4. Clone ke lokal:
git clone https://github.com/username/nama-repo.git
git clone git@github.com:username/nama-repo.git (SSH)
CARA 2 — Sudah ada proyek lokal, push ke GitHub:
1. Buat repo kosong di GitHub (jangan centang README)
2. Di terminal proyek lokal:
git init
git add .
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:username/nama-repo.git
git push -u origin main
Remote Commands
# Lihat remote yang terhubung
git remote
git remote -v # tampilkan URL juga
# Tambah remote baru
git remote add origin git@github.com:user/repo.git
git remote add upstream git@github.com:original/repo.git
# Ubah URL remote
git remote set-url origin git@github.com:user/repo-baru.git
# Hapus remote
git remote remove nama-remote
# Rename remote
git remote rename origin github
# ── Fetch, Pull, Push ──
# Ambil update dari remote (tanpa merge ke branch lokal)
git fetch origin
git fetch --all # fetch semua remote
# Pull = fetch + merge
git pull origin main
git pull # shortcut jika tracking sudah di-set
# Pull dengan rebase (lebih bersih)
git pull --rebase origin main
# Push branch ke remote
git push origin main
git push origin feature/login
# Push sekaligus set tracking upstream
git push -u origin main # -u = --set-upstream
# Push semua branch lokal
git push --all origin
# Push tag
git push origin --tags
Clone Repository
# Clone repository
git clone https://github.com/user/repo.git
# Clone ke folder dengan nama berbeda
git clone https://github.com/user/repo.git nama-folder
# Clone branch tertentu saja
git clone -b develop https://github.com/user/repo.git
# Clone tanpa riwayat (shallow clone — lebih cepat)
git clone --depth 1 https://github.com/user/repo.git
# Clone hanya satu commit terbaru (sangat ringan)
git clone --depth 1 --single-branch https://github.com/user/repo.git
# Setelah clone, lihat struktur
cd repo
git log --oneline
git branch -a
Tagging — Menandai Versi Rilis
# Buat tag ringan (lightweight)
git tag v1.0.0
# Buat tag beranotasi (direkomendasikan untuk rilis)
git tag -a v1.0.0 -m "Rilis versi 1.0.0 — fitur login dan dashboard"
# Buat tag di commit tertentu
git tag -a v0.9.0 abc1234 -m "Beta release"
# Lihat semua tag
git tag
# Lihat detail tag
git show v1.0.0
# Push tag ke remote
git push origin v1.0.0
git push origin --tags # push semua tag
# Hapus tag lokal
git tag -d v1.0.0
# Hapus tag dari remote
git push origin --delete v1.0.0
# Checkout ke tag tertentu
git checkout v1.0.0 # detached HEAD state
6. Kolaborasi di GitHub
GitHub Flow — Alur Kerja Tim
ALUR KERJA GITHUB FLOW:
1. Update branch main lokal
git checkout main
git pull origin main
2. Buat branch baru dari main
git checkout -b feature/nama-fitur
3. Kerjakan fitur, buat commit berkala
git add .
git commit -m "feat: tambah komponen Card"
git commit -m "feat: tambah styling Card"
git commit -m "test: tambah unit test Card"
4. Push branch ke GitHub
git push -u origin feature/nama-fitur
5. Buat Pull Request di GitHub
- Buka repo di browser
- Klik "Compare & pull request"
- Isi deskripsi, request reviewer
- Klik "Create pull request"
6. Code Review
- Reviewer beri komentar
- Perbaiki jika ada feedback
- Push commit baru ke branch yang sama
7. Merge Pull Request (setelah approved)
- "Squash and merge" (1 commit bersih)
- atau "Merge commit" (pertahankan semua commit)
8. Hapus branch setelah merge
git branch -d feature/nama-fitur
git push origin --delete feature/nama-fitur
Fork & Pull Request (Kontribusi Open Source)
# 1. Fork repository di GitHub (klik tombol Fork)
# Ini membuat salinan repo di akun kamu
# 2. Clone fork kamu
git clone git@github.com:KAMU/nama-repo.git
cd nama-repo
# 3. Tambah remote 'upstream' (repo original)
git remote add upstream git@github.com:ORIGINAL/nama-repo.git
git remote -v
# 4. Buat branch untuk perubahan
git checkout -b fix/typo-readme
# 5. Buat perubahan dan commit
git add .
git commit -m "fix: perbaiki typo di README.md"
# 6. Sync dengan upstream sebelum push
git fetch upstream
git rebase upstream/main
# 7. Push ke fork kamu
git push origin fix/typo-readme
# 8. Buat Pull Request di GitHub
# Dari: KAMU/nama-repo (branch fix/typo-readme)
# Ke : ORIGINAL/nama-repo (branch main)
# 9. Update fork dengan upstream terbaru (rutin)
git fetch upstream
git checkout main
git merge upstream/main
git push origin main
Issues, Discussions & Wiki
ISSUES — Pelacak bug dan fitur:
- Laporkan bug atau request fitur
- Label: bug, enhancement, documentation, help wanted
- Assign ke anggota tim
- Hubungkan dengan PR: "Closes #42" di deskripsi PR
→ Issue otomatis tertutup saat PR di-merge
PULL REQUEST TIPS:
- Judul jelas: "feat: tambah autentikasi OAuth Google"
- Deskripsi apa yang diubah dan mengapa
- Screenshot UI jika ada perubahan visual
- Centang checklist: test lolos, dokumentasi diupdate
- Link ke issue yang diselesaikan
CODE REVIEW:
- Reviewer: beri komentar konstruktif, bukan personal
- Gunakan "Suggest changes" untuk saran langsung
- Approve jika sudah siap merge
- Request changes jika masih perlu perbaikan
GITHUB DISCUSSIONS:
- Untuk tanya jawab dan obrolan umum
- Tidak seperti Issues (yang untuk bug/task spesifik)
WIKI:
- Dokumentasi panjang proyek
- Panduan setup, arsitektur, konvensi kode
GitHub Actions — CI/CD Otomatis
name: CI Pipeline
on:
push:
branches: [ main, develop ]
pull_request:
branches: [ main ]
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout kode
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Jalankan linting
run: npm run lint
- name: Jalankan tests
run: npm test
- name: Build proyek
run: npm run build
# Workflow ini otomatis berjalan setiap:
# - Ada push ke branch main atau develop
# - Ada Pull Request ke branch main
# Jika ada yang gagal, PR tidak bisa di-merge
7. Git Tingkat Lanjut
Git Bisect — Temukan Bug dengan Binary Search
# Git bisect mencari commit penyebab bug dengan binary search
# Sangat berguna jika ada ratusan commit
# 1. Mulai bisect
git bisect start
# 2. Tandai commit sekarang sebagai 'bad' (ada bugnya)
git bisect bad
# 3. Tandai commit lama yang masih bagus sebagai 'good'
git bisect good v1.0.0
# atau pakai hash commit
git bisect good abc1234
# 4. Git otomatis checkout commit di tengah
# Cek apakah bug ada di commit ini
# Jika ada bug:
git bisect bad
# Jika tidak ada bug:
git bisect good
# 5. Ulangi hingga Git temukan commit penyebab bug
# 6. Setelah selesai, kembali ke HEAD
git bisect reset
# Bisect otomatis dengan script
git bisect run npm test # jalankan test otomatis
Git Reflog — Riwayat Tersembunyi
# Reflog menyimpan semua pergerakan HEAD
# Sangat berguna untuk recovery setelah kesalahan
# Lihat reflog
git reflog
# Output contoh:
# abc1234 HEAD@{0}: commit: feat: tambah halaman about
# def5678 HEAD@{1}: reset: moving to HEAD~1
# ghi9012 HEAD@{2}: commit: fix: perbaiki bug login
# jkl3456 HEAD@{3}: checkout: moving from main to feature
# Kembalikan ke state sebelum reset --hard
git reset --hard HEAD@{1}
# atau
git reset --hard ghi9012
# Recover branch yang sudah dihapus
git reflog # temukan hash terakhir branch itu
git checkout -b nama-branch abc1234
# Reflog tersimpan selama 90 hari secara default
Git Submodules & Worktrees
# ── SUBMODULES — Repository di dalam repository ──
# Berguna untuk shared library/component antar proyek
# Tambah submodule
git submodule add https://github.com/user/library.git libs/library
# Clone repo dengan submodule
git clone --recurse-submodules https://github.com/user/repo.git
# Update semua submodule
git submodule update --init --recursive
# Update submodule ke versi terbaru
git submodule update --remote
# ── WORKTREES — Multiple checkout sekaligus ──
# Berguna jika ingin kerjakan dua branch sekaligus
# tanpa switch branch
# Buat worktree di folder baru
git worktree add ../hotfix-branch hotfix/payment-bug
# Sekarang kamu punya dua folder:
# ./repo-utama → branch main
# ../hotfix-branch → branch hotfix/payment-bug
# Lihat semua worktree
git worktree list
# Hapus worktree
git worktree remove ../hotfix-branch
Git Hooks — Otomasi Lokal
# Git hooks adalah script yang berjalan otomatis
# saat event tertentu terjadi
# Lokasi hooks: .git/hooks/
ls .git/hooks/
# pre-commit, commit-msg, post-commit,
# pre-push, post-merge, dll
# ── Contoh: pre-commit hook ──
# Jalankan linting sebelum setiap commit
# Buat file: .git/hooks/pre-commit
#!/bin/sh
echo "Menjalankan linting..."
npm run lint
if [ $? -ne 0 ]; then
echo "Linting gagal! Commit dibatalkan."
exit 1
fi
echo "Linting sukses. Commit dilanjutkan."
# Jadikan executable
chmod +x .git/hooks/pre-commit
# ── Husky — Git Hooks yang bisa di-share ke tim ──
# (hooks di .git/ tidak ikut di-commit)
npm install --save-dev husky
npx husky init
# .husky/pre-commit:
npm run lint
# .husky/commit-msg:
npx --no -- commitlint --edit $1
Git Alias — Shortcut Perintah
# Buat alias via command
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.cm "commit -m"
git config --global alias.lg "log --oneline --graph --all"
# Atau edit langsung ~/.gitconfig:
[alias]
st = status
co = checkout
sw = switch
br = branch
cm = commit -m
cam = commit -am
ps = push
pl = pull
lg = log --oneline --graph --all --decorate
df = diff
dfs = diff --staged
rb = rebase
undo = reset HEAD~1 --mixed
save = !git add -A && git commit -m 'SAVEPOINT'
undo-push = push --force-with-lease
# Sekarang bisa pakai:
git st # git status
git lg # log bagus dengan grafik
git undo # batalkan commit terakhir
8. Deploy ke GitHub Pages
Apa itu GitHub Pages?
GitHub Pages adalah layanan hosting statis gratis dari GitHub. Website kamu bisa online di URL username.github.io/nama-repo atau domain custom.
- Gratis tanpa batas waktu untuk repository publik
- HTTPS otomatis (sertifikat SSL gratis)
- CDN global — website cepat diakses dari mana saja
- Deploy otomatis setiap push ke branch tertentu
- Mendukung domain custom (misal: portfolio.com)
Cara 1 — Deploy Langsung dari Branch
Cara paling sederhana: deploy file HTML langsung dari branch main atau gh-pages.
LANGKAH 1: Siapkan proyek
Pastikan ada file index.html di root folder atau folder /docs
LANGKAH 2: Push ke GitHub
git add .
git commit -m "feat: tambah website portofolio"
git push origin main
LANGKAH 3: Aktifkan GitHub Pages
1. Buka repository di GitHub
2. Klik tab "Settings"
3. Scroll ke bagian "Pages" (sidebar kiri)
4. Source: pilih "Deploy from a branch"
5. Branch: pilih "main" → folder: "/ (root)"
6. Klik "Save"
LANGKAH 4: Tunggu deploy (~1-2 menit)
URL website: https://username.github.io/nama-repo
LANGKAH 5: Update website
Cukup push commit baru ke branch main
→ GitHub Pages otomatis update
Catatan:
- Nama repo harus: username.github.io → untuk website utama
URL: https://username.github.io
- Nama repo lain → sub-path
URL: https://username.github.io/nama-repo
Cara 2 — Deploy Menggunakan Branch gh-pages
# Cocok jika file build ada di folder /dist atau /build
# Install gh-pages package (untuk proyek Node.js)
npm install --save-dev gh-pages
# Tambah script di package.json:
{
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
},
"homepage": "https://username.github.io/nama-repo"
}
# Deploy!
npm run deploy
# Script otomatis:
# 1. Build proyek (npm run build)
# 2. Push folder dist/ ke branch gh-pages
# 3. GitHub Pages serve dari branch gh-pages
# Atau deploy manual tanpa package:
# 1. Build proyek
npm run build
# 2. Masuk ke folder build
cd dist
# 3. Init git baru di sana
git init
git add .
git commit -m "deploy"
# 4. Force push ke branch gh-pages
git push -f git@github.com:username/nama-repo.git main:gh-pages
cd ..
Cara 3 — Deploy Otomatis dengan GitHub Actions
Cara paling profesional: setiap push ke main, GitHub Actions otomatis build dan deploy.
name: Deploy ke GitHub Pages
on:
push:
branches: [ main ]
workflow_dispatch: # izinkan trigger manual
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build
run: npm run build
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: './dist' # folder hasil build
deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy ke GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
Setelah push workflow file:
1. GitHub → Settings → Pages
2. Source: pilih "GitHub Actions"
3. Workflow akan otomatis berjalan saat push ke main
URL deploy tampil di:
- Tab "Actions" → workflow run terakhir
- Settings → Pages → URL live
Custom Domain untuk GitHub Pages
# LANGKAH 1: Beli domain (Niagahoster, Namecheap, dll)
# LANGKAH 2: Tambah file CNAME di root proyek
echo "namadomain.com" > CNAME
git add CNAME
git commit -m "chore: tambah custom domain"
git push
# LANGKAH 3: Konfigurasi DNS di provider domain
# Untuk root domain (namadomain.com):
# Tambah A records yang mengarah ke IP GitHub Pages:
# 185.199.108.153
# 185.199.109.153
# 185.199.110.153
# 185.199.111.153
# Untuk subdomain (www.namadomain.com):
# Tambah CNAME record:
# www → username.github.io
# LANGKAH 4: Aktifkan di GitHub Settings
# Settings → Pages → Custom domain
# Masukkan domain → Save
# Centang "Enforce HTTPS"
# LANGKAH 5: Tunggu propagasi DNS (bisa sampai 48 jam)
# Cek status: https://www.whatsmydns.net
Deploy Project-Specific: React, Vue, Next.js
# ── REACT (Vite) ──
# vite.config.js:
export default {
base: '/nama-repo/', // sesuaikan nama repository
}
# package.json:
{
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d dist"
}
}
npm run deploy
# ── REACT (Create React App) ──
# package.json:
{
"homepage": "https://username.github.io/nama-repo",
"scripts": {
"predeploy": "npm run build",
"deploy": "gh-pages -d build"
}
}
npm run deploy
# ── NEXT.JS (Static Export) ──
# next.config.js:
const nextConfig = {
output: 'export',
basePath: '/nama-repo',
images: { unoptimized: true },
}
# package.json:
{
"scripts": {
"deploy": "npm run build && gh-pages -d out"
}
}
# ── VUE (Vite) ──
# vite.config.js:
export default {
base: process.env.NODE_ENV === 'production' ? '/nama-repo/' : '/'
}
Kesimpulan & Langkah Selanjutnya
Ringkasan Materi
- Konsep Git: Working Directory, Staging Area, Repository
- Instalasi dan konfigurasi Git + SSH Key
- Perintah dasar: init, add, commit, log, diff, stash
- Branching, merging, rebase, dan cherry-pick
- Remote repository: push, pull, fetch, clone, tag
- Kolaborasi GitHub: Fork, Pull Request, Code Review, Actions
- Git lanjutan: bisect, reflog, submodule, hooks, alias
- Deploy ke GitHub Pages: dari branch, gh-pages package, GitHub Actions
- Custom domain dan konfigurasi framework (React, Vue, Next.js)
Perintah Paling Sering Dipakai
git status # cek kondisi repo
git add . # staging semua perubahan
git commit -m "pesan" # buat commit
git push # push ke remote
git pull # ambil update remote
git checkout -b nama-branch # buat + pindah branch
git switch -c nama-branch # cara modern
git merge nama-branch # merge ke branch saat ini
git log --oneline --graph --all # lihat grafik riwayat
git stash # simpan sementara
git stash pop # ambil kembali
git diff # lihat perubahan
git restore nama-file # batalkan perubahan
git clone URL # salin repo
git remote -v # lihat remote
Langkah Selanjutnya
- Pelajari Git Flow — strategi branching untuk tim besar
- Dalami GitHub Actions — CI/CD yang lebih kompleks
- Eksplorasi Semantic Versioning dan Conventional Commits
- Belajar Monorepo dengan Turborepo atau Nx
- Coba GitLab CI/CD sebagai alternatif
- Buat GitHub Profile README yang menarik
- Berkontribusi ke proyek Open Source di GitHub
Resources Berguna
Dokumentasi resmi Git:
https://git-scm.com/doc
Pro Git Book (gratis, lengkap):
https://git-scm.com/book/id/v2
GitHub Docs:
https://docs.github.com
GitHub Skills (kursus interaktif):
https://skills.github.com
Oh My Git! (game belajar Git):
https://ohmygit.org
Git Cheat Sheet resmi GitHub:
https://education.github.com/git-cheat-sheet-education.pdf
Conventional Commits:
https://www.conventionalcommits.org