Setup & Config
git initInitialize a new repository in current directorygit init my-projectCreate new directory and initialize repogit clone urlClone remote repositorygit clone url dirClone into a specific directorygit clone --depth 1 urlShallow clone - only latest commitgit clone --branch main urlClone specific branchgit config --global user.name "Alice"Set global usernamegit config --global user.email "a@b.com"Set global emailgit config --global core.editor "vim"Set default editorgit config --global init.defaultBranch mainSet default branch namegit config --listShow all configuration valuesgit config user.nameShow a specific config value.gitignoreFile listing patterns to ignore - one per linegit check-ignore -v fileCheck why a file is ignoredStage & Commit
git statusShow working tree and staging area statusgit status -sShort status - compact formatgit add file.txtStage a filegit add src/Stage all changes in directorygit add -pInteractively stage hunksgit add -uStage all tracked modifications and deletionsgit restore --staged fileUnstage file (keep changes in working tree)git restore fileDiscard working tree changes (irreversible)git commit -m "message"Commit staged changesgit commit -am "message"Stage tracked files and commit in one stepgit commit --amendModify the last commit message or add staged changesgit commit --amend --no-editAmend last commit keeping same messagegit diffDiff working tree vs staging areagit diff --stagedDiff staging area vs last commitgit diff HEADDiff working tree vs last commitgit diff branch1..branch2Diff between two branchesgit diff --statShow changed file names and line counts onlygit showShow last commit details and diffgit show abc1234Show specific commitBranching
git branchList local branchesgit branch -aList all branches including remote-trackinggit branch -vShow last commit on each branchgit branch featureCreate branchgit switch featureSwitch to branch (modern)git switch -c featureCreate and switch to new branchgit switch -Switch to previous branchgit checkout featureSwitch to branch (legacy)git checkout -b featureCreate and switch to branch (legacy)git branch -d featureDelete merged branchgit branch -D featureForce delete branchgit branch -m old newRename branchgit branch -m newRename current branchgit switch -c feature origin/featureCreate local branch tracking remotegit merge featureMerge branch into current branchgit merge --no-ff featureMerge with a merge commit (no fast-forward)git merge --squash featureSquash all commits into one staged changegit merge --abortAbort in-progress mergegit cherry-pick abc1234Apply a specific commit to current branchgit cherry-pick a..bApply range of commitsRemote
git remote -vList remotes with URLsgit remote add origin urlAdd a remotegit remote remove originRemove a remotegit remote rename origin upstreamRename a remotegit remote set-url origin urlChange remote URLgit fetchDownload all remote changes - does not mergegit fetch originFetch from specific remotegit fetch --pruneFetch and remove deleted remote-tracking branchesgit pullFetch and merge current branchgit pull --rebaseFetch and rebase instead of mergegit pushPush current branch to tracked remotegit push -u origin featurePush and set upstream trackinggit push origin --delete featureDelete remote branchgit push --force-with-leaseForce push - fails if remote has new commitsgit push --tagsPush all tagsHistory & Inspection
git logShow commit historygit log --onelineOne line per commitgit log --oneline --graph --allVisualize all branches as a graphgit log -n 10Show last 10 commitsgit log --author="Alice"Filter by authorgit log --since="2 weeks ago"Filter by dategit log -- path/to/fileHistory for a specific filegit log --grep="bug"Filter commits by message contentgit log -S "searchString"Find commits that added/removed a stringgit log --format="%h %an %s" --no-mergesCustom format: short hash, author, subjectgit log --diff-filter=D -- file.txtFind commit that deleted a filegit blame file.txtShow who changed each line and whengit blame -L 10,20 file.txtBlame specific line rangegit shortlog -snCommit count per authorgit reflogHistory of HEAD movements - useful for recoverygit reflog expire --expire=now --all && git gc --prune=nowExpire reflog and run garbage collectiongit bisect startStart binary search for a regressiongit bisect bad / git bisect good abcMark commit as bad/good during bisectgit bisect run npm testAutomate bisect with a test scriptUndo & Reset
git revert HEADCreate new commit that undoes last commitgit revert abc1234Revert a specific commitgit revert abc1234..HEADRevert a range of commitsgit reset --soft HEAD~1Undo last commit - keep changes stagedgit reset --mixed HEAD~1Undo last commit - keep changes unstaged (default)git reset --hard HEAD~1Undo last commit - discard all changes (destructive)git reset --hard origin/mainReset local branch to match remotegit restore file.txtDiscard working tree changes to a filegit restore --source=HEAD~1 file.txtRestore file from a specific commitgit clean -fdRemove untracked files and directoriesgit clean -nDry run - show what clean would removegit rebase -i HEAD~3Interactive rebase - reword, squash, or drop last 3 commitsgit rebase mainReplay current branch commits on top of maingit rebase --abortAbort in-progress rebasegit rebase --continueContinue after resolving conflictgit commit --fixup=HEAD~1Create a fixup commit targeting a parentgit rebase -i --autosquash HEAD~5Auto-apply fixup!/squash! commits during rebaseStash
git stashStash working tree and staging area changesgit stash push -m "WIP: auth"Stash with a descriptiongit stash -uAlso stash untracked filesgit stash listList all stashesgit stash popApply latest stash and remove it from stackgit stash pop stash@{2}Apply specific stash and remove itgit stash apply stash@{2}Apply stash without removing itgit stash drop stash@{0}Delete a specific stashgit stash clearDelete all stashesgit stash show -pShow diff of latest stashgit stash branch feature stash@{0}Create branch from stashTags
git tagList all tagsgit tag v1.0.0Create lightweight tag on current commitgit tag -a v1.0.0 -m "Release 1.0"Create annotated tag with messagegit tag -a v1.0.0 abc1234Tag a specific past commitgit tag -d v1.0.0Delete local taggit push origin v1.0.0Push a tag to remotegit push origin --tagsPush all tagsgit push origin --delete v1.0.0Delete remote taggit describe --tagsDescribe commit using nearest taggit switch --detach v1.0.0Check out a tag (detached HEAD)Worktrees
git worktree add ../hotfix hotfix-branchCheck out a branch into a separate directorygit worktree add -b new-branch ../new-dirCreate new branch and check it out in a new directorygit worktree add --detach ../review abc1234Check out a specific commit in a new directorygit worktree listList all worktrees with path and HEADgit worktree remove ../hotfixRemove a worktree directory and its metadatagit worktree pruneClean up stale worktree metadatagit worktree lock ../hotfixPrevent a worktree from being prunedgit worktree unlock ../hotfixRemove lock from a worktreeSubmodules
git submodule add https://github.com/user/lib libs/libAdd a submodule at a pathgit submodule initInitialize submodule config after cloninggit submodule updateCheck out the recorded commit for each submodulegit submodule update --init --recursiveInit and update all submodules recursivelygit clone --recurse-submodules urlClone and initialize all submodules in one stepgit submodule update --remoteFetch and update submodules to latest upstream commitgit submodule foreach git pull origin mainRun a command in every submodulegit submodule statusShow current commit hash for each submodulegit submodule deinit libs/libUnregister a submodulegit rm libs/libRemove submodule entry from .gitmodules and indexDebugging
git bisect startStart bisect session to find a regressiongit bisect badMark current commit as badgit bisect good v1.0.0Mark known-good commitgit bisect run ./test.shAuto-bisect with script (exit 0=good, 1=bad)git bisect resetEnd bisect session, return to HEADgit reflog --date=isoReflog with ISO timestampsgit reflog show branchReflog for a specific branchgit fsck --lost-foundFind dangling/unreachable objectsgit log --follow path/to/fileFollow file history across renamesgit diff branch1...branch2Changes since two branches divergedgit show HEAD:file.txtShow file contents at a specific commitConventional Commits
feat: add login endpointNew feature (triggers MINOR bump in semver)fix: correct null pointer errorBug fix (triggers PATCH bump)docs: update READMEDocumentation only changesstyle: format with prettierFormatting, no logic changerefactor: extract auth serviceRefactor without feature/fixtest: add user unit testsAdding or updating testschore: update dependenciesMaintenance tasksperf: cache database queriesPerformance improvementci: add GitHub Actions workflowCI/CD changesbuild: upgrade webpack to v5Build system changesBREAKING CHANGE: footerBreaking API change (triggers MAJOR bump)feat!: new APIShorthand for breaking change<type>(<scope>): <description>Optional scope: feat(auth): add OAuth