Git dan GitHub untuk Pemula: Panduan Lengkap 2024

Git dan GitHub untuk Pemula: Panduan Lengkap 2024
Git adalah skill wajib untuk setiap developer. Di artikel ini, kita akan belajar Git dan GitHub dari nol hingga bisa digunakan sehari-hari.
Git vs GitHub: Apa Bedanya?
| Git | GitHub | |-----|--------| | Software | Platform/Website | | Version control system | Hosting untuk Git repository | | Diinstall di komputer | Diakses via browser | | Offline | Online | | Gratis | Free & Paid plans |
Install Git
Windows
Download dari git-scm.com
Mac
brew install git
Linux
sudo apt install git # Ubuntu/Debian
sudo yum install git # CentOS/RHEL
Verifikasi
git --version
# Output: git version 2.42.0
Konfigurasi Awal
# Set nama dan email
git config --global user.name "Nama Anda"
git config --global user.email "email@anda.com"
# Cek konfigurasi
git config --list
Perintah Dasar Git
1. Initialize Repository
# Buat folder project
mkdir my-project
cd my-project
# Initialize git
git init
# Output: Initialized empty Git repository in /my-project/.git/
2. Status dan Add
# Cek status
git status
# Add file tertentu
git add filename.js
# Add semua file
git add .
# Add file dengan pattern
git add *.js
3. Commit
# Commit dengan message
git commit -m "Menambahkan fitur login"
# Commit dengan deskripsi panjang
git commit -m "Feat: Add user authentication" -m "
- Implement login endpoint
- Add JWT token generation
- Create password hashing"
4. Melihat History
# Log lengkap
git log
# Log singkat
git log --oneline
# Log dengan graph
git log --oneline --graph
Branching
Branch memungkinkan kita bekerja di fitur berbeda tanpa mengganggu kode utama.
# Lihat semua branch
git branch
# Buat branch baru
git branch feature-login
# Pindah ke branch
git checkout feature-login
# Buat dan pindah sekaligus
git checkout -b feature-register
# Kembali ke main
git checkout main
Merge Branch
# Pastikan di branch main
git checkout main
# Merge feature branch
git merge feature-login
# Hapus branch setelah merge
git branch -d feature-login
Bekerja dengan GitHub
1. Buat Repository di GitHub
- Login ke GitHub
- Klik "New repository"
- Isi nama dan deskripsi
- Klik "Create repository"
2. Connect Local ke Remote
# Tambah remote origin
git remote add origin https://github.com/username/repo-name.git
# Verifikasi
git remote -v
3. Push ke GitHub
# Push pertama kali
git push -u origin main
# Push selanjutnya
git push
4. Clone Repository
git clone https://github.com/username/repo-name.git
5. Pull Updates
# Download dan merge
git pull origin main
# Download saja (tanpa merge)
git fetch origin
Workflow yang Direkomendasikan
Feature Branch Workflow
main
│
├── feature/login
│ ├── commit 1
│ └── commit 2
│
├── feature/register
│ └── commit 1
│
└── hotfix/critical-bug
Step-by-Step
# 1. Selalu update main dulu
git checkout main
git pull origin main
# 2. Buat feature branch
git checkout -b feature/user-profile
# 3. Kerjakan dan commit
git add .
git commit -m "Add user profile page"
# 4. Push branch
git push -u origin feature/user-profile
# 5. Buat Pull Request di GitHub
# 6. Setelah di-merge, hapus branch local
git checkout main
git pull origin main
git branch -d feature/user-profile
Tips dan Best Practices
Commit Message yang Baik
# ❌ Bad
git commit -m "fix"
git commit -m "update"
git commit -m "asdfgh"
# ✅ Good
git commit -m "Fix: login button not responding on mobile"
git commit -m "Feat: add user avatar upload"
git commit -m "Docs: update README with setup instructions"
Format Commit Message
<type>: <subject>
<body> (optional)
<footer> (optional)
Types:
feat: Fitur barufix: Bug fixdocs: Dokumentasistyle: Formattingrefactor: Refactoring kodetest: Testingchore: Maintenance
.gitignore
File yang tidak perlu di-track:
# Dependencies
node_modules/
# Environment variables
.env
.env.local
# Build output
dist/
build/
# IDE
.vscode/
.idea/
# OS files
.DS_Store
Thumbs.db
# Logs
*.log
Cheat Sheet
| Command | Deskripsi |
|---------|-----------|
| git init | Initialize repository |
| git status | Cek status |
| git add . | Stage semua file |
| git commit -m "msg" | Commit changes |
| git push | Upload ke remote |
| git pull | Download dari remote |
| git branch | List branches |
| git checkout -b name | Buat & pindah branch |
| git merge branch | Merge branch |
| git log --oneline | Lihat history |
Kesimpulan
Git adalah tool essential yang harus dikuasai setiap developer. Mulai dengan command basic, dan gradually pelajari fitur advanced seperti rebasing, stashing, dan cherry-pick.
Praktik setiap hari, dan dalam beberapa minggu Anda akan comfortable menggunakan Git untuk semua project.
Happy coding! 🚀
Share this article