Git Basics (First Hour)

Git tracks snapshots of your project. Each commit is a full snapshot, linked to its parent. Branches are just named pointers to commits.

1 credit

Mental model

  • **Working directory** — your actual files on disk.
  • **Staging area (index)** — what's queued to go into the next commit.
  • **Repository (.git)** — the full history, every commit, every branch, objects.
  • `git add` moves working → staging. `git commit` moves staging → repo. `git checkout/switch` moves repo → working.

First workflow

bash
git init                              # inside a project folder
git add README.md                     # stage a file
git commit -m "initial"               # record
git remote add origin git@...         # attach a remote
git push -u origin main               # publish + set upstream

Daily loop

  • `git status` — where am I? always safe to run.
  • `git switch -c feature/x` — start a branch for the change.
  • Edit → `git add <files>` → `git commit -m "msg"` → repeat.
  • `git push` when you want to share.
  • Open a PR → review → merge → pull main locally → delete the feature branch.

Undo cookbook

5 items
Unstage a file
git restore --staged file
Discard local changes
git restore file (destructive — uncommitted changes lost)
Edit last commit message
git commit --amend
Undo last commit, keep changes
git reset --soft HEAD~1
Recover 'lost' commit
git reflog (find SHA, then git checkout or git reset --hard <sha>)

Further reading