Git adalah version control system yang wajib dikuasai developer. Mari pelajari dari dasar.
Install Git
Linux
# Ubuntu/Debian
sudo apt update
sudo apt install git
sudo dnf install git
Verify installation
git --version
Windows & Mac
# Windows: Download dari git-scm.com
# Mac: Install via Homebrew
brew install git
Konfigurasi Awal
Set Identity
# Set nama dan email
git config --global user.name "Nama Anda"
git config --global user.email "[email protected]"
Set default branch name
git config --global init.defaultBranch main
Lihat semua config
git config --list
Set Editor
# Set VS Code sebagai editor
git config --global core.editor "code --wait"
Set Nano
git config --global core.editor "nano"
Basic Git Commands
Initialize Repository
# Buat folder project
mkdir my-project
cd my-project
Initialize git
git init
Check status
git status
Stage and Commit
# Buat file
echo "Hello World" > README.md
Stage file
git add README.md
Stage semua file
git add .
Commit changes
git commit -m "Initial commit"
Lihat history
git log
git log --oneline
Working with Branches
Create and Switch Branch
# Buat branch baru
git branch feature-login
Switch ke branch
git checkout feature-login
Atau buat dan switch sekaligus
git checkout -b feature-login
Git 2.23+ syntax
git switch -c feature-login
List semua branch
git branch
git branch -a
Merge Branch
# Switch ke main
git checkout main
Merge feature branch
git merge feature-login
Delete branch setelah merge
git branch -d feature-login
Remote Repository (GitHub)
Connect to GitHub
# Add remote
git remote add origin https://github.com/username/repo.git
Verify remote
git remote -v
Push ke GitHub
git push -u origin main
Clone Repository
# Clone via HTTPS
git clone https://github.com/username/repo.git
Clone via SSH
git clone [email protected]:username/repo.git
Clone ke folder tertentu
git clone
https://github.com/username/repo.git my-folder
SSH Authentication
Generate SSH Key
# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"
Start SSH agent
eval "$(ssh-agent -s)"
Add key ke agent
ssh-add ~/.ssh/id_ed25519
Copy public key
cat ~/.ssh/id_ed25519.pub
Paste ke GitHub > Settings > SSH Keys
Test SSH Connection
# Test connection
ssh -T [email protected]
# Should see: Hi username! You've successfully authenticated
Pull and Push
Sync with Remote
# Fetch updates (tidak merge)
git fetch origin
Pull updates (fetch + merge)
git pull origin main
Push changes
git push origin main
Push new branch
git push -u origin feature-branch
Handling Merge Conflicts
Resolve Conflicts
# Pull may cause conflict
git pull origin main
Edit conflicted files
Look for:
<<<<<<< HEAD
Your changes
=======
Their changes
>>>>>>> branch-name
After resolving
git add .
git commit -m "Resolve merge conflicts"
Useful Git Commands
View Changes
# Lihat perubahan unstaged
git diff
Lihat perubahan staged
git diff --staged
Lihat history dengan graph
git log --oneline --graph --all
Undo Changes
# Undo unstaged changes
git checkout -- filename
git restore filename
Unstage file
git reset HEAD filename
git restore --staged filename
Undo last commit (keep changes)
git reset --soft HEAD~1
Undo last commit (discard changes)
git reset --hard HEAD~1
Stash Changes
# Simpan perubahan sementara
git stash
List stash
git stash list
Apply stash
git stash pop
Apply specific stash
git stash apply stash@{0}
.gitignore File
Create .gitignore
# .gitignore
node_modules/
.env
.env.local
*.log
dist/
build/
.DS_Store
*.pyc
__pycache__/
.vscode/
Ignore Already Tracked Files
# Remove from tracking but keep file
git rm --cached filename
Remove folder
git rm -r --cached foldername
GitHub Features
Pull Request Workflow
# 1. Fork repository di GitHub
# 2. Clone fork
git clone https://github.com/your-username/repo.git
3. Add upstream remote
git remote add upstream
https://github.com/original/repo.git
4. Create feature branch
git checkout -b my-feature
5. Make changes and commit
git add .
git commit -m "Add new feature"
6. Push ke fork
git push origin my-feature
7. Create Pull Request di GitHub
Sync Fork
# Fetch upstream
git fetch upstream
Merge upstream/main
git checkout main
git merge upstream/main
Push to fork
git push origin main
Git Aliases
Create Shortcuts
# Common aliases
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
Usage
git co main
git st
git lg
Kesimpulan
Git dan GitHub adalah tools essential untuk developer. Mulai dengan basic commands lalu pelajari branching dan collaboration workflow.