Git adalah version control system yang essential untuk developer. Mari pelajari cara setup dan menggunakannya dengan GitHub.
Install Git
Linux (Ubuntu/Debian)
# Update package list
sudo apt update
sudo apt install git
Verify installation
git --version
Windows
# Download dari https://git-scm.com/download/win
# Atau gunakan winget
winget install Git.Git
macOS
# Install via Homebrew
brew install git
Atau Xcode Command Line Tools
xcode-select --install
Initial Configuration
Set Identity
# Set username
git config --global user.name "Nama Anda"
Set email (sama dengan GitHub)
git config --global user.email "[email protected]"
Verify configuration
git config --list
Configure Defaults
# Set default branch name
git config --global init.defaultBranch main
Set default editor
git config --global core.editor "code --wait"
Enable colored output
git config --global color.ui auto
Setup GitHub Account
Create SSH Key
# Generate SSH key
ssh-keygen -t ed25519 -C "[email protected]"
Start SSH agent
eval "$(ssh-agent -s)"
Add key to agent
ssh-add ~/.ssh/id_ed25519
Copy public key
cat ~/.ssh/id_ed25519.pub
Add SSH Key to GitHub
1. GitHub > Settings > SSH and GPG keys
2. Click "New SSH key"
3. Paste public key
4. Save
Test Connection
# Test SSH connection
ssh -T [email protected]
Should see: "Hi username! You've successfully authenticated..."
Basic Git Workflow
Initialize Repository
# Create project directory
mkdir myproject
cd myproject
Initialize Git
git init
Check status
git status
Stage and Commit
# Create a file
echo "# My Project" > README.md
Stage file
git add README.md
Stage all files
git add .
Commit with message
git commit -m "Initial commit"
View History
# View commit history
git log
Compact view
git log --oneline
With graph
git log --oneline --graph --all
Working with Remote
Add Remote Repository
# Create repo on GitHub first, then:
git remote add origin [email protected]:username/repo.git
Verify remote
git remote -v
Push to GitHub
# Push to main branch
git push -u origin main
Subsequent pushes
git push
Clone Repository
# Clone via SSH
git clone [email protected]:username/repo.git
Clone via HTTPS
git clone
https://github.com/username/repo.git
Clone to specific folder
git clone [email protected]:username/repo.git myfolder
Branching
Create and Switch Branches
# Create new branch
git branch feature-login
Switch to branch
git checkout feature-login
Create and switch in one command
git checkout -b feature-signup
Modern way (Git 2.23+)
git switch -c feature-dashboard
List Branches
# List local branches
git branch
List all branches (including remote)
git branch -a
List remote branches
git branch -r
Merge Branches
# Switch to main
git checkout main
Merge feature branch
git merge feature-login
Delete merged branch
git branch -d feature-login
Pull Requests on GitHub
Create Pull Request
# Push feature branch
git push -u origin feature-login
Then on GitHub:
1. Go to repository
2. Click "Compare & pull request"
3. Add description
4. Request reviewers
5. Create pull request
Sync with Main
# Fetch latest changes
git fetch origin
Merge main into feature branch
git checkout feature-login
git merge origin/main
Or rebase
git rebase origin/main
Common Scenarios
Undo Changes
# Discard changes in working directory
git checkout -- filename
Unstage file
git reset HEAD filename
Undo last commit (keep changes)
git reset --soft HEAD~1
Undo last commit (discard changes)
git reset --hard HEAD~1
Stash Changes
# Stash current changes
git stash
List stashes
git stash list
Apply latest stash
git stash pop
Apply specific stash
git stash apply stash@{0}
Resolve Conflicts
# When merge conflict occurs:
# 1. Open conflicted file
# 2. Look for conflict markers
# <<<<<<< HEAD
# your changes
# =======
# their changes
# >>>>>>> branch-name
3. Edit to resolve
4. Stage and commit
git add .
git commit -m "Resolve merge conflict"
Git Ignore
Create .gitignore
# Create .gitignore file
cat > .gitignore << 'EOF'
# Dependencies
node_modules/
vendor/
Build output
dist/
build/
Environment files
.env
.env.local
IDE
.vscode/
.idea/
OS files
.DS_Store
Thumbs.db
Logs
*.log
EOF
Useful Aliases
Configure Aliases
# Short status
git config --global alias.st status
Short commit
git config --global alias.co checkout
Log graph
git config --global alias.lg "log --oneline --graph --all"
Undo last commit
git config --global alias.undo "reset --soft HEAD~1"
Best Practices
Commit Messages
Good commit message format:
- Short summary (50 chars)
- Blank line
- Detailed explanation (optional)
Example:
"Add user authentication
- Implement JWT tokens
- Add login/logout endpoints
- Create auth middleware"
Branch Naming
Conventions:
- feature/user-login
- bugfix/header-alignment
- hotfix/security-patch
- release/v1.0.0
Kesimpulan
Git dan GitHub adalah essential tools untuk development. Mulai dengan basic commands dan gradually learn advanced features seperti branching dan pull requests.
Ditulis oleh
Hendra Wijaya