How Git Works
Git stores your project history as a series of snapshots (commits). Each commit records:
- What files changed and how
- A reference to its parent commit(s)
- Metadata: author, timestamp, message
A ← B ← C ← D (main branch)
↑
HEAD
The Three Areas
Working Directory → Staging Area (Index) → Repository (.git)
edit git add git commit
| Area | What it holds |
|---|---|
| Working directory | Your actual files on disk |
| Staging area | Changes you've marked for the next commit |
| Repository | All committed snapshots |
Essential Commands
git init # initialise a new repo
git clone <url> # clone a remote repo
git status # see what's changed
git diff # unstaged changes
git diff --staged # staged changes
git add . # stage all changes
git add src/app.ts # stage a specific file
git commit -m "feat: add login" # commit with message
git log --oneline --graph # visualise history
git show HEAD # inspect last commitUndoing Changes
git restore file.ts # discard unstaged changes
git restore --staged file.ts # unstage (keep changes)
git revert HEAD # new commit that undoes HEAD
git reset --soft HEAD~1 # undo last commit, keep changes stagedgit reset --hard permanently discards changes. Use git revert in shared branches instead.
Configuring Git
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global core.editor "code --wait"