main
mdx 636 lines 11.8 KB
Raw
1 ---
2 title: "Git"
3 term: "apa-itu-git"
4 category: "Software Development"
5 tags: ["version-control", "collaboration", "github", "gitlab", "source-code"]
6 description: "Sistem version control terdistribusi untuk tracking perubahan kode, berkolaborasi dengan tim, dan mengelola history project."
7 englishTerm: "Git"
8 relatedTerms: ["apa-itu-github", "apa-itu-gitlab", "apa-itu-version-control", "apa-itu-devops", "apa-itu-ci-cd"]
9 difficulty: "pemula"
10 lastmod: 2024-01-15
11 published: true
12 ---
13
14 ## Apa itu Git?
15
16 **Git** adalah sistem version control terdistribusi untuk mencatat perubahan kode dari waktu ke waktu. Dengan Git, developer bisa menyimpan riwayat pekerjaan, bekerja di beberapa fitur secara terpisah, dan berkolaborasi tanpa harus terus saling menimpa file.
17
18 Git biasa dipakai untuk:
19 - Melacak perubahan kode
20 - Bekerja bersama tim
21 - Kembali ke versi sebelumnya saat ada masalah
22 - Mencoba fitur baru di branch terpisah
23
24 Git dibuat oleh Linus Torvalds pada tahun 2005. Sampai sekarang, Git masih menjadi tool utama untuk version control di banyak proyek software.
25
26 ## Mengapa Git Penting?
27
28 ### Sebelum Git
29 Bayangkan Anda bekerja tanpa Git:
30
31 ```
32 project/
33 ├── app.js
34 ├── app_backup.js
35 ├── app_backup_final.js
36 ├── app_backup_final_FINAL.js
37 ├── app_backup_final_FINAL_v2.js
38 └── app_backup_THIS_ONE_USE_THIS.js
39 ```
40
41 **Masalah**:
42 - File duplikat di mana-mana
43 - Tidak tahu perubahan apa saja yang terjadi
44 - Sulit kolaborasi dengan tim
45 - Takut hapus kode karena tidak bisa kembali
46
47 ### Dengan Git
48 ```bash
49 $ git log --oneline
50 a1b2c3d (HEAD -> main) Add user authentication
51 d4e5f6g Refactor database queries
52 g7h8i9j Initial commit
53 ```
54
55 **Keuntungan**:
56 - History lengkap semua perubahan
57 - Siapa yang mengubah apa dan kapan
58 - Bisa kembali ke versi manapun
59 - Kolaborasi tim tanpa konflik
60
61 ## Konsep Dasar Git
62
63 ### Repository (Repo)
64 Folder project yang di-track oleh Git. Repository berisi:
65 - Source code
66 - History semua perubahan
67 - Branches dan tags
68 - Configuration
69
70 ```bash
71 # Initialize repository baru
72 git init
73
74 # Clone repository existing
75 git clone https://github.com/username/project.git
76 ```
77
78 ### Commit
79 Snapshot dari perubahan code Anda. Seperti "save point" dalam game.
80
81 ```bash
82 # Lihat status files
83 git status
84
85 # Add files ke staging
86 git add index.html
87 git add . # Add semua files
88
89 # Commit changes
90 git commit -m "Add homepage design"
91 ```
92
93 ### Branch
94 Cabang development terpisah. Seperti "parallel universe" untuk code Anda.
95
96 ```bash
97 # List branches
98 git branch
99
100 # Create branch baru
101 git branch feature-login
102
103 # Switch ke branch
104 git checkout feature-login
105 # atau dengan Git 2.23+
106 git switch feature-login
107
108 # Create dan switch sekaligus
109 git checkout -b feature-login
110 ```
111
112 ### Merge
113 Menggabungkan perubahan dari satu branch ke branch lain.
114
115 ```bash
116 # Switch ke branch tujuan
117 git checkout main
118
119 # Merge branch lain
120 git merge feature-login
121 ```
122
123 ## Git Workflow Dasar
124
125 ### 1. Setup Awal
126
127 ```bash
128 # Configure identity
129 git config --global user.name "Nama Anda"
130 git config --global user.email "email@example.com"
131
132 # Lihat configuration
133 git config --list
134 ```
135
136 ### 2. Daily Workflow
137
138 ```bash
139 # 1. Check status
140 git status
141
142 # 2. Pull latest changes
143 git pull origin main
144
145 # 3. Create feature branch
146 git checkout -b feature-new-button
147
148 # 4. Make changes, then add
149 git add .
150
151 # 5. Commit changes
152 git commit -m "Add new button to homepage"
153
154 # 6. Push to remote
155 git push origin feature-new-button
156 ```
157
158 ### 3. Merge ke Main Branch
159
160 ```bash
161 # Switch ke main
162 git checkout main
163
164 # Pull latest
165 git pull origin main
166
167 # Merge feature branch
168 git merge feature-new-button
169
170 # Push to remote
171 git push origin main
172 ```
173
174 ## Git Commands Essential
175
176 ### Status & Information
177
178 ```bash
179 # Status files
180 git status
181
182 # View commit history
183 git log
184 git log --oneline
185 git log --graph --oneline --all
186
187 # Show changes
188 git diff
189 git diff --staged
190
191 # Show specific commit
192 git show a1b2c3d
193 ```
194
195 ### Working with Changes
196
197 ```bash
198 # Add files
199 git add file.txt
200 git add *.js
201 git add .
202
203 # Remove from staging
204 git reset file.txt
205
206 # Discard changes
207 git checkout -- file.txt
208 git restore file.txt # Git 2.23+
209
210 # Stash changes (temporary save)
211 git stash
212 git stash list
213 git stash pop
214 ```
215
216 ### Branches
217
218 ```bash
219 # List branches
220 git branch
221 git branch -a # Include remote branches
222
223 # Create branch
224 git branch feature-name
225
226 # Switch branch
227 git switch feature-name
228
229 # Delete branch
230 git branch -d feature-name
231 git branch -D feature-name # Force delete
232 ```
233
234 ### Remote Operations
235
236 ```bash
237 # List remotes
238 git remote -v
239
240 # Add remote
241 git remote add origin https://github.com/user/repo.git
242
243 # Fetch changes (don't merge)
244 git fetch origin
245
246 # Pull changes (fetch + merge)
247 git pull origin main
248
249 # Push changes
250 git push origin main
251 git push -u origin main # Set upstream
252 ```
253
254 ### Undo Changes
255
256 ```bash
257 # Undo last commit (keep changes)
258 git reset --soft HEAD~1
259
260 # Undo last commit (discard changes)
261 git reset --hard HEAD~1
262
263 # Revert commit (create new commit)
264 git revert a1b2c3d
265
266 # Amend last commit
267 git commit --amend -m "New message"
268 ```
269
270 ## Branching Strategies
271
272 ### Git Flow
273 Popular strategy untuk production apps:
274
275 ```
276 main (production)
277 ├── develop (integration)
278 ├── feature/login
279 ├── feature/payment
280 └── feature/dashboard
281
282 release/1.0.0
283 hotfix/critical-bug
284 ```
285
286 ### GitHub Flow
287 Simpler strategy untuk continuous deployment:
288
289 ```
290 main (always deployable)
291 ├── feature/user-auth
292 ├── feature/api-endpoint
293 └── bugfix/login-error
294 ```
295
296 ## .gitignore File
297
298 File untuk exclude files/folders dari Git:
299
300 ```gitignore
301 # Node.js
302 node_modules/
303 npm-debug.log
304 .env
305
306 # Python
307 __pycache__/
308 *.pyc
309 venv/
310
311 # IDE
312 .vscode/
313 .idea/
314 *.swp
315
316 # OS
317 .DS_Store
318 Thumbs.db
319
320 # Build files
321 dist/
322 build/
323 *.log
324 ```
325
326 ## Git dengan GitHub/GitLab
327
328 ### SSH Setup
329
330 ```bash
331 # Generate SSH key
332 ssh-keygen -t ed25519 -C "email@example.com"
333
334 # Copy public key
335 cat ~/.ssh/id_ed25519.pub
336
337 # Add to GitHub/GitLab settings
338 # Test connection
339 ssh -T git@github.com
340 ```
341
342 ### Pull Requests / Merge Requests
343
344 Workflow kolaborasi:
345
346 1. **Fork/Clone** repository
347 2. **Create branch** untuk feature
348 3. **Commit changes** dengan descriptive messages
349 4. **Push** ke remote
350 5. **Create PR/MR** di GitHub/GitLab
351 6. **Code review** dari tim
352 7. **Merge** setelah approved
353
354 ## Git Best Practices
355
356 ### 1. Commit Messages
357
358 **Kurang baik:**
359 ```bash
360 git commit -m "fix"
361 git commit -m "update"
362 git commit -m "asdfasdf"
363 ```
364
365 **Lebih baik:**
366 ```bash
367 git commit -m "Fix login validation bug"
368 git commit -m "Add user profile endpoint"
369 git commit -m "Refactor database connection logic"
370 ```
371
372 **Format yang baik:**
373 ```
374 <type>: <subject>
375
376 <body>
377
378 <footer>
379 ```
380
381 **Contoh:**
382 ```
383 feat: Add user authentication
384
385 - Implement JWT token generation
386 - Add login/register endpoints
387 - Create middleware for auth check
388
389 Closes #123
390 ```
391
392 **Types:**
393 - `feat`: New feature
394 - `fix`: Bug fix
395 - `docs`: Documentation
396 - `style`: Formatting, no code change
397 - `refactor`: Code restructure
398 - `test`: Add tests
399 - `chore`: Maintenance tasks
400
401 ### 2. Commit Often, Perfect Later
402
403 ```bash
404 # Commit frequently during development
405 git commit -m "WIP: Add user model"
406 git commit -m "WIP: Add validation"
407 git commit -m "WIP: Add tests"
408
409 # Squash commits before merge
410 git rebase -i HEAD~3
411 ```
412
413 ### 3. Never Commit Sensitive Data
414
415 **Jangan commit:**
416 - API keys
417 - Passwords
418 - Database credentials
419 - Private keys
420 - `.env` files
421
422 **Gunakan environment variables:**
423 ```javascript
424 // config.js
425 module.exports = {
426 apiKey: process.env.API_KEY,
427 dbPassword: process.env.DB_PASSWORD
428 }
429 ```
430
431 ### 4. Pull Before Push
432
433 ```bash
434 # Always pull first
435 git pull origin main
436
437 # Resolve conflicts if any
438 # Then push
439 git push origin main
440 ```
441
442 ## Handling Merge Conflicts
443
444 Ketika Git tidak bisa auto-merge:
445
446 ```bash
447 $ git merge feature-branch
448 Auto-merging index.html
449 CONFLICT (content): Merge conflict in index.html
450 Automatic merge failed; fix conflicts and then commit the result.
451 ```
452
453 **File dengan conflict:**
454 ```html
455 <!DOCTYPE html>
456 <html>
457 <<<<<<< HEAD
458 <title>Main Branch Title</title>
459 =======
460 <title>Feature Branch Title</title>
461 >>>>>>> feature-branch
462 </html>
463 ```
464
465 **Resolve:**
466 1. Edit file, remove conflict markers
467 2. Keep one version or combine both
468 3. `git add` the file
469 4. `git commit` to complete merge
470
471 ```bash
472 # After resolving
473 git add index.html
474 git commit -m "Resolve merge conflict in index.html"
475 ```
476
477 ## Git Aliases (Shortcuts)
478
479 ```bash
480 # Setup aliases
481 git config --global alias.co checkout
482 git config --global alias.br branch
483 git config --global alias.ci commit
484 git config --global alias.st status
485 git config --global alias.unstage 'reset HEAD --'
486 git config --global alias.last 'log -1 HEAD'
487 git config --global alias.visual 'log --graph --oneline --all'
488
489 # Usage
490 git co main
491 git br feature-new
492 git ci -m "message"
493 git st
494 ```
495
496 ## Common Mistakes & Solutions
497
498 ### 1. Committed to Wrong Branch
499
500 ```bash
501 # Create branch from current state
502 git branch correct-branch
503
504 # Reset current branch
505 git reset --hard HEAD~1
506
507 # Switch to correct branch
508 git checkout correct-branch
509 ```
510
511 ### 2. Need to Change Last Commit Message
512
513 ```bash
514 git commit --amend -m "Correct message"
515 ```
516
517 ### 3. Accidentally Deleted File
518
519 ```bash
520 # Restore from last commit
521 git checkout HEAD -- file.txt
522 ```
523
524 ### 4. Want to Undo All Local Changes
525
526 ```bash
527 # Discard all local changes
528 git reset --hard HEAD
529
530 # Clean untracked files
531 git clean -fd
532 ```
533
534 ## Git untuk Tim
535
536 ### Code Review Checklist
537
538 - Code follows style guide
539 - Tests are included
540 - No sensitive data committed
541 - Commit messages are clear
542 - Documentation updated
543 - No merge conflicts
544
545 ### Protected Branches
546
547 Di GitHub/GitLab, set rules untuk main branch:
548 - Require pull request reviews
549 - Require status checks to pass
550 - Require branches to be up to date
551 - No force push
552
553 ## Git Tips untuk Developer Indonesia
554
555 ### 1. Mulai dari Project Pribadi
556
557 ```bash
558 # Create simple project
559 mkdir my-first-repo
560 cd my-first-repo
561 git init
562 echo "# My First Repo" > README.md
563 git add README.md
564 git commit -m "Initial commit"
565 ```
566
567 Project kecil justru enak untuk belajar Git, karena Anda bisa bebas mencoba tanpa takut merusak pekerjaan tim.
568
569 ### 2. Latihan lewat GitHub
570
571 - Buat akun GitHub yang gratis
572 - Clone atau fork project open source
573 - Coba kontribusi kecil, misalnya perbaikan dokumentasi
574 - Pakai repository publik untuk membangun portofolio
575
576 ### 3. Belajar dari Kasus Nyata
577
578 Cobalah skenario yang memang sering terjadi:
579 - Membuat branch baru
580 - Menyelesaikan merge conflict
581 - Mencoba merge dan rebase
582 - Mengembalikan perubahan yang keliru
583
584 ### 4. Pakai GUI Kalau Memang Membantu
585
586 Kalau command line masih terasa berat di awal, GUI tools bisa membantu memahami alur Git:
587 - **GitHub Desktop** - Sederhana dan ramah pemula
588 - **SourceTree** - Fitur lebih banyak
589 - **GitKraken** - Visualisasi yang cukup jelas
590 - **VS Code** - Sudah punya integrasi Git bawaan
591
592 ## Git dengan smbCloud
593
594 smbCloud terintegrasi dengan Git untuk auto-deployment:
595
596 ```bash
597 # Setup remote
598 git remote add smb https://git.smbcloud.xyz/yourapp.git
599
600 # Deploy dengan git push
601 git push smb main
602
603 # smbCloud otomatis:
604 # Pull latest code
605 # Install dependencies
606 # Run tests
607 # Build application
608 # Deploy to production
609 ```
610
611 ## Resources untuk Belajar
612
613 ### Interactive Tutorials
614 - **Learn Git Branching** - learngitbranching.js.org
615 - **GitHub Skills** - skills.github.com
616
617 ### Cheat Sheets
618 - **GitHub Git Cheat Sheet** - education.github.com/git-cheat-sheet-education.pdf
619 - **Atlassian Git Cheat Sheet** - atlassian.com/git/tutorials/atlassian-git-cheatsheet
620
621 ### Books
622 - **Pro Git** (Free) - git-scm.com/book
623 - **Git Pocket Guide** - O'Reilly
624
625 ## Kesimpulan
626
627 Git adalah dasar kerja harian banyak developer. Dengan Git, Anda bisa melacak perubahan, bekerja lebih aman, dan berkolaborasi tanpa harus mengandalkan salinan file manual yang membingungkan.
628
629 **Langkah selanjutnya:**
630 1. Install Git di komputer Anda
631 2. Buat akun GitHub
632 3. Inisialisasi Git di project pribadi
633 4. Latihan commit, branch, dan merge
634 5. Coba kontribusi kecil ke open source
635
636 Kalau dasarnya sudah nyaman, fitur yang lebih lanjut seperti rebase, cherry-pick, atau workflow CI/CD akan jauh lebih mudah dipahami.