Git
Git commands I use every day
A short reference of the Git commands I actually reach for daily. Not exhaustive, just the ones that earn their keep.
Checking state
git status # what changed, what is staged
git status -sb # compact one-line-per-file view
git diff # unstaged changes
git diff --staged # what is about to be committed
git log --oneline -10 # last 10 commits, terse
Staging and committing
git add -p # stage hunks interactively
git commit -m 'msg' # commit staged changes
git commit --amend # fix the previous commit (before pushing)
Use git add -p more than you think. Reviewing each hunk before it lands keeps commits small and honest.
Branching
git switch -c feature/login # create and switch to a new branch
git switch main # move back to main
git branch -d feature/login # delete a merged branch
git switch is the modern, clearer alternative to git checkout for branches.
Undo without panic
| Goal | Command |
|---|---|
| Unstage a file | git restore --staged file |
| Discard local edits | git restore file |
| Undo last commit, keep changes | git reset --soft HEAD~1 |
| Recover a lost commit | git reflog then git checkout <hash> |
git reflog has saved me more times than I can count. Almost nothing in Git is truly gone for a couple of weeks.
Syncing
git fetch # download refs, do not merge
git pull --rebase # replay your commits on top of upstream
git push -u origin HEAD
I prefer pull --rebase so history stays linear instead of sprouting merge commits for every sync.
