A quick, task-oriented Git reference. Pair this with the in-depth guide for concepts and best practices.
Minimal Mental Model#
graph LR
WD[Working Dir] -- add --> ST[Staging]
ST -- commit --> REPO[Local Repo]
REPO -- push --> ORI[Origin]
ORI -- fetch/pull --> REPOSetup#
git --version
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch mainCreate or Clone#
git init
git clone <url>Status and Diffs#
git status
git diff # unstaged
git diff --staged # staged vs HEADStage and Commit#
git add <path>
git add -p # interactive hunks
git commit -m "feat: message"
git commit --amend # edit last commitBranching#
git branch
git switch -c feature/x
git switch main
git branch -d feature/xSync with Remote#
git remote -v
git fetch
git pull # merge
git pull --rebase # rebase
git push -u origin my-branchMerge vs Rebase#
git switch my-branch && git merge main
git switch my-branch && git rebase mainResolve Conflicts#
git status
# edit files, remove markers
git add <file>
git commit # after merge
git rebase --continue # during rebaseStash Work#
git stash push -m "wip"
git stash list
git stash popUndo Safely#
git restore --staged <file> # unstage
git restore <file> # discard local edits
git revert <sha> # new commit to undo
git reset --soft HEAD~1 # keep changes, drop last commit
git reflog # find lost commitsTags and Releases#
git tag -a v1.0.0 -m "msg"
git push --tagsIgnore and Clean#
echo "node_modules/" >> .gitignore
git clean -fdx # dangerous: removes untracked filesAuthentication (Quick)#
# HTTPS + PAT
git clone https://github.com/owner/repo.git
# SSH
ssh-keygen -t ed25519 -C "you@example.com"
ssh-add ~/.ssh/id_ed25519
git clone git@github.com:owner/repo.gitConventional Commits (Optional)#
feat(auth): add oauth login
fix(api): handle null pointer in user service
chore(ci): update node to 20Common One-Liners#
# See last commit summary
git log -1 --stat
# Interactive rebase last 5 commits
git rebase -i HEAD~5
# Squash branch onto main
git switch my-branch && git rebase -i mainQuick PR Flow (GitHub)#
git switch -c feat/x
# edit, add, commit
git push -u origin feat/x
# open PR on GitHubSee also: the full guide “The Definitive Guide to Version Control with Git and GitHub”.