Git & GitHub Cheat Sheet
Every command, diagram, and hard-won lesson you actually reach for day to day - from your first git init to rescuing a branch you thought you broke.
Mental model
The three (really four) trees
Almost every Git command just moves a change between these places. Once this clicks, the rest of Git is mostly vocabulary.
Before you start
Setup & configuration
One-time-per-machine commands.
Identity & defaults
git config --global user.name "Your Name"Sets the name attached to your commits.
git config --global user.email "you@example.com"Sets the email attached to your commits — match your GitHub account email.
git config --global init.defaultBranch mainMakes new repos default to `main` instead of `master`.
git config --listShows all active config values and where they came from.
Starting a repository
git initTurns the current folder into a new Git repository.
git clone <url>Downloads a full copy of a remote repository, including history.
git clone <url> <folder>Clones into a specific folder name instead of the repo's default.
git clone --depth 1 <url>Shallow clone — only the latest commit, useful for huge repos.
Daily loop
Everyday commands
The status → add → commit loop you'll run dozens of times a day.
Check & review
git statusShows staged, unstaged, and untracked changes.
git diffShows unstaged changes, line by line.
git diff --stagedShows changes that are staged but not yet committed.
git show <commit>Shows the full diff and metadata of a specific commit.
Stage & commit
git add <file>Stages a specific file's changes.
git add .Stages every change in the current directory.
git add -pInteractively stage changes hunk by hunk — great for clean commits.
git commit -m "message"Commits staged changes with a message.
git commit -am "message"Stages every tracked file's changes and commits in one step.
Parallel work
Branching & merging
Branches are just movable pointers to commits — cheap to create, cheap to throw away.
Branching
git branchLists local branches.
git branch <name>Creates a new branch (doesn't switch to it).
git switch <name>Switches to an existing branch (modern replacement for checkout).
git switch -c <name>Creates and switches to a new branch in one step.
git branch -d <name>Deletes a branch that's already merged.
git branch -D <name>Force-deletes a branch, merged or not.
Merging & rebasing
git merge <branch>Merges another branch into the current one, keeping full history.
git rebase <branch>Replays your commits on top of another branch — linear history.
git rebase -i HEAD~3Interactive rebase: reorder, squash, or reword the last 3 commits.
git rebase --continueContinues a rebase after resolving a conflict.
git rebase --abortBails out and restores the branch to before the rebase started.
Merge vs. rebase, in one line
Syncing
Working with remotes
Your local repo talks to remotes (usually GitHub) with fetch, pull, and push.
Remotes
git remote -vLists remotes and their URLs.
git remote add origin <url>Connects the local repo to a remote named 'origin'.
git fetchDownloads remote changes without merging them into your branch.
git fetch --all --pruneFetches all remotes and removes references to deleted remote branches.
Pull & push
git pullFetches and merges remote changes into your current branch.
git pull --rebaseFetches and rebases your local commits on top — avoids merge clutter.
git pushUploads your commits to the remote branch.
git push -u origin <branch>Pushes and sets the remote as the branch's upstream, so future `git push` just works.
git push origin --delete <branch>Deletes a branch on the remote.
Rescue
Undoing things
Mistakes are normal. Match the command to how far the change has traveled.
Safe undos
git restore <file>Discards unstaged changes to a file, restoring it to the last commit.
git restore --staged <file>Unstages a file without touching its contents.
git commit --amendEdits the message (or adds staged changes to) the last commit.
git revert <commit>Creates a new commit that undoes a previous one — safe on shared branches.
Rewriting history (careful)
git reset --soft HEAD~1Undoes the last commit, keeps changes staged.
git reset --mixed HEAD~1Undoes the last commit, keeps changes unstaged (default mode).
git reset --hard HEAD~1Undoes the last commit and deletes the changes entirely. Only do this on commits nobody else has pulled.
git reflogShows a log of everywhere HEAD has pointed — your safety net after a bad reset.
Stash
git stashShelves your uncommitted changes and cleans the working directory.
git stash -uAlso stashes untracked files.
git stash listLists all stashed sets of changes.
git stash popRe-applies the most recent stash and removes it from the stash list.
git stash applyRe-applies a stash but keeps it in the list — useful across branches.
git stash dropDeletes a stash without applying it.
Tags & history
git tagLists existing tags.
git tag v1.0.0Creates a lightweight tag on the current commit.
git tag -a v1.0.0 -m "message"Creates an annotated tag — preferred for releases.
git push origin --tagsPushes all local tags to the remote.
git log --oneline --graph --allA compact, visual view of branch and merge history.
git blame <file>Shows who last changed each line of a file, and in which commit.
$ git log --oneline --graph --all
* 7e4f2a1 (HEAD -> main) Merge pull request #42 |\ | * 9c3d8b2 (feature/login) Add login validation | * a1b2c3d Add login form |/ * 5f6e7d8 Update README * 3a2b1c0 Initial commit
Beyond Git
GitHub & pull requests
Git is the tool; GitHub is a hosting service and collaboration layer built on top of it.
Git
A version control tool that runs on your machine. Tracks history, branches, and diffs — works with no internet connection.
GitHub
A cloud service that hosts Git repositories and adds collaboration tools: pull requests, issues, code review, and CI/CD (Actions).
Fork & pull request flow
gh repo fork <owner>/<repo> --cloneForks a repo to your account and clones it locally (GitHub CLI).
git remote add upstream <url>Adds the original repo as a second remote so you can pull its updates.
git fetch upstream && git merge upstream/mainSyncs your fork with the original repo's latest changes.
gh pr createOpens a pull request from your current branch.
gh pr listLists open pull requests in the repo.
gh pr checkout <number>Checks out someone else's PR locally to review or test it.
Issues & repos (gh CLI)
gh repo clone <owner>/<repo>Clones a repo using the GitHub CLI (handles auth for you).
gh issue createOpens a new issue interactively.
gh issue list --assignee @meLists issues assigned to you.
gh pr view --webOpens the current branch's PR in your browser.
gh run listLists recent GitHub Actions workflow runs.
Putting it together
A typical feature-branch workflow
Branch off main, commit as you go, open a PR, merge, tag a release.
- 1.
git switch -c feature/login— branch off the latest main. - 2. Commit in small, reviewable chunks as you build.
- 3.
git push -u origin feature/loginthengh pr create. - 4. Address review comments with more commits, or an interactive rebase to tidy up.
- 5. Merge the PR, delete the branch, tag a release if it ships.
Habits that save you
Important things to keep in mind
Write commits in the imperative mood
git log and matches Git's own generated messages (e.g. "Merge branch...").Never rewrite shared history
rebase or reset --hard on that branch. Use git push --force-with-lease at most, and only on branches you own alone.Set up .gitignore before your first commit
node_modules/, .env, and build output from the start than to untrack them later with git rm -r --cached ..Committed secrets don't just 'go away'
git filter-repo or the BFG Repo-Cleaner to scrub history.Pull before you push
git pull (or --rebase) before pushing avoids most "non-fast-forward" rejections and surfaces conflicts early, while they're small.A detached HEAD isn't broken
git switch -c new-branch-name before switching away.Troubleshooting
Common mistakes & how to fix them
| Situation | Fix |
|---|---|
| Committed to the wrong branch | git branch new-branch → git reset --hard HEAD~1 on the original → git switch new-branch |
| Need to change the last commit message | git commit --amend |
| Merge conflict on a pull | Open the flagged files, resolve the <<<<<<< / ======= / >>>>>>> markers, then git add . && git commit |
| Accidentally deleted a branch | git reflog to find its last commit hash, then git branch <name> <hash> |
| A large or sensitive file got committed | Remove with git filter-repo or BFG, then add the path to .gitignore |
| Force push overwrote a teammate's work | They can recover via their own git reflog; going forward use --force-with-lease and coordinate before force-pushing shared branches |