Git: The Commands That Matter, and the One to Never Run
A working reference, not a tutorial. The ordinary loop plus the handful of commands that get you out of trouble.
The loop
git status
git add path/to/file another/file
git commit -m "what changed and why"
git push
Never git add .
Stage explicit paths. Every time.
git add . stages whatever happens to be in the tree — a .env, a vault of personal notes, a 400MB video, an API key in a scratch file. A secret committed once is in the history forever, and rotating it is the only real fix. The two seconds saved are not worth owning that afterwards.
git add index.html pages/Store.html # yes
git add . # no
Find out what a change actually touched
git log --oneline -- path/to/file # this file's history
git show --stat <commit> # what a commit touched, at a glance
git log --diff-filter=D -- path # the commit that DELETED something
git log --all --diff-filter=ADR -- path # added, deleted or renamed, any branch
That third one is how you answer "where did this file go", and the fourth answers the more useful question: was it ever here at all? A reference to a file that was never tracked is a different problem from one that was deleted, and the fix is different too.
Get something back
git checkout <commit> -- path/to/file # restore one file from a commit
git show <commit>:path/to/file # read it without restoring it
git restore path/to/file # discard uncommitted changes to it
Before you push
git diff --staged # read what you are about to commit
git log --oneline origin/main..HEAD # what is going out
Reading your own staged diff catches more than any hook. It is also the last point at which a stray file is free to remove.
Commit messages
The message is the only place the reason survives. The diff already says what changed; nobody can reconstruct why from it a year later. If a decision was made — an option rejected, a measurement taken, a trade accepted — the message is where it belongs, because the next person will git log the file long before they find any document.
The rule worth keeping
Stage deliberately, and write the message for the person who has to change this in a year, who will be you.