Git basics

The three areas every file lives in, the daily commit loop, and writing history worth reading later.

Three areas, one mental model

Git moves content through the working tree (your files), the index (staged snapshot), and the repository (committed history). Almost every confusing command becomes obvious once you know which two areas it moves files between.

git init                 # create a repository here
git status               # always the first question
git add index.html       # working tree -> index
git commit -m 'Add landing page'
git log --oneline --graph --decorate
CommandMoves
git addworking tree β†’ index
git commitindex β†’ repository
git restore --staged findex β†’ working tree (unstage)
git checkout HEAD -- frepository β†’ index + working tree

Commits that read well

  • One logical change per commit β€” not 'stuff from today'.
  • Write the subject in the imperative: 'Add retry logic', not 'Added' or 'Adds'.
  • Explain why in the body; the diff already shows what changed.
  • Keep the subject under about 50 characters so logs stay scannable.
git commit -m 'Cap retries at three attempts

Upstream timeouts occasionally last 30s; unbounded
retries kept workers busy during incidents.'

Ignoring files

# .gitignore
node_modules/
.env
*.log
.DS_Store

# stop tracking a file that is already committed
git rm --cached config.local.json
⚠️
Adding a path to .gitignore does not remove it from history β€” and a committed secret must be treated as leaked regardless. Rotate it, don't just ignore it.

Reading differences

git diff                 # unstaged changes
git diff --staged        # what the next commit contains
git show <sha>           # one commit
git diff HEAD~1 HEAD     # compare with previous commit
git diff --stat          # summary instead of full text

FAQ

Amend or new commit?
Amend only for genuinely local, unpushed commits. Once pushed, rewriting published history forces everyone else to reconcile β€” add a new commit instead.
How do I configure my identity?
git config --global user.name 'Name' and user.email. Set it once per machine.

Branching and merging Remotes and collaboration

Last refreshed 2026-09-17.