Master Version Control seperti Pro Developer
Git & GitHub adalah essential tools untuk setiap developer. Tutorial ini akan transform Anda dari zero to hero.
Apa Itu Git & GitHub?
Git:
- Version control system
- Track changes dalam code
- Collaborate dengan tim
- Revert ke previous versions
GitHub:
- Cloud platform untuk host Git repositories
- Collaboration features
- Portfolio untuk developers
- Open-source projects
Installation
Windows:
- Download dari git-scm.com
- Run installer (default settings OK)
- Verify: Open cmd ?
git --version
Mac:
brew install git
Linux:
sudo apt-get install git
Initial Configuration
git config --global user.name "Your Name"\ngit config --global user.email "your@email.com"\ngit config --global init.defaultBranch main
Check config:
git config --list
Basic Git Workflow
1. Initialize Repository
Create new project:
mkdir my-project\ncd my-project\ngit init
Or clone existing:
git clone https://github.com/username/repo.git
2. Check Status
git status
Shows modified files, staging area, current branch
3. Stage Changes
# Stage specific file\ngit add filename.txt\n\n# Stage all changes\ngit add .\n\n# Stage all .js files\ngit add *.js
4. Commit Changes
git commit -m "Your descriptive commit message"
Good commit messages:
- "Add login functionality"
- "Fix navbar responsive issue"
- "Update README with installation steps"
Bad: "update", "fix", "changes"
5. View History
# See commit history\ngit log\n\n# Compact view\ngit log --oneline\n\n# Graph view\ngit log --graph --oneline --all
Working with Branches
Why Branches?
- Develop features independently
- Experiment without affecting main code
- Multiple people work simultaneously
Common Commands:
# Create new branch\ngit branch feature-name\n\n# Switch to branch\ngit checkout feature-name\n\n# Create & switch (shortcut)\ngit checkout -b feature-name\n\n# List all branches\ngit branch\n\n# Delete branch\ngit branch -d feature-name
Best Practice Branch Names:
- feature/user-authentication
- bugfix/login-error
- hotfix/security-patch
Merging Branches
# Switch to main branch\ngit checkout main\n\n# Merge feature branch\ngit merge feature-name
Merge Conflicts:
Terjadi saat same line diubah di different branches
Resolution:
- Git akan mark conflicts di file
- Manually edit file, choose which changes to keep
- Remove conflict markers (<<<, ===, >>>)
git add .git commit -m "Resolve merge conflict"
Working with GitHub
1. Create GitHub Account
github.com ? Sign up
2. Create Repository
- Click "New repository"
- Enter name & description
- Public atau Private
- Add README (optional)
- Click "Create repository"
3. Connect Local to GitHub
# Add remote\ngit remote add origin https://github.com/username/repo.git\n\n# Push to GitHub\ngit push -u origin main
Future pushes:
git push
4. Pull Changes
# Fetch & merge changes from GitHub\ngit pull
Collaborative Workflow
Fork & Pull Request:
- Fork: Copy repository ke your account
- Clone: Download ke local
- Branch: Create feature branch
- Changes: Make modifications
- Commit: Commit changes
- Push: Push ke your fork
- Pull Request: Propose changes ke original repo
Useful Git Commands
Undo Changes:
# Unstage file\ngit reset filename.txt\n\n# Discard changes (CAREFUL!)\ngit checkout -- filename.txt\n\n# Undo last commit (keep changes)\ngit reset --soft HEAD~1\n\n# Undo last commit (discard changes)\ngit reset --hard HEAD~1
Stash Changes:
# Save changes temporarily\ngit stash\n\n# List stashes\ngit stash list\n\n# Apply stash\ngit stash apply\n\n# Apply & remove stash\ngit stash pop
View Differences:
# Changes not staged\ngit diff\n\n# Changes staged\ngit diff --staged\n\n# Between branches\ngit diff branch1 branch2
GitHub Features
1. README.md
Project documentation dalam Markdown
2. Issues
- Track bugs
- Feature requests
- Discussion
3. Pull Requests
- Propose changes
- Code review
- Discussion before merge
4. GitHub Actions
- CI/CD automation
- Run tests automatically
- Deploy on push
5. GitHub Pages
- Free website hosting
- From repository
.gitignore File
Specify files Git should ignore
Common entries:
# Dependencies\nnode_modules/\nvendor/\n\n# Environment variables\n.env\n\n# IDE\n.vscode/\n.idea/\n\n# OS\n.DS_Store\nThumbs.db\n\n# Build\ndist/\nbuild/
Git Best Practices
- Commit often: Small, focused commits
- Descriptive messages: Explain what & why
- Pull before push: Avoid conflicts
- Branch for features: Keep main stable
- Review before merge: Code quality
- Don't commit sensitive data: Use .gitignore
Common Workflows
Daily Workflow:
# Start of day\ngit pull\n\n# Create feature branch\ngit checkout -b feature/new-thing\n\n# Make changes, then\ngit add .\ngit commit -m "Add new feature"\n\n# Push to GitHub\ngit push origin feature/new-thing\n\n# Create Pull Request on GitHub\n# After approval, merge\n\n# Switch back & update\ngit checkout main\ngit pull
Hotfix Workflow:
# From main\ngit checkout -b hotfix/critical-bug\n\n# Fix bug\ngit add .\ngit commit -m "Fix critical bug"\n\n# Push & create PR\ngit push origin hotfix/critical-bug
Troubleshooting
Forgot to branch:
# Stash changes\ngit stash\n\n# Create & switch branch\ngit checkout -b feature-branch\n\n# Apply stashed changes\ngit stash pop
Pushed wrong commit:
# Revert commit (creates new commit)\ngit revert HEAD\ngit push
Merge conflict:
# Abort merge\ngit merge --abort\n\n# Or resolve manually & commit
Learning Resources
- Git Documentation: git-scm.com/doc
- GitHub Learning Lab: lab.github.com
- Interactive tutorial: learngitbranching.js.org
- Visualize Git: git-school.github.io/visualizing-git
Kesimpulan
Git & GitHub are essential untuk modern development. Practice daily, dan soon akan jadi second nature. Happy coding!
