Perintah Git penting untuk commit, branch, merge, rebase, remote, dan lainnya.
git config --global user.name "Name"Pengaturan & KonfigurasiSet global author name
mis. git config --global user.name "Jane Doe"
git config --global user.email "email"Pengaturan & KonfigurasiSet global author email
mis. git config --global user.email "[email protected]"
git config --listPengaturan & KonfigurasiList all configuration settings
git config --global core.editor "code"Pengaturan & KonfigurasiSet VS Code as default editor
git config --global alias.<alias> <cmd>Pengaturan & KonfigurasiCreate a command alias
mis. git config --global alias.st status
git initPengaturan & KonfigurasiInitialize a new local repository
git init <dir>Pengaturan & KonfigurasiInitialize a repo in a specific directory
mis. git init my-project
git clone <url>Pengaturan & KonfigurasiClone a remote repository
mis. git clone https://github.com/user/repo.git
git clone <url> <dir>Pengaturan & KonfigurasiClone into a specific folder
mis. git clone https://github.com/user/repo.git my-dir
git statusStaging & Dasar-dasarShow working tree status
git status -sStaging & Dasar-dasarShort/compact status output
git add <file>Staging & Dasar-dasarStage a specific file
mis. git add README.md
git add .Staging & Dasar-dasarStage all changes in current directory
git add -pStaging & Dasar-dasarInteractively stage hunks of changes
git commit -m "msg"Staging & Dasar-dasarCommit staged changes with a message
mis. git commit -m "feat: add login page"
git commit -am "msg"Staging & Dasar-dasarStage tracked files and commit in one step
git commit --amenddestruktifStaging & Dasar-dasarModify the most recent commit (message or content)
git diffStaging & Dasar-dasarShow unstaged changes
git diff --stagedStaging & Dasar-dasarShow staged changes (vs last commit)
git diff <branch1>..<branch2>Staging & Dasar-dasarCompare two branches
mis. git diff main..feature
git rm <file>Staging & Dasar-dasarRemove a file from working tree and index
mis. git rm old-file.txt
git rm --cached <file>Staging & Dasar-dasarUntrack a file without deleting it
mis. git rm --cached secret.txt
git mv <old> <new>Staging & Dasar-dasarMove or rename a tracked file
mis. git mv old.js new.js
git branchPercabangan & PenggabunganList local branches
git branch -aPercabangan & PenggabunganList all branches (local + remote)
git branch <name>Percabangan & PenggabunganCreate a new branch
mis. git branch feature/auth
git checkout <branch>Percabangan & PenggabunganSwitch to an existing branch
mis. git checkout main
git checkout -b <branch>Percabangan & PenggabunganCreate and switch to a new branch
mis. git checkout -b feature/auth
git switch <branch>Percabangan & PenggabunganSwitch branches (modern syntax)
mis. git switch main
git switch -c <branch>Percabangan & PenggabunganCreate and switch (modern syntax)
mis. git switch -c feature/auth
git branch -d <branch>Percabangan & PenggabunganDelete a merged branch
mis. git branch -d feature/auth
git branch -D <branch>destruktifPercabangan & PenggabunganForce delete a branch (unmerged too)
mis. git branch -D feature/old
git branch -m <old> <new>Percabangan & PenggabunganRename a branch
mis. git branch -m old-name new-name
git merge <branch>Percabangan & PenggabunganMerge a branch into current branch
mis. git merge feature/auth
git merge --no-ff <branch>Percabangan & PenggabunganMerge with a merge commit (no fast-forward)
git merge --squash <branch>Percabangan & PenggabunganSquash all commits from branch into one staged change
git merge --abortPercabangan & PenggabunganAbort an in-progress merge
git remote -vOperasi RemoteList remote connections with URLs
git remote add <name> <url>Operasi RemoteAdd a new remote
mis. git remote add origin https://github.com/user/repo.git
git remote remove <name>Operasi RemoteRemove a remote connection
mis. git remote remove origin
git remote rename <old> <new>Operasi RemoteRename a remote
mis. git remote rename origin upstream
git fetchOperasi RemoteDownload all changes from remote (no merge)
git fetch <remote>Operasi RemoteFetch from a specific remote
mis. git fetch origin
git pullOperasi RemoteFetch and merge changes from remote
git pull --rebaseOperasi RemoteFetch and rebase onto remote branch
git push <remote> <branch>Operasi RemotePush branch to remote
mis. git push origin main
git push -u origin <branch>Operasi RemotePush and set upstream tracking
mis. git push -u origin feature/auth
git push --force-with-leasedestruktifOperasi RemoteSafe force push (fails if others pushed)
git push --tagsOperasi RemotePush all tags to remote
git push origin --delete <branch>destruktifOperasi RemoteDelete a remote branch
mis. git push origin --delete feature/old
git restore <file>destruktifMembatalkan PerubahanDiscard changes in working directory
mis. git restore index.html
git restore --staged <file>Membatalkan PerubahanUnstage a file (keep changes)
mis. git restore --staged index.html
git reset HEAD <file>Membatalkan PerubahanUnstage a file (older syntax)
mis. git reset HEAD index.html
git reset --soft HEAD~1Membatalkan PerubahanUndo last commit, keep changes staged
git reset --mixed HEAD~1Membatalkan PerubahanUndo last commit, unstage changes (default)
git reset --hard HEAD~1destruktifMembatalkan PerubahanUndo last commit, discard all changes
git reset --hard <commit>destruktifMembatalkan PerubahanReset to a specific commit, discard everything after
git revert <commit>Membatalkan PerubahanCreate a new commit that undoes a specific commit
mis. git revert a1b2c3d
git revert HEADMembatalkan PerubahanRevert the most recent commit
git clean -fddestruktifMembatalkan PerubahanRemove untracked files and directories
git clean -nMembatalkan PerubahanDry run: show what would be cleaned
git stashMenyimpan sementaraStash current working directory changes
git stash push -m "msg"Menyimpan sementaraStash with a descriptive message
mis. git stash push -m "WIP: auth feature"
git stash listMenyimpan sementaraList all stashes
git stash popMenyimpan sementaraApply most recent stash and remove it
git stash apply stash@{n}Menyimpan sementaraApply a specific stash without removing
mis. git stash apply stash@{0}
git stash drop stash@{n}Menyimpan sementaraDelete a specific stash
mis. git stash drop stash@{0}
git stash cleardestruktifMenyimpan sementaraRemove all stashes
git stash branch <branch>Menyimpan sementaraCreate a branch from a stash
mis. git stash branch feature/from-stash
git rebase <branch>Rebase & LanjutanRebase current branch onto another
mis. git rebase main
git rebase -i HEAD~nRebase & LanjutanInteractive rebase for last n commits
mis. git rebase -i HEAD~3
git rebase --continueRebase & LanjutanContinue rebase after resolving conflicts
git rebase --abortRebase & LanjutanAbort an in-progress rebase
git cherry-pick <commit>Rebase & LanjutanApply a specific commit to current branch
mis. git cherry-pick a1b2c3d
git cherry-pick <c1>..<c2>Rebase & LanjutanApply a range of commits
mis. git cherry-pick a1b2c3d..f6e5d4c
git bisect startRebase & LanjutanStart binary search for a bug
git bisect good <commit>Rebase & LanjutanMark a commit as good in bisect
git bisect bad <commit>Rebase & LanjutanMark a commit as bad in bisect
git bisect resetRebase & LanjutanEnd bisect session
git worktree add <path> <branch>Rebase & LanjutanCheck out a branch into a separate directory
git logInspeksi & LogShow commit history
git log --onelineInspeksi & LogCompact one-line log
git log --oneline --graph --allInspeksi & LogVisual branch graph in terminal
git log -n <count>Inspeksi & LogShow last N commits
mis. git log -n 5
git log --author="name"Inspeksi & LogFilter commits by author
mis. git log --author="Jane Doe"
git log --since="2 weeks ago"Inspeksi & LogShow commits since a date
git log -p <file>Inspeksi & LogShow changes to a specific file over time
mis. git log -p README.md
git log --follow <file>Inspeksi & LogFollow renames of a file in history
git show <commit>Inspeksi & LogShow details of a specific commit
mis. git show a1b2c3d
git blame <file>Inspeksi & LogShow who last modified each line
mis. git blame src/app.tsx
git shortlog -snInspeksi & LogSummarize commits by author
git reflogInspeksi & LogShow history of HEAD movements (recovery tool)
git grep "pattern"Inspeksi & LogSearch working directory for a pattern
mis. git grep "TODO"
git tagTagList all tags
git tag <name>TagCreate a lightweight tag
mis. git tag v1.0.0
git tag -a <name> -m "msg"TagCreate an annotated tag
mis. git tag -a v1.0.0 -m "Release v1.0.0"
git tag -a <name> <commit>TagTag a past commit
mis. git tag -a v0.9.0 a1b2c3d
git show <tag>TagShow tag metadata and commit
mis. git show v1.0.0
git push origin <tag>TagPush a specific tag to remote
mis. git push origin v1.0.0
git push origin --tagsTagPush all tags to remote
git tag -d <tag>TagDelete a local tag
mis. git tag -d v1.0.0
git push origin --delete <tag>TagDelete a remote tag
mis. git push origin --delete v1.0.0
git merge menggabungkan dua branch dengan membuat merge commit, mempertahankan riwayat lengkap kedua branch. git rebase memindahkan commit dari satu branch ke branch lain, menulis ulang riwayat untuk membuat riwayat commit yang linear dan lebih bersih. Gunakan merge untuk branch publik dan pekerjaan bersama; gunakan rebase untuk membersihkan branch fitur lokal sebelum merging.
Gunakan git reset --soft HEAD~1 untuk membatalkan commit terakhir sambil mempertahankan perubahan dalam staging. Gunakan git reset HEAD~1 (atau --mixed) untuk menghapus perubahan dari staging tetapi menyimpannya di direktori kerja. Gunakan git reset --hard HEAD~1 untuk membuang commit dan semua perubahan secara permanen (operasi destruktif).
git fetch mengunduh perubahan dari remote tetapi TIDAK menggabungkannya ke branch Anda saat ini. Ini memperbarui branch pelacak remote Anda (origin/main). git pull adalah git fetch + git merge (atau git rebase dengan --rebase). Gunakan fetch ketika Anda ingin memeriksa perubahan sebelum mengintegrasikannya.
Setelah merge/rebase yang konflik, Git menandai file yang konflik dengan penanda '<<<<<<', =======, >>>>>>. Buka setiap file yang konflik, pilih secara manual perubahan mana yang akan disimpan, hapus penanda, lalu stage file yang telah diselesaikan dengan git add '<'file> dan selesaikan dengan git merge --continue atau git commit.