Skip to main content

Basic Commands Cheatsheet

Setup & Init

CommandAction
git initInitialize a new repository
git clone <url>Clone a remote repository
git clone --depth 1 <url>Shallow clone (latest only)
git config --global user.name "Name"Set your name
git config --global user.email "email"Set your email
git config --listView all configuration

Daily Workflow

CommandAction
git statusShow working directory state
git status -sShort format status
git add <file>Stage a specific file
git add .Stage all changes
git add -pInteractive staging (per hunk)
git commit -m "msg"Commit with message
git commit -am "msg"Stage tracked + commit
git commit --amendModify last commit

Viewing Changes

CommandAction
git diffUnstaged changes
git diff --stagedStaged changes (vs last commit)
git diff HEADAll changes (vs last commit)
git diff branch1..branch2Compare two branches
git diff --statSummary of changes

History

CommandAction
git logFull commit history
git log --onelineOne line per commit
git log --oneline --graph --allVisual branch graph
git log -n 5Last 5 commits
git log --author="name"Filter by author
git log --since="2 weeks ago"Filter by date
git log -- <file>History of a specific file
git show <hash>Show a specific commit
git blame <file>Who changed each line

Undoing

CommandAction
git restore <file>Discard unstaged changes
git restore --staged <file>Unstage a file
git reset --soft HEAD~1Undo commit, keep staged
git reset HEAD~1Undo commit, keep unstaged
git reset --hard HEAD~1Undo commit, delete changes
git revert <hash>Create undo commit (safe)
git reflogView HEAD history (recovery)

Stash

CommandAction
git stashSave uncommitted changes
git stash push -m "msg"Save with description
git stash listList all stashes
git stash popRestore and remove latest stash
git stash applyRestore (keep in stack)
git stash dropDelete latest stash

Clean

CommandAction
git clean -nPreview files to remove
git clean -fRemove untracked files
git clean -fdRemove untracked files + directories