Quick-lookup reference · all essential commands
WORKSPACE STAGING LOCAL REPO REMOTE REPO
| | | |
| git add/mv/rm | | |
|-------------->| | |
| | git commit | |
| |-------------->| |
| | | |
| git commit -a | |
|------------------------------>| |
| | | |
| git reset | | |
| <file> | | |
|<--------------| | |
| | | |
| git reset <commit> | |
|<------------------------------| |
| | | |
| | | git push |
| | |-------------->|
| | | |
| | | git fetch |
| | |<--------------|
| | | |
| git diff | | |
|xxxxxxxxxxxxxxx| | |
| | | |
| git diff HEAD | |
|xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx|| |
| | | |
| git clone / pull |
|<----------------------------------------------|
| | | |git switch <branch>git restore <file>git restore --staged <file>git checkout <commit>git clone <url>git initgit add .git commit -m "feat: msg"git commit -a -m msggit commit --amendSubject ≤50 chars, present tense, no period. Body ≤72 chars: what & why.
git reset --soft HEAD~1git reset --mixed HEAD~1git reset --hard HEAD~1git reset --hard HEADgit fetch origin && git reset --hard origin/maingit reset <commit> <file>git checkout <commit> <file>git reset --soft HEAD~N && git commit--soft: keeps staged · --mixed: unstages · --hard: discards
git rebase origin/maingit rebase -i HEAD~5git rebase -i <hash>git rebase --continuegit rebase --abortIn editor: pick · squash (s) · reword (r) · drop (d) · edit (e)
git merge --squash <branch>git cherry-pick <hash>git cherry-pick h1 h2 h3git cherry-pick h1^..h2git cherry-pick --continuegit cherry-pick --abortWrong branch commit? checkout target, cherry-pick, then reset --hard HEAD~1 on source.
git stashgit stash push -m "name"git stash listgit stash popgit stash apply stash@{n}git stash drop stash@{n}git stash cleargit tag v1.0-lwgit tag -a v1.0 -m "Release v1.0"git taggit show v1.0git push origin v1.0git push origin --tagsgit tag -d v1.0.1git push origin --delete v1.0.1Lightweight = pointer only. Annotated = full object with author, date, msg.
git branchgit branch -agit branch --merged maingit switch -c <name>git branch -d <name>git branch -D <name>git push origin --delete <name>Workflow: main → develop → feature/* → develop → release/* → main + develop. hotfix/* branches off main.
git fetch origingit pullgit push origin <branch>git push -u origin <branch>git push --force --mirror origingit remote -vgit remote add origin <url>git diffgit diff --cachedgit diff HEADgit diff-tree -r <hash>git statusgit log --oneline --graphgit blame <file>git diff: working↔index · git diff HEAD: working↔last commit · git status: working↔index
git revert <hash>git revert -n <hash>Revert adds a new commit. Use instead of reset on shared/main branches.
git rm --cached <file>git filter-repo --path <file> --invert-pathsgit filter-repo --replace-text <(echo 'secret==>***')git filter-repo --analyzeecho "file" >> .gitignoreAfter filter-repo, force push with --force --mirror to overwrite remote history.
git bisect startgit bisect badgit bisect good <hash>git bisect skipgit bisect resetGit binary-searches commits. Mark each checkout bad/good until culprit is found.
git refloggit checkout -b <name> HEAD@{n}git reflog showGit keeps 90 days of HEAD history (30 for unreachable). Untracked files are NOT recovered. Run git gc to purge manually.
mv pre-commit.sample pre-commitchmod +x .git/hooks/pre-commitpre-commitlint / format / tests — abort if exit 1prepare-commit-msgauto-insert Jira ID or templatecommit-msgenforce naming: feat: fix: docs:pre-pushvalidate before uploadpre-receiveserver: reject policy violationsupdateserver: per-branch rejectionpost-receiveserver: trigger CI/CD webhookUse Husky or pre-commit framework to share hooks — .git/ isn't tracked by VCS.
mainproduction-ready codedevelopnext release integrationfeature/*branches off develop → merges to developrelease/*final prep before mainhotfix/*branches off main → merges to main + developgit diff-tree -r <hash>git blame <file>git refloggit rm --cached <file>git add -pgit config --listgit config --global user.name "Name"git log --oneline --graph --all