Skip to content

Shell Cheatsheet

Quick reference for daily command-line operations.

Git

# Status and info
git status                  # Working tree status
git status -sb              # Short format
git log --oneline -10       # Recent commits
git diff                    # Unstaged changes
git diff --staged           # Staged changes

# Branching
git checkout -b feature     # Create and switch
git switch -c feature       # Modern alternative
git branch -d feature       # Delete merged branch
git branch -D feature       # Force delete

# Commits
git add -p                  # Interactive staging
git commit -m "message"     # Commit
git commit --amend          # Amend last commit

# Remote
git push -u origin feature  # Push new branch
git pull --rebase          # Pull with rebase
git push --force-with-lease # Safe force push

# Undo
git restore file.py         # Discard changes
git restore --staged file   # Unstage
git reset HEAD~1            # Undo commit, keep changes
git reset --hard HEAD~1     # Undo commit, discard changes

# Stash
git stash                   # Stash changes
git stash pop               # Apply and remove
git stash list              # List stashes

Docker

# Containers
docker ps                   # Running containers
docker ps -a                # All containers
docker logs -f container    # Follow logs
docker exec -it container bash  # Shell access

# Images
docker images               # List images
docker build -t name .      # Build image
docker image prune -a       # Remove unused

# Compose
docker compose up -d        # Start detached
docker compose down -v      # Stop with volumes
docker compose logs -f api  # Follow service logs
docker compose exec api bash # Shell into service

# Cleanup
docker system prune -af --volumes  # Remove all unused

Python (uv)

# Dependencies
uv add package              # Add dependency
uv add --dev package        # Add dev dependency
uv sync                     # Sync dependencies
uv lock --upgrade           # Update lockfile

# Running
uv run python script.py     # Run script
uv run pytest               # Run tests
uv run alembic upgrade head # Run migration

Node (Bun)

# Dependencies
bun install                 # Install all
bun add package             # Add dependency
bun add -D package          # Add dev dependency
bun update                  # Update packages

# Scripts
bun run dev                 # Start dev server
bun run build               # Build
bun test                    # Run tests

Testing

# pytest
uv run pytest               # Run all
uv run pytest -x            # Stop on first fail
uv run pytest --lf          # Last failed only
uv run pytest -k "pattern"  # Filter by name
uv run pytest --cov=app     # With coverage
uv run pytest -n auto       # Parallel

# Debugging
uv run pytest --pdb         # Drop into debugger
uv run pytest -s            # Show print output

Database

# Connection
psql -h localhost -U postgres -d dbname
pgcli -h localhost -U postgres -d dbname

# Migrations
uv run alembic current      # Current revision
uv run alembic upgrade head # Apply all
uv run alembic downgrade -1 # Rollback one
uv run alembic revision --autogenerate -m "message"

# Backup/Restore
pg_dump -Fc dbname > backup.dump
pg_restore -d dbname backup.dump

File Operations

# Find files
fd "\.py$"                  # Find Python files
fd -e py -e pyi             # Multiple extensions
fd -H ".env"                # Include hidden

# Search content
rg "pattern"                # Search text
rg "pattern" -t py          # Filter by type
rg "pattern" -l             # Files only
rg "pattern" -C 3           # With context

# View files
bat file.py                 # With syntax highlighting
cat file.json | jq .        # Parse JSON
less +F file.log            # Follow file

Process Management

# Find
ps aux | grep process
pgrep -f "pattern"
lsof -i :8000               # Find process on port

# Kill
kill pid
kill -9 pid                 # Force kill
pkill -f "pattern"
kill $(lsof -t -i:8000)     # Kill process on port

Network

# HTTP requests
curl url                    # GET request
curl -X POST -d '{}' url    # POST request
curl -v url                 # Verbose
curl -I url                 # Headers only

# Using httpie
http url                    # GET
http POST url key=value     # POST JSON

# Ports
nc -zv host port            # Check port
lsof -i :port               # Find process

Text Processing

# jq (JSON)
cat file.json | jq .        # Pretty print
cat file.json | jq '.key'   # Extract key
cat file.json | jq '.[] | .name'  # Array map

# cut/awk
cut -d',' -f2 file.csv      # Extract column
awk '{print $1}' file       # First field

# sort/uniq
sort file | uniq -c         # Count occurrences
sort -n file                # Numeric sort
sort -r file                # Reverse sort

fzf

# Basic
fzf                         # Find files
cmd | fzf                   # Pipe anything

# Keybindings (after install)
Ctrl+R                      # Search history
Ctrl+T                      # Search files
Alt+C                       # Search directories

tmux

# Sessions
tmux                        # New session
tmux new -s name            # Named session
tmux attach -t name         # Attach
tmux ls                     # List sessions

# Prefix: Ctrl+b
d                           # Detach
c                           # New window
n/p                         # Next/prev window
%                           # Split vertical
"                           # Split horizontal
arrow                       # Navigate panes
z                           # Zoom pane
x                           # Kill pane

Useful Aliases

# Git
alias g="git"
alias gs="git status"
alias gc="git commit"
alias gp="git push"
alias gl="git pull"

# Docker
alias dc="docker compose"
alias dcu="docker compose up -d"
alias dcd="docker compose down"
alias dcl="docker compose logs -f"

# Navigation
alias ..="cd .."
alias ...="cd ../.."

# Project
alias dev="uv run uvicorn app.main:app --reload"
alias test="uv run pytest"
alias lint="uv run ruff check --fix ."

Environment Variables

# Set for command
VAR=value command

# Export for session
export VAR=value

# Load from file
source .env
export $(cat .env | xargs)

# direnv (auto-load)
echo 'export VAR=value' > .envrc
direnv allow

Shell History

Ctrl+R                      # Search history
!!                          # Repeat last command
!$                          # Last argument
!*                          # All arguments
history | grep pattern      # Search history

Keyboard Shortcuts

Ctrl+A                      # Beginning of line
Ctrl+E                      # End of line
Ctrl+U                      # Clear line before cursor
Ctrl+K                      # Clear line after cursor
Ctrl+W                      # Delete word before
Alt+D                       # Delete word after
Ctrl+L                      # Clear screen
Ctrl+C                      # Cancel command
Ctrl+Z                      # Suspend (fg to resume)