Getting started

Command What it does
git init Turns the current folder into a new Git repository
git clone <url> Copies a remote repository to your machine
git status Shows what's changed and what's staged
git config --global user.name "<name>" Sets your name for commits (add user.email too)

Everyday workflow

Command What it does
git add <file> Stages a file for the next commit (git add . stages everything)
git commit -m "<message>" Saves the staged changes with a message
git push Sends your commits to the remote repository
git pull Fetches and merges the latest changes from the remote
git log Shows the commit history (git log --oneline for a compact view)
git diff Shows unstaged changes line by line

Branching and switching

Command What it does
git branch Lists branches (git branch <name> creates one)
git switch <branch> Switches to an existing branch
git switch -c <branch> Creates a new branch and switches to it
git merge <branch> Merges another branch into your current one

<aside> 💡

You'll still see git checkout everywhere in older tutorials. git switch and git restore do the same jobs more clearly, one for branches and one for files, so they're the better habit to build now.

</aside>

Undoing things

Command What it does
git restore <file> Discards uncommitted changes to a file
git restore --staged <file> Unstages a file without losing the changes
git reset --soft HEAD~1 Undoes the last commit but keeps the changes staged
git revert <commit> Creates a new commit that undoes a previous one, safely, even after pushing

Collaboration

Command What it does
git remote -v Shows the remotes your repo is connected to
git fetch Downloads changes from the remote without merging them
git stash Temporarily shelves uncommitted changes so you can switch tasks
gh pr create Opens a pull request from the terminal (needs the GitHub CLI)