Pertemuan Pertemuan 29

Submodules, Worktrees, LFS & Git Internals

Pertemuan 29 / 32
Advanced Modul 3.4-3.5 60 menit
Pertemuan 29

Submodules, Worktrees, LFS & Git Internals

Tujuan Pembelajaran

  • Memahami dan menggunakan Git submodules
  • Mengenal Git worktrees untuk bekerja di banyak branch bersamaan
  • Memahami Git LFS untuk file besar
  • Mengenal Git internals: objects, refs, dan HEAD

Materi Inti

A. Git Submodules

Submodule memungkinkan kamu menyertakan repository lain di dalam repository utama. Cocok untuk library atau komponen yang dikembangkan secara terpisah.

# Tambah submodule
git submodule add https://github.com/user/library.git libs/library

# Hasilnya:
# - Folder libs/library/ berisi repo library
# - File .gitmodules berisi konfigurasi submodule
# - Git mencatat commit hash spesifik dari library

Clone repo yang punya submodule:

# Cara 1: Clone sekaligus dengan submodule
git clone --recurse-submodules https://github.com/user/project.git

# Cara 2: Clone dulu, init submodule kemudian
git clone https://github.com/user/project.git
cd project
git submodule update --init --recursive

Update submodule ke versi terbaru:

# Masuk ke folder submodule dan pull
cd libs/library
git pull origin main

# Kembali ke repo utama, commit perubahan
cd ../..
git add libs/library
git commit -m "chore: update library submodule"

Kapan menggunakan submodule:

  • Project bergantung pada library yang dikembangkan terpisah
  • Beberapa project menggunakan komponen yang sama
  • Ingin mengunci versi spesifik dari dependency

Tips: Submodule bisa membingungkan untuk pemula. Alternatif yang lebih sederhana termasuk npm packages atau Git subtree.

B. Git Worktrees

Worktree memungkinkan kamu bekerja di beberapa branch bersamaan tanpa perlu stash atau commit dulu. Setiap worktree adalah folder terpisah yang terhubung ke repository yang sama.

# Buat worktree baru untuk branch hotfix
git worktree add ../hotfix-dir hotfix/urgent

# Sekarang kamu bisa:
# - Bekerja di folder utama (branch main)
# - Bekerja di folder ../hotfix-dir (branch hotfix/urgent)
# - Kedua folder berbagi .git yang sama!

# Daftar semua worktree
git worktree list
# /path/to/project        abc1234 [main]
# /path/to/hotfix-dir     def5678 [hotfix/urgent]

# Hapus worktree setelah selesai
git worktree remove ../hotfix-dir

Kapan menggunakan worktree:

  • Perlu memperbaiki bug urgent sambil mengerjakan fitur
  • Ingin membandingkan 2 branch secara visual
  • Menjalankan test di branch berbeda bersamaan

C. Git LFS (Large File Storage)

Git tidak efisien untuk file besar (gambar, video, dataset). Git LFS menyimpan file besar di server terpisah dan hanya menyimpan pointer di repository.

# Install Git LFS
git lfs install

# Track file besar berdasarkan ekstensi
git lfs track "*.psd"
git lfs track "*.mp4"
git lfs track "*.zip"

# File .gitattributes otomatis dibuat
cat .gitattributes
# *.psd filter=lfs diff=lfs merge=lfs -text
# *.mp4 filter=lfs diff=lfs merge=lfs -text

# Commit dan push seperti biasa
git add .gitattributes
git add gambar-besar.psd
git commit -m "tambah file design"
git push origin main

D. Git Internals — Bagaimana Git Bekerja

Di balik layar, Git adalah content-addressable filesystem. Semua data disimpan sebagai objects yang di-hash dengan SHA-1.

4 tipe Git object:

ObjectFungsiAnalogi
BlobMenyimpan konten fileIsi file
TreeMenyimpan struktur direktoriDaftar isi folder
CommitMenyimpan snapshot + metadataFoto + keterangan
TagPointer ke commit (annotated)Label pada foto
# Lihat tipe object
git cat-file -t HEAD
# commit

# Lihat isi object commit
git cat-file -p HEAD
# tree abc123
# parent def456
# author Nama <email> timestamp
# committer Nama <email> timestamp
#
# Pesan commit

# Lihat isi tree (struktur direktori)
git cat-file -p HEAD^{tree}
# 100644 blob abc123  index.html
# 100644 blob def456  style.css
# 040000 tree ghi789  src

# Branch hanyalah file teks berisi hash!
cat .git/refs/heads/main
# abc123def456...

# HEAD menunjuk ke branch aktif
cat .git/HEAD
# ref: refs/heads/main

Pemahaman penting:

  • Branch = hanya pointer (file teks berisi hash commit)
  • HEAD = pointer ke branch aktif (atau langsung ke commit jika detached)
  • Commit = snapshot lengkap, bukan diff
  • Setiap object memiliki hash SHA-1 yang unik

Demonstrasi Live

Eksplorasi Git Internals

mkdir latihan-internals && cd latihan-internals && git init
echo "Hello Git" > hello.txt
git add . && git commit -m "init"

# Eksplorasi internal
cat .git/HEAD
# ref: refs/heads/main (atau master)

cat .git/refs/heads/main
# hash commit terbaru

git cat-file -p HEAD
# Lihat isi commit: tree, author, message

git cat-file -p HEAD^{tree}
# Lihat isi tree: blob hello.txt

# Lihat isi blob (konten file)
git cat-file -p <blob-hash>
# Hello Git

Setup Git Aliases

# Buat alias yang berguna
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.st "status -sb"
git config --global alias.co "checkout"
git config --global alias.br "branch"
git config --global alias.last "log -1 HEAD"
git config --global alias.unstage "reset HEAD --"

# Test alias
git lg
git st
git last

Latihan Praktik

Latihan Terminal: Eksplorasi Git Internals

# Gunakan project yang sudah punya beberapa commit
cd proyek-website  # atau project kamu

# Langkah 1: Lihat HEAD dan refs
cat .git/HEAD
cat .git/refs/heads/main

# Langkah 2: Eksplorasi object
git cat-file -p HEAD
git cat-file -p HEAD^{tree}

# Langkah 3: Ikuti chain dari commit -> tree -> blob
# Catat hash dari tree
# Lihat isi tree, catat hash dari blob
# Lihat isi blob

# Langkah 4: Buat alias favorit kamu
git config --global alias.lg "log --oneline --graph --all --decorate"
git config --global alias.st "status -sb"

# Test
git lg
git st

Latihan Worktree

mkdir latihan-worktree && cd latihan-worktree && git init
echo "main content" > main.txt && git add . && git commit -m "init"

# Buat branch fitur
git switch -c feature/new-page
echo "new page" > page.html && git add . && git commit -m "feat: new page"
git switch main

# Buat worktree untuk hotfix
git worktree add ../hotfix-work hotfix/urgent -b
echo "hotfix applied" > fix.txt
cd ../hotfix-work
git add . && git commit -m "fix: urgent hotfix"

# Kembali ke main, lihat daftar worktree
cd ../latihan-worktree
git worktree list

# Merge hotfix
git merge hotfix/urgent

# Hapus worktree
git worktree remove ../hotfix-work

Tugas Mandiri

  1. Eksplorasi Git internals di salah satu project kamu: ikuti chain commit -> tree -> blob
  2. Buat minimal 5 Git alias yang berguna dan simpan di global config
  3. Praktikkan worktree: buat 2 worktree untuk 2 branch berbeda, kerjakan sesuatu di masing-masing
  4. (Opsional) Setup Git LFS di project yang memiliki file gambar besar
  5. Baca: https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain