Git Command Cheatsheet

Quick reference for common Git commands, covering basics, branching, remotes, undo, and more。 Runs locally—nothing uploaded.

Basic

git init— Initialize a new repositoryClick to copy
git clone <url>— Clone a remote repositoryClick to copy
git status— Show working tree statusClick to copy
git add <file>— Stage file(s) for commitClick to copy
git add .— Stage all changesClick to copy
git commit -m "msg"— Commit staged changesClick to copy
git diff— Show unstaged changesClick to copy
git diff --staged— Show staged changesClick to copy

Branch

git branch— List local branchesClick to copy
git branch <name>— Create a new branchClick to copy
git checkout <branch>— Switch to branchClick to copy
git checkout -b <name>— Create & switch to branchClick to copy
git switch <branch>— Switch to branch (new)Click to copy
git switch -c <name>— Create & switch (new)Click to copy
git merge <branch>— Merge branch into currentClick to copy
git branch -d <name>— Delete a branchClick to copy
git branch -m <old> <new>— Rename a branchClick to copy

Remote

git remote -v— List remote repositoriesClick to copy
git remote add <name> <url>— Add a remoteClick to copy
git fetch— Fetch from remoteClick to copy
git pull— Fetch & mergeClick to copy
git push— Push to remoteClick to copy
git push -u origin <branch>— Push & set upstreamClick to copy
git remote remove <name>— Remove a remoteClick to copy

Undo/Reset

git reset HEAD <file>— Unstage a fileClick to copy
git checkout -- <file>— Discard working changesClick to copy
git restore <file>— Discard working changes (new)Click to copy
git reset --soft HEAD~1— Undo last commit, keep stagedClick to copy
git reset --mixed HEAD~1— Undo last commit, keep unstagedClick to copy
git reset --hard HEAD~1— Undo last commit, discard allClick to copy
git revert <commit>— Create undo commitClick to copy

Stash

git stash— Stash current changesClick to copy
git stash list— List stashesClick to copy
git stash pop— Apply & remove latest stashClick to copy
git stash apply— Apply latest stashClick to copy
git stash drop— Remove latest stashClick to copy
git stash push -m "msg"— Stash with a messageClick to copy

Log

git log— Show commit historyClick to copy
git log --oneline— Compact commit historyClick to copy
git log --graph— Graph of branchesClick to copy
git log -n <count>— Show last N commitsClick to copy
git show <commit>— Show commit detailsClick to copy
git blame <file>— Line-by-line historyClick to copy

Tag

git tag— List tagsClick to copy
git tag <name>— Create lightweight tagClick to copy
git tag -a <name> -m "msg"— Create annotated tagClick to copy
git tag -d <name>— Delete a tagClick to copy
git push origin --tags— Push tags to remoteClick to copy