Logo

~/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

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 aktif

Membuat & Clone Repo

git init
git clone https://github.com/user/repo.git
git clone --depth 1 <url>                     # shallow clone, tanpa full history
git remote add origin <url>
git remote -v

Staging & Commit

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 pesan

Branching

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 aktif

Sync dengan Remote

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 --force

Merge & Rebase

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 --abort

Undo & Reset

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

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 tertentu

Log & Diff

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 mana

Subtree

Deploy satu folder dari monorepo ke remote/branch terpisah, tanpa split repo penuh.

git subtree push --prefix=app-laravel origin production
git subtree pull --prefix=app-laravel origin production   # tarik balik perubahan dari remote
git subtree split --prefix=app-laravel -b app-laravel-only # ekstrak folder jadi branch tersendiri

Tag

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