Setup & Config
| Command | Description |
|---|
git config --global user.name "Name" | Set username |
git config --global user.email "email@example.com" | Set email |
git config --list | List all config |
Basic Workflow
| Command | Description |
|---|
git init | Initialize repo |
git add <file> / git add . | Stage files |
git commit -m "msg" | Commit staged |
git status | Working tree status |
git log | View commit history |
Branching
| Command | Description |
|---|
git branch | List branches |
git branch <name> | Create branch |
git checkout <branch> | Switch branch |
git switch <branch> | Switch (modern) |
git merge <branch> | Merge into current |
git rebase <branch> | Rebase onto branch |
Remote Operations
| Command | Description |
|---|
git remote -v | List remotes |
git remote add origin <url> | Add remote |
git fetch | Fetch remote refs |
git pull | Fetch + merge |
git push | Push to remote |
git clone <url> | Clone repo |
Stashing
git stash # Stash changes
git stash list # List stashes
git stash pop # Apply & remove most recent
git stash apply # Apply, keep stash
git stash drop # Remove stash
Undoing Changes
| Command | Description |
|---|
git reset --soft HEAD~1 | Undo commit, keep changes staged |
git reset --mixed HEAD~1 | Undo commit, unstage changes |
git reset --hard HEAD~1 | Undo commit, discard changes |
git revert <commit> | New commit that undoes |
git checkout -- <file> | Discard file changes |
git restore <file> | Restore file (modern) |
Tagging
git tag v1.0.0 # Lightweight tag
git tag -a v1.0.0 -m "msg" # Annotated tag
git push origin v1.0.0 # Push tag
Log & Diff
| Command | Description |
|---|
git log --oneline | Compact one-line log |
git log -p | Log with patches |
git log --graph | Graph view |
git diff | Unstaged changes |
git diff --staged | Staged changes |
git diff <commit> | Diff vs commit |
Advanced
| Command | Description |
|---|
git cherry-pick <commit> | Apply single commit |
git bisect start | Start bisect search |
git bisect good/bad | Mark bisect state |
git reflog | Reference log (recovery) |