Essential commands for version control and branching.
git init / git clonegit clone --depth=1 https://github.com/org/repo.git local-namegit remote -vgit remote add upstream https://github.com/original/repo.git.gitignore patternsnode_modules/ .env* !.env.example *.log dist/ **/.DS_Storegit config --globalgit config --global user.email "you@example.com" core.autocrlf inputgit aliasgit config --global alias.lg "log --oneline --graph --decorate --all"git add -p (patch)git add -p src/auth.js # y/n/s(split)/e(edit)git status -sbgit status -sb # ?? = untracked, M = modified, A = addedgit commit --amendgit add missing.js && git commit --amend --no-editConventional commitsfeat(auth): add Google OAuth login | fix(api): handle null user responsegit diff --stagedgit diff HEAD~1 HEAD -- src/auth.js # diff specific file between commitsgit stash push -m "name"git stash push -u -m "WIP feature-x"; git stash pop stash@{1}git switch / git restoregit switch -c feature/login; git restore --staged file.jsgit merge --no-ffgit merge --no-ff feature/auth -m "Merge feature/auth"git rebase -i HEAD~Ngit rebase -i HEAD~5 # squash last 5 commits into onegit cherry-pick SHAgit cherry-pick abc123 def456 # pick two commitsgit merge --squashgit merge --squash feature/messy; git commit -m "feat: clean squashed merge"Merge conflict resolutiongit mergetool; git add resolved.js; git merge --continuegit branch -vvgit branch -vv # [origin/main: ahead 2, behind 1]git log --oneline --graphgit log --oneline --graph --all --decorate --since="2 weeks ago"git log -S "text" (pickaxe)git log -S "password_hash" --source --allgit bisect start/good/badgit bisect start; git bisect bad HEAD; git bisect good v1.0.0; git bisect run ./test.shgit blame -L 10,20 filegit blame -w -C -L 50,70 src/auth.jsgit refloggit reflog; git reset --hard HEAD@{3} # go back 3 movesgit shortlog -sngit shortlog -sn --no-merges --since="1 year ago"git reset --soft HEAD~1git reset --soft HEAD~3 # collapse last 3 commits, keep stagedgit reset --mixed HEAD~1git reset HEAD~1 # equivalent to --mixedgit reset --hard HEAD~1git reset --hard origin/main # sync to remote (loses local work!)git revert SHAgit revert abc123..def456 --no-commit; git revert --continuegit clean -fdxgit clean -fdn # preview; git clean -fdx # nuke all untrackedgit tag -a v1.0.0 -m "msg"git tag -a v2.0.0 -m "Release v2.0.0"; git push origin v2.0.0git push --tags / push origin taggit push origin --follow-tags # push commits + annotated tagsgit hooks (pre-commit, pre-push).git/hooks/pre-commit #!/bin/bash; npm run lint || exit 1git worktree addgit worktree add ../hotfix-v2 release/v2git archivegit archive --format=tar.gz HEAD > source-v1.0.tar.gz