📡 You're offline — showing cached content
New version available!
Quick Access
Git Reference

Git & GitHub

Essential commands for version control and branching.

All Topics

Repository Setup

git init / git clone
Init new repo / clone existing. Clone automatically sets up origin remote and tracks default branch.
Example: git clone --depth=1 https://github.com/org/repo.git local-name
git remote -v
List remotes with fetch/push URLs. Add, rename, set-url, remove remotes.
Example: git remote add upstream https://github.com/original/repo.git
.gitignore patterns
/ prefix = root only. ** = any depth. ! = negate. Track with git check-ignore -v filename.
Example: node_modules/ .env* !.env.example *.log dist/ **/.DS_Store
git config --global
Set user name, email, default branch, editor, merge tool, aliases globally.
Example: git config --global user.email "you@example.com" core.autocrlf input
git alias
Create custom git commands — shorthand for long commands you use daily.
Example: git config --global alias.lg "log --oneline --graph --decorate --all"

Staging & Committing

git add -p (patch)
Interactively stage HUNKS of files — not whole files. Craft atomic commits with precision.
Example: git add -p src/auth.js # y/n/s(split)/e(edit)
git status -sb
Short branch+status format — fast overview of working tree state.
Example: git status -sb # ?? = untracked, M = modified, A = added
git commit --amend
Modify the LAST commit — change message or add forgotten changes. Only amend unpushed commits.
Example: git add missing.js && git commit --amend --no-edit
Conventional commits
type(scope): message format. Enables automated changelogs and semantic versioning.
Example: feat(auth): add Google OAuth login | fix(api): handle null user response
git diff --staged
Show diff of staged changes (what's about to be committed). Plain git diff shows unstaged.
Example: git diff HEAD~1 HEAD -- src/auth.js # diff specific file between commits
git stash push -m "name"
Stash uncommitted work. List, apply (keeps stash), or pop (removes stash). Stash untracked with -u.
Example: git stash push -u -m "WIP feature-x"; git stash pop stash@{1}

Branching & Merging

git switch / git restore
Modern replacements for git checkout. switch = branch ops, restore = file restore. Clearer intent.
Example: git switch -c feature/login; git restore --staged file.js
git merge --no-ff
Force a merge commit even when fast-forward is possible — preserves feature branch history.
Example: git merge --no-ff feature/auth -m "Merge feature/auth"
git rebase -i HEAD~N
Interactive rebase — squash, fixup, reorder, edit, drop commits. Clean up before opening PR.
Example: git rebase -i HEAD~5 # squash last 5 commits into one
git cherry-pick SHA
Apply specific commits from another branch without merging the whole branch.
Example: git cherry-pick abc123 def456 # pick two commits
git merge --squash
Stage all changes from feature branch as one pending commit — commits manually to keep history clean.
Example: git merge --squash feature/messy; git commit -m "feat: clean squashed merge"
Merge conflict resolution
Edit conflict markers <<<<<< HEAD ... ======= ... >>>>>>. Stage resolved files, then commit.
Example: git mergetool; git add resolved.js; git merge --continue
git branch -vv
Show all local branches with their upstream tracking branch and ahead/behind counts.
Example: git branch -vv # [origin/main: ahead 2, behind 1]

History & Inspection

git log --oneline --graph
Visualize branch history as ASCII graph. Filter by author, date, file, or commit message.
Example: git log --oneline --graph --all --decorate --since="2 weeks ago"
git log -S "text" (pickaxe)
Find commits that added or removed a specific string — trace when code was introduced.
Example: git log -S "password_hash" --source --all
git bisect start/good/bad
Binary search through history to find the commit that introduced a bug.
Example: git bisect start; git bisect bad HEAD; git bisect good v1.0.0; git bisect run ./test.sh
git blame -L 10,20 file
Show who last modified each line. -L limits to a range. -w ignores whitespace-only changes.
Example: git blame -w -C -L 50,70 src/auth.js
git reflog
Record of HEAD movements — recover from bad rebases, accidental branch deletions, hard resets.
Example: git reflog; git reset --hard HEAD@{3} # go back 3 moves
git shortlog -sn
Summarize commit count by author — sorted by number of commits.
Example: git shortlog -sn --no-merges --since="1 year ago"

Undoing & Reset

git reset --soft HEAD~1
Undo last commit but keep changes STAGED. Perfect for recommitting differently.
Example: git reset --soft HEAD~3 # collapse last 3 commits, keep staged
git reset --mixed HEAD~1
Default reset — undo commit and unstage changes. Files remain modified in working tree.
Example: git reset HEAD~1 # equivalent to --mixed
git reset --hard HEAD~1
Nuke commit AND working tree changes. IRREVERSIBLE (unless in reflog). Use with caution.
Example: git reset --hard origin/main # sync to remote (loses local work!)
git revert SHA
Creates a NEW commit that undoes a previous commit — safe for shared/main branches.
Example: git revert abc123..def456 --no-commit; git revert --continue
git clean -fdx
Remove untracked files (-f), directories (-d), and ignored files (-x). Add -n for dry run first.
Example: git clean -fdn # preview; git clean -fdx # nuke all untracked

Tags, Releases & Hooks

git tag -a v1.0.0 -m "msg"
Annotated tags store tagger info, date, message — use for releases. Lightweight tags are just refs.
Example: git tag -a v2.0.0 -m "Release v2.0.0"; git push origin v2.0.0
git push --tags / push origin tag
Tags are NOT pushed automatically. Push all tags or a specific tag explicitly.
Example: git push origin --follow-tags # push commits + annotated tags
git hooks (pre-commit, pre-push)
Shell scripts in .git/hooks triggered on git events — run linters, tests, format checks.
Example: .git/hooks/pre-commit #!/bin/bash; npm run lint || exit 1
git worktree add
Check out multiple branches simultaneously into separate directories — no stashing needed.
Example: git worktree add ../hotfix-v2 release/v2
git archive
Export source tree at a specific commit/tag as tar or zip without .git history.
Example: git archive --format=tar.gz HEAD > source-v1.0.tar.gz