Skip to main content

📋 Git Quick Reference

Every command from the class on one page, plus a few "next step" commands you'll meet soon. Keep it open while you work, or use Print Page at the bottom for a paper copy.

📖 How to use this page

  • Words in <angle brackets> are placeholders. Replace the whole thing, brackets included: git add <file> becomes git add about.html.
  • Every command works the same in PowerShell, Git Bash, and the macOS Terminal. Run them from VS Code's terminal (Ctrl+`, or ⌃+` on a Mac) while your site folder is open.
  • Stuck in a screen full of text with a : or (END) at the bottom? That's the pager. Press q to get out.
On This Page

🔁 The Everyday Loop

Almost every working session looks like this. If you remember nothing else, remember the loop.

flowchart LR A["git pull
get the latest"] --> B["Edit files
in VS Code"] B --> C["git status
what changed?"] C --> D["git add .
stage it"] D --> E["git commit -m
save a snapshot"] E --> F["git push
send to GitHub"] F -.-> A
git pull
# ...edit and save your files...
git status
git add .
git commit -m "Add contact form to contact page"
git push

⚙️ One-Time Setup

Check Git is installed

  • git --versionPrints something like git version 2.51.0.

Tell Git who you are

  • git config --global user.name "Your Name"Name shown on every commit.
  • git config --global user.email "you@example.com"Use the same email as your GitHub account (or GitHub's private noreply address).

Sensible defaults

  • git config --global init.defaultBranch mainNew repositories start on main.
  • git config --global core.editor "code --wait"Git opens VS Code when it needs a message.
  • git config --global pull.rebase falseMakes git pull merge when your copy and GitHub's have both changed (otherwise Git asks you to choose).

Check your settings

  • git config --global --listShows everything you've set. Press q if it opens a pager.
  • git config user.nameShows one setting.

📸 Saving Snapshots

Start a repository

  • git initRun inside your site folder (never your home folder or Desktop). Creates the hidden .git folder.
  • git clone <url>Download a copy of a GitHub repository, history and all.

Stage and commit

  • git statusYour most-used command: what's changed, what's staged.
  • git add <file>Stage one file.
  • git add .Stage everything in this folder.
  • git commit -m "Describe the change"Save the staged snapshot with a message.

Good commit messages

  • Add photo gallery to about pageImperative mood: finish the sentence "This commit will…".
  • Fix broken link to contact pageSay what changed (and why, if it isn't obvious).
  • stuff ✗Future you will not know what "stuff" was.

Ignore files

  • .gitignoreA file listing names Git should ignore, one per line, e.g. .DS_Store and Thumbs.db.
  • .envAnything with passwords or API keys belongs in .gitignore, never in a commit.

🕰️ Looking at History

The log

  • git logFull history, newest first. q to quit.
  • git log --onelineOne line per commit: short hash + message.
  • git log --oneline --graph --allEvery branch, drawn as a graph.
  • git log --oneline -- <file>Only commits that touched this file.

What changed?

  • git diffChanges you haven't staged yet.
  • git diff --stagedChanges that are staged and ready to commit.
  • git show <hash>One commit: message plus its changes.
  • git show <hash>:about.htmlAn old version of a file, without changing anything.

↩️ Undo Ladder

Pick the lowest rung that fixes your problem. All of these are safe.

SituationCommandWhat happens
I edited a file and want the last committed version backgit restore <file>Your unsaved-to-Git edits in that file are thrown away
I staged something by mistakegit restore --staged <file>Unstaged; your edits stay in the file
Typo in my last commit message, or I forgot a file (not pushed yet)git commit --amendReplaces the last commit with a fixed one
A commit (even a pushed one) broke somethinggit revert <hash>Adds a new commit that undoes it; history stays intact

⚠️ The one you don't need yet

git reset --hard throws away commits and uncommitted work. It has its uses (see Advanced Git), but everything above covers day-to-day mistakes without that risk.

🌿 Branches & Merging

Make and move between branches

  • git branchList branches; * marks the one you're on.
  • git switch -c <name>Create a branch and switch to it.
  • git switch <name>Switch to an existing branch.
  • git switch mainBack to main.

Merge and tidy up

  • git switch main then git merge <name>Bring a branch's work into main. Always switch to the receiving branch first.
  • git branch -d <name>Delete a branch that's already merged. Git refuses if it isn't, which protects you.
gitGraph commit id: "Home page" commit id: "About page" branch new-colors checkout new-colors commit id: "Try dark theme" commit id: "Tweak links" checkout main merge new-colors commit id: "Add contact page"

⚔️ Merge Conflicts

A conflict just means both branches changed the same lines and Git wants you to choose. It's normal.

<<<<<<< HEAD
<h1>Welcome to My Site</h1>
=======
<h1>Hello, and Welcome!</h1>
>>>>>>> new-heading
  1. Open the file. Top half (HEAD) is the branch you're on; bottom half is the branch coming in.
  2. In VS Code click Accept Current Change, Accept Incoming Change, or Accept Both Changes, or edit by hand. Make sure every <<<<<<<, =======, >>>>>>> line is gone.
  3. Save, then finish the merge:
git add <file>
git commit

Escape hatch: git merge --abort puts everything back the way it was before the merge.

☁️ Working with GitHub

Connect a local repo (first time)

  • git remote add origin <https-url>Paste the HTTPS URL of your new, empty GitHub repo.
  • git remote -vCheck where origin points.
  • git push -u origin mainFirst push; -u remembers the link so plain git push works afterwards.

Every day after that

  • git pullBring down commits made elsewhere (on GitHub.com, another computer, a teammate).
  • git pushSend your new commits up.
  • git push -u origin <branch>First push of a new branch (for a pull request).

Signing in

  • Git Credential ManagerComes with Git for Windows; a browser sign-in pops up on your first push.
  • gh auth loginGitHub CLI sign-in. Choose HTTPS and let it set up Git for you.
  • Personal access tokenFallback only. GitHub stopped accepting account passwords for Git in 2021.

Push rejected?

  • ! [rejected] main -> main (fetch first)GitHub has commits you don't. Run git pull, fix any conflict, then git push again. Never "fix" this with a force push.

🔀 Pull Request Checklist (GitHub Flow)

git switch main
git pull
git switch -c update-about-page
# ...edit, then commit as usual...
git add .
git commit -m "Rewrite about page intro"
git push -u origin update-about-page
  1. On GitHub, click Compare & pull request, write a short description, and click Create pull request.
  2. If Netlify is connected, wait for the Deploy Preview link on the PR and check your change on a real URL.
  3. Click Merge pull request → Confirm merge, then Delete branch.
  4. Back home:
git switch main
git pull
git branch -d update-about-page

🖱️ VS Code Equivalents

Same Git, different buttons. Mix and match freely. See Git with VS Code for the full tour.

CommandIn VS Code
git statusSource Control view (Ctrl+Shift+G); U = untracked, M = modified, A = added, D = deleted
git add <file>+ next to the file (or next to "Changes" for all)
git restore --staged <file>− next to a staged file
git restore <file>↶ Discard Changes icon next to the file
git commit -m "…"Type in the message box, click ✓ Commit (or Ctrl+Enter)
git push / git pullSync Changes button, or the ⟳ in the status bar
git switch / git switch -cClick the branch name at the bottom-left of the status bar
git log -- <file>Timeline panel at the bottom of the Explorer

📜 Older Spellings You'll See Online

Git added switch and restore in 2019 to split up the overloaded checkout command. Older tutorials and Stack Overflow answers still use the originals. They work, and they do the same thing:

You'll seeWe use
git checkout <branch>git switch <branch>
git checkout -b <branch>git switch -c <branch>
git checkout -- <file>git restore <file>
git reset HEAD <file>git restore --staged <file>
mastermain (GitHub's default since 2020)

🚀 Next-Step Commands

Not part of the one-day class, but you'll bump into these soon. Each links to a fuller explanation on the Advanced Git page.

Park unfinished work

  • git stash push -m "half-done header"Put uncommitted changes aside.
  • git stash listSee what's parked.
  • git stash popBring the latest one back.

Mark versions

  • git tag -a v1.0 -m "First public version"Label the current commit.
  • git push origin v1.0Tags aren't pushed automatically.

Investigate

  • git blame <file>Which commit last changed each line.
  • git log -S "some text" --onelineCommits that added or removed that text.
  • git reflogEverywhere HEAD has been, the safety net for "lost" commits.

Rewrites history (learn first)

  • git rebase -i HEAD~3Tidy your last few unpushed commits.
  • git reset --soft HEAD~1Undo the last commit, keep its changes staged.
  • git push --force-with-leaseOverwrite a remote branch. Only on your own branch, only when you mean it.

🆘 "Help, I'm Stuck!"

You see…Do this
A wall of text ending in : or (END)Press q
A strange editor with ~ down the left side (Vim)Press Esc, type :q!, press Enter. Then set core.editor to "code --wait" (see Setup).
fatal: not a git repositoryYou're in the wrong folder. In VS Code use File → Open Folder and pick your site folder.
A merge you want to back out ofgit merge --abort
Need to specify how to reconcile divergent branchesgit pull --no-rebase this time, and set pull.rebase false (see Setup) for next time
You are in 'detached HEAD' stateJust looking? git switch main. Made commits you want to keep? git switch -c <new-branch> first.
Anything elseRun git status and read it slowly. Git usually tells you the next command to run.

🌱 Remember

Once something is committed, it's very hard to truly lose it. Commit small and often, and a mistake is never more than one small undo away.