Add perintahgit.md
This commit is contained in:
parent
4af159192a
commit
37d24a82ce
|
|
@ -0,0 +1,404 @@
|
|||
Berikut adalah perintah Git yang paling sering digunakan dalam pengembangan sehari-hari, diurutkan berdasarkan alur kerja.
|
||||
|
||||
## 1. Konfigurasi
|
||||
|
||||
```bash
|
||||
# Melihat konfigurasi
|
||||
git config --list
|
||||
|
||||
# Set nama
|
||||
git config --global user.name "Nama Anda"
|
||||
|
||||
# Set email
|
||||
git config --global user.email "email@example.com"
|
||||
|
||||
# Editor default
|
||||
git config --global core.editor nano
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. Membuat dan Mengambil Repository
|
||||
|
||||
```bash
|
||||
# Inisialisasi repository baru
|
||||
git init
|
||||
|
||||
# Clone repository
|
||||
git clone https://github.com/user/project.git
|
||||
|
||||
# Clone ke folder tertentu
|
||||
git clone https://github.com/user/project.git nama-folder
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. Mengecek Status
|
||||
|
||||
```bash
|
||||
# Status file
|
||||
git status
|
||||
|
||||
# Status singkat
|
||||
git status -s
|
||||
|
||||
# Riwayat commit
|
||||
git log
|
||||
|
||||
# Riwayat singkat
|
||||
git log --oneline
|
||||
|
||||
# Riwayat dalam bentuk grafik
|
||||
git log --oneline --graph --all
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Menambah File
|
||||
|
||||
```bash
|
||||
# Tambah satu file
|
||||
git add file.txt
|
||||
|
||||
# Tambah semua file
|
||||
git add .
|
||||
|
||||
# Tambah semua perubahan
|
||||
git add -A
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. Commit
|
||||
|
||||
```bash
|
||||
git commit -m "Menambahkan fitur login"
|
||||
|
||||
# Commit semua file yang sudah di-track
|
||||
git commit -am "Perbaikan bug"
|
||||
|
||||
# Mengubah commit terakhir
|
||||
git commit --amend
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. Melihat Perubahan
|
||||
|
||||
```bash
|
||||
# Perubahan yang belum di-add
|
||||
git diff
|
||||
|
||||
# Perubahan yang sudah di-add
|
||||
git diff --cached
|
||||
|
||||
# Perubahan antar commit
|
||||
git diff HEAD~1 HEAD
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. Branch
|
||||
|
||||
```bash
|
||||
# Lihat branch
|
||||
git branch
|
||||
|
||||
# Membuat branch
|
||||
git branch fitur-login
|
||||
|
||||
# Pindah branch
|
||||
git switch fitur-login
|
||||
|
||||
# Cara lama
|
||||
git checkout fitur-login
|
||||
|
||||
# Membuat sekaligus pindah
|
||||
git switch -c fitur-login
|
||||
|
||||
# Hapus branch
|
||||
git branch -d fitur-login
|
||||
|
||||
# Paksa hapus
|
||||
git branch -D fitur-login
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 8. Merge
|
||||
|
||||
```bash
|
||||
# Merge branch ke branch aktif
|
||||
git merge fitur-login
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 9. Remote
|
||||
|
||||
```bash
|
||||
# Lihat remote
|
||||
git remote -v
|
||||
|
||||
# Menambah remote
|
||||
git remote add origin https://github.com/user/project.git
|
||||
|
||||
# Mengubah URL remote
|
||||
git remote set-url origin https://github.com/user/project.git
|
||||
|
||||
# Menghapus remote
|
||||
git remote remove origin
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 10. Push
|
||||
|
||||
```bash
|
||||
# Push branch aktif
|
||||
git push
|
||||
|
||||
# Push pertama kali
|
||||
git push -u origin main
|
||||
|
||||
# Push branch tertentu
|
||||
git push origin develop
|
||||
|
||||
# Paksa push
|
||||
git push --force
|
||||
|
||||
# Lebih aman
|
||||
git push --force-with-lease
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 11. Pull
|
||||
|
||||
```bash
|
||||
# Pull
|
||||
git pull
|
||||
|
||||
# Pull branch tertentu
|
||||
git pull origin main
|
||||
|
||||
# Fetch lalu rebase
|
||||
git pull --rebase
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 12. Fetch
|
||||
|
||||
```bash
|
||||
git fetch
|
||||
|
||||
git fetch --all
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 13. Stash
|
||||
|
||||
```bash
|
||||
# Simpan perubahan
|
||||
git stash
|
||||
|
||||
# Dengan pesan
|
||||
git stash push -m "Belum selesai"
|
||||
|
||||
# Lihat daftar stash
|
||||
git stash list
|
||||
|
||||
# Ambil kembali
|
||||
git stash pop
|
||||
|
||||
# Ambil tanpa menghapus
|
||||
git stash apply
|
||||
|
||||
# Hapus stash
|
||||
git stash drop
|
||||
|
||||
# Hapus semua
|
||||
git stash clear
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 14. Undo
|
||||
|
||||
### Membatalkan perubahan file
|
||||
|
||||
```bash
|
||||
git restore file.txt
|
||||
```
|
||||
|
||||
### Membatalkan file yang sudah di-add
|
||||
|
||||
```bash
|
||||
git restore --staged file.txt
|
||||
```
|
||||
|
||||
### Kembali ke commit sebelumnya
|
||||
|
||||
```bash
|
||||
git reset --soft HEAD~1
|
||||
|
||||
git reset --mixed HEAD~1
|
||||
|
||||
git reset --hard HEAD~1
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 15. Tag
|
||||
|
||||
```bash
|
||||
# Lihat tag
|
||||
git tag
|
||||
|
||||
# Membuat tag
|
||||
git tag v1.0.0
|
||||
|
||||
# Push tag
|
||||
git push origin v1.0.0
|
||||
|
||||
# Push semua tag
|
||||
git push origin --tags
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 16. Melihat File
|
||||
|
||||
```bash
|
||||
# File yang berubah
|
||||
git diff --name-only
|
||||
|
||||
# File yang di-track
|
||||
git ls-files
|
||||
|
||||
# File yang belum di-track
|
||||
git ls-files --others --exclude-standard
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 17. Membersihkan Repository
|
||||
|
||||
```bash
|
||||
# Hapus file untracked
|
||||
git clean -f
|
||||
|
||||
# Hapus folder untracked
|
||||
git clean -fd
|
||||
|
||||
# Simulasi
|
||||
git clean -n
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 18. Melihat Commit Tertentu
|
||||
|
||||
```bash
|
||||
git show
|
||||
|
||||
git show HEAD
|
||||
|
||||
git show <hash>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 19. Cherry Pick
|
||||
|
||||
```bash
|
||||
git cherry-pick <hash-commit>
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 20. Rebase
|
||||
|
||||
```bash
|
||||
git rebase main
|
||||
|
||||
# Interactive rebase
|
||||
git rebase -i HEAD~5
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 21. Blame
|
||||
|
||||
```bash
|
||||
git blame file.go
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 22. Mencari Commit
|
||||
|
||||
```bash
|
||||
git log --grep="login"
|
||||
|
||||
git log --author="Sugik"
|
||||
|
||||
git log --since="1 week ago"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 23. Melihat Hash Commit
|
||||
|
||||
```bash
|
||||
git rev-parse HEAD
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Alur kerja yang paling sering dipakai
|
||||
|
||||
```bash
|
||||
git status
|
||||
git pull
|
||||
git checkout -b fitur-baru # atau: git switch -c fitur-baru
|
||||
# edit file...
|
||||
git add .
|
||||
git commit -m "Menambahkan fitur"
|
||||
git push -u origin fitur-baru
|
||||
```
|
||||
|
||||
Setelah fitur selesai:
|
||||
|
||||
```bash
|
||||
git switch main
|
||||
git pull
|
||||
git merge fitur-baru
|
||||
git push
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# Perintah yang sangat sering digunakan (sekitar 90% pekerjaan)
|
||||
|
||||
| Perintah | Fungsi |
|
||||
| --------------------------------- | ------------------------------------- |
|
||||
| `git status` | Melihat status repository |
|
||||
| `git add .` | Menambahkan semua perubahan |
|
||||
| `git commit -m "..."` | Menyimpan perubahan |
|
||||
| `git pull` | Mengambil perubahan terbaru |
|
||||
| `git push` | Mengirim commit ke remote |
|
||||
| `git switch <branch>` | Berpindah branch |
|
||||
| `git switch -c <branch>` | Membuat dan berpindah branch |
|
||||
| `git branch` | Melihat daftar branch |
|
||||
| `git merge` | Menggabungkan branch |
|
||||
| `git fetch` | Mengambil update tanpa merge |
|
||||
| `git stash` | Menyimpan perubahan sementara |
|
||||
| `git log --oneline --graph --all` | Melihat riwayat commit secara ringkas |
|
||||
| `git diff` | Melihat perubahan file |
|
||||
| `git restore` | Membatalkan perubahan file |
|
||||
| `git reset` | Membatalkan commit atau staging |
|
||||
|
||||
Jika Anda bekerja sendiri maupun dalam tim, menguasai sekitar 15 perintah di atas sudah mencakup sebagian besar aktivitas Git sehari-hari.
|
||||
Loading…
Reference in New Issue