Git adalah sistem version control yang wajib dipakai untuk setiap proyek C—mulai dari skrip satu file hingga library besar. Version control memungkinkan kita melacak perubahan, berkolaborasi tanpa konflik, dan kembali ke versi lama bila bug ditemukan. Bab ini menuntun kita menginisialisasi repository, membuat .gitignore untuk proyek C, dan menulis commit message yang baik.

1. Pemasangan Git #

Linux #

bash
# Debian/Ubuntu
sudo apt install -y git

# Fedora
sudo dnf install -y git

# Arch
sudo pacman -S --needed git

macOS #

Git disertakan Xcode Command Line Tools. Bila belum:

bash
xcode-select --install

Atau pasang via Homebrew untuk versi terbaru:

bash
brew install git

Windows #

Download installer dari https://git-scm.com/download/win. Saat instalasi, pilih opsi Git Bash yang memberi terminal Unix-like dengan git, ssh, dan bash di Windows.

Verifikasi #

bash
git --version

Output yang Dihasilkan:

text
git version 2.47.3

2. Konfigurasi Awal #

Sebelum menggunakan git pertama kali, set identitas global:

bash
git config --global user.name "Budi Developer"
git config --global user.email "[email protected]"

Beberapa konfigurasi berguna lainnya:

bash
# Default branch utama bernama 'main' (bukan 'master')
git config --global init.defaultBranch main

# Warna output otomatis
git config --global color.ui auto

# Pull dengan rebase (lebih bersih dari merge)
git config --global pull.rebase false

# Simpan credential HTTPS (HTTPS push tanpa password berulang)
git config --global credential.helper store

# Editor untuk commit message
git config --global core.editor nano

Verifikasi konfigurasi:

bash
git config --list --global

Output yang Dihasilkan (potongan):

text
user.name=Budi Developer
[email protected]
init.defaultbranch=main
color.ui=auto
pull.rebase=false

3. Inisialisasi Repository Baru #

Buat folder proyek dan inisialisasi git:

bash
mkdir belajar-c
cd belajar-c
git init

Output yang Dihasilkan:

text
Initialized empty Git repository in /home/budi/belajar-c/.git/

4. Membuat .gitignore untuk Proyek C #

File .gitignore mencegah artifact build, binary, dan file editor ter-commit. Buat file .gitignore:

text
# Object files
*.o
*.obj
*.ko
*.pyc

# Static libraries
*.a
*.lib

# Shared libraries
*.so
*.so.*
*.dylib
*.dll

# Executables
app
*.exe
*.out

# Build directories
build/
cmake-build-*/

# CMake generated
CMakeCache.txt
CMakeFiles/
cmake_install.cmake
Makefile
!Makefile

# Compile commands
compile_commands.json

# Editor
.vscode/
.idea/
*.swp
*.swo
*~

# OS
.DS_Store
Thumbs.db
desktop.ini

# Debug files
*.dSYM/
*.pdb
core
core.*

5. Struktur Proyek C yang Baik #

Buat beberapa file source untuk demo:

c
/* File: util.h */
#ifndef UTIL_H
#define UTIL_H
void greet(void);
#endif
c
/* File: util.c */
#include <stdio.h>
#include "util.h"
void greet(void) {
    printf("Halo dari git project\n");
}
c
/* File: main.c */
#include "util.h"
int main(void) {
    greet();
    return 0;
}
makefile
# File: Makefile
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -g
TARGET = app
OBJS = main.o util.o

all: $(TARGET)

$(TARGET): $(OBJS)
	$(CC) -o $@ $^

%.o: %.c
	$(CC) $(CFLAGS) -c -o $@ $<

clean:
	rm -f $(OBJS) $(TARGET)

6. Commit Pertama #

Cek status repository:

bash
git status

Output yang Dihasilkan:

text
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        Makefile
        main.c
        util.c
        util.h

Tambah semua file ke staging area:

bash
git add .

Verifikasi apa yang akan di-commit:

bash
git status

Output yang Dihasilkan:

text
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   .gitignore
        new file:   Makefile
        new file:   main.c
        new file:   util.c
        new file:   util.h

Commit:

bash
git commit -m "Initial commit: proyek C dengan Makefile"

Output yang Dihasilkan:

text
[main (root-commit) c68c0c5] Initial commit: proyek C dengan Makefile
 5 files changed, 49 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 main.c
 create mode 100644 util.c
 create mode 100644 util.h

Verifikasi dengan git log:

bash
git log --oneline

Output yang Dihasilkan:

text
c68c0c5 Initial commit: proyek C dengan Makefile

7. Workflow Sehari-hari #

Edit File, Build, Test #

bash
# Edit util.c, tambah fungsi baru
vi util.c

# Build dan test
make
./app

# Bersihkan artifact build (jangan di-commit!)
make clean

Stage dan Commit Perubahan #

bash
git status               # lihat apa berubah
git diff                 # lihat detail perubahan
git add util.c util.h    # stage file yang berubah
git commit -m "Tambah fungsi calculate_average"

Lihat Riwayat Commit #

bash
# Ringkas
git log --oneline

# Detail dengan diff
git log -p

# Statistik perubahan per commit
git log --stat

# Tampilan grafis untuk branch merge
git log --graph --oneline --all

Cabang (Branch) untuk Fitur Baru #

bash
# Buat dan pindah ke branch baru
git checkout -b fitur/logger

# Setelah selesai, gabungkan ke main
git checkout main
git merge fitur/logger

# Hapus branch yang sudah digabung
git branch -d fitur/logger

8. Commit Message yang Baik #

Format commit message yang direkomendasikan (Conventional Commits):

text
<type>(<scope>): <subject>

<body>

<footer>
TypeKegunaan
featFitur baru
fixPerbaikan bug
docsDokumentasi
styleFormatting, whitespace
refactorRefactoring tanpa ubah perilaku
testTambah/ubah test
choreMaintenance, build config
perfPerformance improvement

Contoh:

text
feat(logger): tambah level filtering berdasarkan environment

Logger sekarang membaca env var LOG_LEVEL saat startup untuk
menentukan level minimum yang dicetak. Default: INFO.

Closes: #42

9. Integrasi dengan Remote (GitHub/GitLab) #

bash
# Tambah remote (ganti URL dengan repository Anda)
git remote add origin [email protected]:budi/belajar-c.git

# Push pertama dengan -u untuk tracking
git push -u origin main

# Selanjutnya cukup
git push

# Ambil perubahan dari remote
git pull

10. Troubleshooting #

git: error: cannot run code #

PATH tidak lengkap atau git binary corrupt. Reinstall git.

fatal: not a git repository #

Folder belum di-init. Jalankan git init di root proyek.

File yang seharusnya di-ignore masih muncul #

File sudah ter-track sebelum .gitignore ditambah. Hapus dari index:

bash
git rm --cached build/
git rm --cached *.o
git commit -m "Hapus artifact build dari tracking"

git diff menampilkan perubahan yang tidak kita buat #

Line ending (CRLF vs LF) berbeda antara developer. Konfigurasi git untuk otomatis normalisasi:

bash
git config --global core.autocrlf input   # Linux/macOS
git config --global core.autocrlf true     # Windows

Tambahkan file .gitattributes di root proyek:

text
* text=auto
*.c text
*.h text
Makefile text

Lupa commit perubahan sebelum pull #

bash
git stash       # simpan perubahan sementara
git pull
git stash pop   # kembalikan perubahan

Commit ke branch yang salah #

bash
# Batal commit terakhir (file tetap di staging)
git reset --soft HEAD~1

# Pindah ke branch yang benar
git checkout fitur/logger

# Commit ulang
git commit -m "..."