AZ.dev

git Essentials Cheat Sheet

Git

The git commands for a normal day, from checking what changed to undoing a commit, stashing, rebasing, and finding the commit that broke it.

12 cards | git: 2.23

Looking around

See what changed, unstaged and staged

git diff
git diff --staged

The first shows edits you haven’t staged. The second shows what the next commit will contain.

Search history for a string

git log -S "useLegacyAuth" --oneline

-S finds the commits that added or removed the string. -G takes a regex and matches any changed line.

See who last touched these lines

git blame -L 10,20 src/app.ts

git log -L 10,20:src/app.ts walks the history of the same range.

Undoing

Unstage a file without losing the edits

git restore --staged src/app.ts

Without --staged it discards the working-tree changes instead, and there is no undo for that.

Fix the last commit's message

git commit --amend

Only on a commit you haven’t pushed. Amending rewrites it, and anyone who already has the old one gets a conflict.

Branches

Create a branch and switch to it

git switch -c feature/login

git switch - goes back to the branch you came from.

Rebase the branch onto the latest main

git fetch origin
git rebase origin/main

Rewrites your branch’s commits on top of main. Never on a branch someone else has checked out.

Delete a branch you've merged

git branch -d feature/login

-d refuses if the branch isn’t merged, -D deletes anyway. The remote copy goes with git push origin --delete feature/login.

Work in progress