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 #
# Debian/Ubuntu
sudo apt install -y git
# Fedora
sudo dnf install -y git
# Arch
sudo pacman -S --needed gitmacOS #
Git disertakan Xcode Command Line Tools. Bila belum:
xcode-select --installAtau pasang via Homebrew untuk versi terbaru:
brew install gitWindows #
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 #
git --versionOutput yang Dihasilkan:
git version 2.47.32. Konfigurasi Awal #
Sebelum menggunakan git pertama kali, set identitas global:
git config --global user.name "Budi Developer"
git config --global user.email "[email protected]"Beberapa konfigurasi berguna lainnya:
# 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 nanoVerifikasi konfigurasi:
git config --list --globalOutput yang Dihasilkan (potongan):
user.name=Budi Developer
[email protected]
init.defaultbranch=main
color.ui=auto
pull.rebase=false3. Inisialisasi Repository Baru #
Buat folder proyek dan inisialisasi git:
mkdir belajar-c
cd belajar-c
git initOutput yang Dihasilkan:
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:
# 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:
/* File: util.h */
#ifndef UTIL_H
#define UTIL_H
void greet(void);
#endif/* File: util.c */
#include <stdio.h>
#include "util.h"
void greet(void) {
printf("Halo dari git project\n");
}/* File: main.c */
#include "util.h"
int main(void) {
greet();
return 0;
}# 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:
git statusOutput yang Dihasilkan:
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.hTambah semua file ke staging area:
git add .Verifikasi apa yang akan di-commit:
git statusOutput yang Dihasilkan:
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.hCommit:
git commit -m "Initial commit: proyek C dengan Makefile"Output yang Dihasilkan:
[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.hVerifikasi dengan git log:
git log --onelineOutput yang Dihasilkan:
c68c0c5 Initial commit: proyek C dengan Makefile7. Workflow Sehari-hari #
Edit File, Build, Test #
# Edit util.c, tambah fungsi baru
vi util.c
# Build dan test
make
./app
# Bersihkan artifact build (jangan di-commit!)
make cleanStage dan Commit Perubahan #
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 #
# 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 --allCabang (Branch) untuk Fitur Baru #
# 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/logger8. Commit Message yang Baik #
Format commit message yang direkomendasikan (Conventional Commits):
<type>(<scope>): <subject>
<body>
<footer>| Type | Kegunaan |
|---|---|
feat | Fitur baru |
fix | Perbaikan bug |
docs | Dokumentasi |
style | Formatting, whitespace |
refactor | Refactoring tanpa ubah perilaku |
test | Tambah/ubah test |
chore | Maintenance, build config |
perf | Performance improvement |
Contoh:
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: #429. Integrasi dengan Remote (GitHub/GitLab) #
# 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 pull10. 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:
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:
git config --global core.autocrlf input # Linux/macOS
git config --global core.autocrlf true # WindowsTambahkan file .gitattributes di root proyek:
* text=auto
*.c text
*.h text
Makefile textLupa commit perubahan sebelum pull #
git stash # simpan perubahan sementara
git pull
git stash pop # kembalikan perubahanCommit ke branch yang salah #
# 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 "..."