mbahsomo-note/perintahgit.md

5.7 KiB

Berikut adalah perintah Git yang paling sering digunakan dalam pengembangan sehari-hari, diurutkan berdasarkan alur kerja.

1. Konfigurasi

# 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

# 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

# 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

# Tambah satu file
git add file.txt

# Tambah semua file
git add .

# Tambah semua perubahan
git add -A

5. Commit

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

# 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

# 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

# Merge branch ke branch aktif
git merge fitur-login

9. Remote

# 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

# 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

# Pull
git pull

# Pull branch tertentu
git pull origin main

# Fetch lalu rebase
git pull --rebase

12. Fetch

git fetch

git fetch --all

13. Stash

# 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

git restore file.txt

Membatalkan file yang sudah di-add

git restore --staged file.txt

Kembali ke commit sebelumnya

git reset --soft HEAD~1

git reset --mixed HEAD~1

git reset --hard HEAD~1

15. Tag

# 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

# 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

# Hapus file untracked
git clean -f

# Hapus folder untracked
git clean -fd

# Simulasi
git clean -n

18. Melihat Commit Tertentu

git show

git show HEAD

git show <hash>

19. Cherry Pick

git cherry-pick <hash-commit>

20. Rebase

git rebase main

# Interactive rebase
git rebase -i HEAD~5

21. Blame

git blame file.go

22. Mencari Commit

git log --grep="login"

git log --author="Sugik"

git log --since="1 week ago"

23. Melihat Hash Commit

git rev-parse HEAD

Alur kerja yang paling sering dipakai

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:

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.