cheatsheet/git-cheatsheet.md
2026-06-14 12:14:49 +02:00

4.2 KiB

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 <url> Download an existing repo from GitHub etc. git clone https://github.com/user/repo.git

Everyday Workflow

  ┌────────┐    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 <file> 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 <file> Discard unstaged changes in a file git restore main.cpp
git restore --staged <file> 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 <name> Create a new branch git branch feature-x
git checkout <branch> Switch to a branch git checkout feature-x
git switch <branch> Switch to a branch (newer syntax) git switch main
git checkout -b <name> Create and switch to a new branch git checkout -b feature-x
git merge <branch> Merge another branch into the current one git switch main && git merge feature-x
git branch -d <name> Delete a branch (safe) git branch -d feature-x

Working with GitHub (Remote)

Command What it does Example
git remote add origin <url> 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

git add .
git commit -m "Describe what you changed"

Scenario 2: Put it on GitHub

git push

Scenario 3: Get latest from GitHub

git pull

Scenario 4: Start a new feature (branch)

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

git restore broken_file.cpp

Cheat Sheet — Most Used Commands

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