~/saidqb cat ecosystem/tools/git.md
Gi
Git
Distributed version control system
commit by saidqb
Version control wajib di setiap proyek — cheatsheet perintah yang paling sering dipakai sehari-hari.
Config
bash
git config --global user.name "Nama"
git config --global user.email "email@contoh.com"
git config --global init.defaultBranch main
git config --global core.editor "code --wait"
git config --list # lihat semua config aktifMembuat & Clone Repo
bash
git init
git clone https://github.com/user/repo.git
git clone -b production <url> # clone langsung checkout ke branch tertentu
git clone -b production --single-branch <url> # clone hanya branch tertentu, tanpa branch lain
git clone --depth 1 <url> # shallow clone, tanpa full history
git clone -b production --single-branch --depth 1 <url> # kombinasi paling hemat: 1 branch, 1 commit
git remote add origin <url>
git remote -vMengelola Remote
bash
git remote set-url origin <url-baru> # ganti url remote yang sudah ada
git remote remove origin # hapus remote
git remote add origin <url-baru>
git remote rename origin nama-baru # rename remote
git remote add upstream <url-lain> # tambah remote lain, mis. untuk forkStaging & Commit
bash
git status
git add file.txt
git add . # semua perubahan
git add -p # pilih hunk per hunk (interactive)
git commit -m "pesan commit"
git commit -am "pesan commit" # add + commit sekaligus (file yang sudah tracked)
git commit --amend # revisi commit terakhir
git commit --amend --no-edit # revisi tanpa ubah pesanBranching
bash
git branch # list branch lokal
git branch -a # list branch lokal + remote
git branch nama-branch
git checkout nama-branch
git checkout -b nama-branch # buat + pindah sekaligus
git switch nama-branch # alternatif checkout (lebih baru)
git switch -c nama-branch
git branch -d nama-branch # hapus branch (sudah merge)
git branch -D nama-branch # hapus paksa
git branch -m nama-baru # rename branch aktifSync dengan Remote
bash
git fetch origin
git pull origin main
git pull --rebase origin main # pull tanpa merge commit tambahan
git push origin main
git push -u origin nama-branch # set upstream sekali, selanjutnya cukup `git push`
git push --force-with-lease # force push yang lebih aman dari --forceMerge & Rebase
bash
git merge nama-branch
git merge --no-ff nama-branch # selalu buat merge commit
git rebase main
git rebase -i HEAD~3 # interactive rebase, 3 commit terakhir
git rebase --continue
git rebase --abortUndo & Reset
bash
git restore file.txt # buang perubahan di working directory
git restore --staged file.txt # unstage (tetap ada perubahan di file)
git reset --soft HEAD~1 # undo commit, perubahan tetap staged
git reset --mixed HEAD~1 # undo commit, perubahan jadi unstaged (default)
git reset --hard HEAD~1 # undo commit + buang semua perubahan (hati-hati)
git revert <commit> # buat commit baru yang membalikkan commit lama (aman untuk shared branch)Stash
bash
git stash # simpan perubahan sementara
git stash -u # ikutkan untracked files
git stash list
git stash pop # ambil lagi + hapus dari stash
git stash apply # ambil lagi, tapi tetap disimpan di stash
git stash drop # buang stash tertentuLog & Diff
bash
git log --oneline --graph --all
git log -p file.txt # history perubahan satu file
git diff # unstaged vs staged
git diff --staged # staged vs commit terakhir
git blame file.txt # siapa mengubah baris manaSubtree
Deploy satu folder dari monorepo ke remote/branch terpisah, tanpa split repo penuh.
bash
git subtree push --prefix=project origin production
git subtree pull --prefix=project origin production # tarik balik perubahan dari remote
git subtree split --prefix=project -b project-only # ekstrak folder jadi branch tersendiriTag
bash
git tag v1.0.0
git tag -a v1.0.0 -m "Release 1.0.0" # annotated tag
git push origin v1.0.0
git push origin --tags