Basic Commands Cheatsheet
Setup & Init
| Command | Action |
|---|---|
git init | Initialize 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 --list | View all configuration |
Daily Workflow
| Command | Action |
|---|---|
git status | Show working directory state |
git status -s | Short format status |
git add <file> | Stage a specific file |
git add . | Stage all changes |
git add -p | Interactive staging (per hunk) |
git commit -m "msg" | Commit with message |
git commit -am "msg" | Stage tracked + commit |
git commit --amend | Modify last commit |
Viewing Changes
| Command | Action |
|---|---|
git diff | Unstaged changes |
git diff --staged | Staged changes (vs last commit) |
git diff HEAD | All changes (vs last commit) |
git diff branch1..branch2 | Compare two branches |
git diff --stat | Summary of changes |
History
| Command | Action |
|---|---|
git log | Full commit history |
git log --oneline | One line per commit |
git log --oneline --graph --all | Visual branch graph |
git log -n 5 | Last 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
| Command | Action |
|---|---|
git restore <file> | Discard unstaged changes |
git restore --staged <file> | Unstage a file |
git reset --soft HEAD~1 | Undo commit, keep staged |
git reset HEAD~1 | Undo commit, keep unstaged |
git reset --hard HEAD~1 | Undo commit, delete changes |
git revert <hash> | Create undo commit (safe) |
git reflog | View HEAD history (recovery) |
Stash
| Command | Action |
|---|---|
git stash | Save uncommitted changes |
git stash push -m "msg" | Save with description |
git stash list | List all stashes |
git stash pop | Restore and remove latest stash |
git stash apply | Restore (keep in stack) |
git stash drop | Delete latest stash |
Clean
| Command | Action |
|---|---|
git clean -n | Preview files to remove |
git clean -f | Remove untracked files |
git clean -fd | Remove untracked files + directories |