Remote Lanjutan, Fork Sync & Kolaborasi
Tujuan Pembelajaran
- Memahami konsep multiple remotes dan upstream
- Mampu melakukan sinkronisasi fork dengan repository asli
- Menguasai workflow kolaborasi open source
Materi Inti
A. Multiple Remotes & Upstream
Sejauh ini kamu hanya bekerja dengan satu remote yaitu origin. Dalam kolaborasi open source, kamu sering membutuhkan lebih dari satu remote.
Konsep:
- origin: Remote default yang menunjuk ke repository kamu (fork)
- upstream: Remote tambahan yang menunjuk ke repository asli (yang di-fork)
# Lihat semua remote
git remote -v
# origin git@github.com:KAMU/repo.git (fetch)
# origin git@github.com:KAMU/repo.git (push)
# Tambah remote upstream
git remote add upstream git@github.com:PEMILIK-ASLI/repo.git
# Verifikasi
git remote -v
# origin git@github.com:KAMU/repo.git (fetch)
# origin git@github.com:KAMU/repo.git (push)
# upstream git@github.com:PEMILIK-ASLI/repo.git (fetch)
# upstream git@github.com:PEMILIK-ASLI/repo.git (push)
# Rename remote
git remote rename upstream original
# Hapus remote
git remote remove original
B. Remote Tracking Branches
Remote tracking branch seperti origin/main adalah โbookmarkโ yang menunjukkan posisi terakhir yang diketahui dari branch di remote.
# Lihat semua branch termasuk remote tracking
git branch -a
# Fetch update dari semua remote
git fetch --all
# Fetch hanya dari upstream
git fetch upstream
Penting:
origin/maindi lokal kamu TIDAK otomatis terupdate. Kamu perlugit fetchuntuk memperbaruinya.
C. Sinkronisasi Fork
Saat repository asli (upstream) mendapat update, fork kamu akan tertinggal. Berikut cara menjaga fork tetap up-to-date.
Workflow sinkronisasi fork:
# 1. Fetch perubahan dari upstream
git fetch upstream
# 2. Pindah ke branch main lokal
git switch main
# 3. Merge perubahan upstream ke main lokal
git merge upstream/main
# 4. Push ke fork kamu (origin)
git push origin main
Alternatif dengan rebase (history lebih bersih):
git fetch upstream
git switch main
git rebase upstream/main
git push origin main
D. Workflow Kontribusi Open Source
Langkah lengkap berkontribusi ke project open source:
- Fork repository di GitHub
- Clone fork ke lokal
- Tambah upstream remote
- Buat branch fitur dari main yang sudah sync
- Kerjakan perubahan dan commit
- Push ke fork (origin)
- Buat Pull Request dari fork ke repo asli
- Sync fork secara berkala
Demonstrasi Live
Setup Fork dan Upstream
# 1. Fork repo di browser GitHub (klik tombol Fork)
# 2. Clone fork kamu
git clone git@github.com:KAMU/repo-fork.git
cd repo-fork
# 3. Tambah upstream (repo asli)
git remote add upstream git@github.com:PEMILIK/repo-asli.git
git remote -v
# origin โ fork kamu
# upstream โ repo asli
# 4. Fetch dan sync
git fetch upstream
git switch main
git merge upstream/main
git push origin main
# 5. Buat branch fitur untuk kontribusi
git switch -c feature/improve-docs
echo "Dokumentasi yang lebih baik" >> README.md
git add . && git commit -m "docs: improve README documentation"
git push origin feature/improve-docs
# 6. Buat PR dari fork ke repo asli di GitHub
Latihan Interaktif
Latihan LGB: Remote Tracking
Level: โRemote Trackingโ (Advanced Remote - Level 3)
Buka Learn Git Branching dan ketik:
level remoteAdvanced3
Tujuan: Buat branch yang tracking remote branch, lalu push perubahan.
Solusi:
git checkout -b side o/main
git commit
git pull --rebase
git push
Info: Di LGB,
o/mainadalah singkatan untukorigin/main. Perintahgit checkout -b side o/mainmembuat branchsideyang melacakorigin/main.
Latihan Terminal: Sinkronisasi Fork
# Simulasi: Buat 2 repo untuk simulasi fork workflow
# Repo "asli" (upstream)
mkdir repo-asli && cd repo-asli && git init --bare
cd ..
# Repo "fork" (origin)
git clone repo-asli repo-fork
cd repo-fork
# Simulasikan perubahan di upstream
cd ../repo-asli
git clone . ../repo-upstream-work
cd ../repo-upstream-work
echo "Update dari upstream" > upstream-update.txt
git add . && git commit -m "feat: update dari maintainer"
git push origin main
# Kembali ke fork dan sync
cd ../repo-fork
git remote add upstream ../repo-asli
git fetch upstream
git merge upstream/main
git log --oneline
# Buat kontribusi
git switch -c feature/my-contribution
echo "Kontribusi saya" > my-file.txt
git add . && git commit -m "feat: tambah kontribusi"
git push origin feature/my-contribution
Tugas Mandiri
- Fork sebuah repository publik di GitHub (boleh repo teman atau project open source kecil)
- Clone fork, tambahkan upstream remote, dan sinkronisasi
- Buat branch fitur, lakukan perubahan, dan push ke fork
- Buat Pull Request dari fork ke repository asli
- Praktikkan
git fetch --alldan perhatikan perbedaanorigin/maindanupstream/main
Praktik Interaktif
Selesaikan level berikut untuk memperkuat pemahaman kamu.