Cheat sheet · 5 min read

Git & GitHub for everyday work

A friendly reference for the commands you actually use day to day: editing files, committing your changes, pushing to GitHub, opening pull requests, merging, and politely undoing mistakes. Tap any $ command to copy it, then paste it into your terminal - or back into chat to ask what it does.

1The mental model

Your changes pass through four places. Knowing which one your work is in is half the battle.

Where your changes live

1 · Working dir
Your files
What you see in your editor. Edits live here until you stage them.
2 · Staging area
Ready to commit
A shortlist of changes you've picked with git add to go into the next commit.
3 · Local repo
Your commits
Once you git commit, your snapshot is saved locally with a message.
4 · Remote (GitHub)
Shared with the world
git push uploads your commits so teammates can pull them.
The one-line summary: edit → git addgit commit -m "…"git push. Everything else is a variation on that.

2First-time setup

Run these once per machine so git knows who you are.

CommandPurposeComments
git config --global user.name "Your Name" Tell git your name TipShown on every commit you make.
git config --global user.email "you@example.com" Tell git your email Use the same email as your GitHub account so commits link to your profile.
git config --global init.defaultBranch main Default new repos to main Matches GitHub's default. Avoids the old master name.
git clone https://github.com/user/repo.git Download a GitHub repo GitHubCreates a folder with the project + full history.
git init Start a new repo here Use in an empty folder, or one you want to start tracking.

3The daily loop

The five commands you'll run more than anything else.

CommandPurposeComments
git status What's changed? TipRun this constantly. It tells you what's staged, modified, and untracked.
git diff See unstaged edits Press q to quit. Add --staged to see what's about to be committed.
git add file.txt Stage a specific file Stages the file for the next commit. You can list multiple files.
git add . Stage everything changed CarefulEasy to accidentally include secrets or junk - run git status first.
git commit -m "Add login form" Save a snapshot Write a short, present-tense message describing the change.
git commit -am "fix typo" Stage + commit tracked files Shortcut. Doesn't include brand-new (untracked) files.

4Branches

A branch is a separate line of work. Make one per feature or fix, so main stays stable.

CommandPurposeComments
git branch List local branches The star (*) shows the one you're on.
git switch -c feature/login Create + switch to a new branch TipUse a short, descriptive name. Slashes are fine for grouping.
git switch main Switch to an existing branch Commit or stash any in-progress work first.
git branch -d feature/login Delete a branch (safe) Refuses if the branch has unmerged work. Use after merging.
git branch -D feature/login Force-delete a branch DangerThrows away unmerged commits forever. Be sure.

5Pushing & pulling

Syncing your local repo with GitHub.

CommandPurposeComments
git push Upload your commits Pushes the current branch to GitHub. Requires the branch to track a remote.
git push -u origin feature/login First push of a new branch TipThe -u sets the upstream so future git push/pull "just work".
git pull Get the latest from GitHub Run this before starting work each day. Fetches + merges remote changes.
git fetch Download without merging Useful when you want to look before integrating.
git push --force-with-lease Safe-ish force push CarefulOnly after rebasing your own branch. Refuses if a teammate pushed in the meantime.

6Pull requests & merging

A pull request (PR) is GitHub's way of saying "please review and merge my branch." The flow is: branch → push → open PR → review → merge → delete the branch.

CommandPurposeComments
gh pr create --fill Open a PR from the terminal GitHub CLIUses your last commit as the title and body. Needs gh installed.
gh pr create --web Open PR form in browser Skips the prompts and lets you fill it on github.com.
gh pr status See your open PRs Shows PRs you opened and ones waiting on your review.
gh pr checkout 42 Check out someone else's PR locally Great for testing a teammate's branch before approving.
git switch main Switch to main before merging For local merges. (On GitHub, you usually click the green "Merge" button instead.)
git merge feature/login Merge a branch into your current one Creates a merge commit. Resolve conflicts if git asks.
git pull origin main Catch up before merging TipPull main into your feature branch before opening a PR to reduce conflicts.
The PR workflow, in plain English:
  1. Make a branch: git switch -c feature/login
  2. Do your work, commit as you go.
  3. Push: git push -u origin feature/login
  4. Open a PR - on github.com or with gh pr create.
  5. Teammates review; you push more commits to address feedback.
  6. When approved, click Merge pull request. Delete the branch.
  7. Back on main: git switch main && git pull to grab the merged work.

7Undoing things

Almost everything in git is recoverable. Match the command to which place the change is in.

CommandPurposeComments
git restore file.txt Throw away unstaged edits to a file DangerCannot be undone - the edits are gone.
git restore --staged file.txt Unstage a file Keeps your edits, just removes them from the next commit.
git commit --amend -m "better message" Fix your last commit CarefulOnly amend commits you haven't pushed yet.
git reset --soft HEAD~1 Undo last commit, keep changes staged Great when you committed too early. HEAD~1 means "one commit back."
git reset --hard HEAD~1 Undo last commit AND throw away edits DangerWipes uncommitted work. There's no undo for hard reset.
git revert abc1234 Undo a commit by making a new one TipSafe for commits you've already pushed - doesn't rewrite history.
git reflog "Undo button" for everything Lists every recent state of your branch. You can git reset --hard back to any of them.
Reset vs. revert - which one? Use revert on a branch other people might already have pulled (like main). It adds a new "undo" commit, so history stays linear and safe. Use reset for tidying up local commits you haven't pushed yet.

8Looking at history

CommandPurposeComments
git log --oneline Compact commit list Press q to quit. Add --graph to see branch shapes.
git log --oneline --graph --all Visualize all branches My favorite for getting your bearings after a confusing day.
git show abc1234 See exactly what a commit changed Use the short hash from git log.
git blame file.txt Who wrote each line? Helpful when investigating a bug - not for assigning blame!

9Stashing in-progress work

When you need to switch branches but your edits aren't commit-ready.

CommandPurposeComments
git stash Tuck away your unstaged work Your files reset to clean. Your changes are safe in the stash.
git stash pop Put the stash back Reapplies the most recent stash and removes it from the list.
git stash list See all your stashes Each one has an index like stash@{0}.
git stash drop stash@{0} Delete a stash you don't need CarefulGone for good.

10When you're stuck

A short list of "what do I do now?" moves.

SituationTry thisWhy
Merge conflict git status Lists the files with conflicts. Edit them (look for <<<<<<< markers), git add, then git commit.
Want to abandon a messy merge git merge --abort Returns you to the state before you ran git merge.
"detached HEAD" warning git switch -c rescue Creates a branch from your current spot so commits don't get lost.
Pushed the wrong thing git revert HEAD Safest fix - creates a new commit that undoes the previous one. Push it.
Just want to start over from main git fetch origin && git reset --hard origin/main DangerThrows away all local changes on this branch.

Your scratch pad

Click any $ command above to copy it. Paste here to collect a sequence you want to run - or copy this whole block back into chat to ask "what would these do?"