# Git Cheat Sheet ## Setup (do once) | Command | What it does | Example | |---------|-------------|---------| | git config --global user.name "Your Name" | Set your name on commits | git config --global user.name "Alice" | | git config --global user.email "a@b.com" | Set your email on commits | git config --global user.email "alice@example.com" | ## Starting a Project | Command | What it does | Example | |---------|-------------|---------| | git init | Turn the current folder into a Git repo | cd my_project && git init | | git clone | Download an existing repo from GitHub etc. | git clone https://github.com/user/repo.git | ## Everyday Workflow ```text ┌────────┐ git add ┌────────┐ git commit ┌──────────┐ git push ┌────────┐ │ Working │ ──────────→ │ Staging │ ────────────→ │ Local │ ──────────→ │ Remote │ │ Tree │ │ Area │ │ Branch │ │ (GitHub)│ └────────┘ └────────┘ └──────────┘ └────────┘ ``` | Command | What it does | Example | |---------|-------------|---------| | git status | See what files changed and what's staged | git status | | git add | Stage a file for commit | git add main.cpp | | git add . | Stage **all** changed files | git add . | | git commit -m "message" | Save staged changes with a message | git commit -m "Add login feature" | | git log | Show commit history | git log --oneline (compact view) | | git diff | Show unstaged changes (line by line) | git diff | ## Undoing Things | Command | What it does | Example | |---------|-------------|---------| | git restore | Discard unstaged changes in a file | git restore main.cpp | | git restore --staged | Unstage a file (keep your edits) | git restore --staged main.cpp | | git commit --amend -m "new msg" | Fix the last commit message | git commit --amend -m "Fixed message" | | git reset --hard HEAD | Throw away **all** uncommitted changes ⚠️ | Use with care! | ## Branches | Command | What it does | Example | |---------|-------------|---------| | git branch | List branches (* = current one) | git branch | | git branch | Create a new branch | git branch feature-x | | git checkout | Switch to a branch | git checkout feature-x | | git switch | Switch to a branch (newer syntax) | git switch main | | git checkout -b | Create **and** switch to a new branch | git checkout -b feature-x | | git merge | Merge another branch into the current one | git switch main && git merge feature-x | | git branch -d | Delete a branch (safe) | git branch -d feature-x | ## Working with GitHub (Remote) | Command | What it does | Example | |---------|-------------|---------| | git remote add origin | Link your local repo to GitHub | git remote add origin https://github.com/alice/my-project.git | | git push -u origin main | Push commits to GitHub (first time) | git push -u origin main | | git push | Push commits (after first time) | git push | | git pull | Fetch and merge the latest from GitHub | git pull | | git fetch | Download updates from GitHub (don't merge) | git fetch | ## Common Scenarios ### Scenario 1: Save your work ```bash git add . git commit -m "Describe what you changed" ``` ### Scenario 2: Put it on GitHub ```bash git push ``` ### Scenario 3: Get latest from GitHub ```bash git pull ``` ### Scenario 4: Start a new feature (branch) ```bash git checkout -b my-new-feature # ... make changes ... git add . git commit -m "Add my new feature" git push -u origin my-new-feature ``` ### Scenario 5: Undo changes in a file before committing ```bash git restore broken_file.cpp ``` ## Cheat Sheet — Most Used Commands ```bash git status # What's going on? git add . # Stage everything git commit -m "msg" # Save snapshot git push # Upload to GitHub git pull # Download from GitHub git log --oneline # See history ```