Originally published at as your pager (brew install git-delta, then pager = delta in ~/.gitconfig), the output stops being painful to read.
gcam (git commit -a -m)
gcam "fix: trailing slash in webhook URL"
Quick one-line commits for small fixes. For anything bigger I drop the -m and let $EDITOR open so I can write a proper message with a body.
gpsup (git push --set-upstream origin <current-branch>)
gpsup
First push of a new branch. The full command is annoying to type, so gpsup figures out the current branch name itself. After the first push, plain gp (just git push) works because upstream is set.
gco / gcb (git checkout / git checkout -b)
gco main # switch to main
gco - # switch to previous branch
gcb feature/login # create + switch to new branch
gco - is the one to notice. Like cd - for branches. When you're bouncing between two branches all day, it's a single keystroke each way instead of typing the name.
gst again, then gpf (git push --force-with-lease)
gpf
After rebasing or amending. Always use --force-with-lease, never --force. The lease version refuses to push if someone else has pushed to your branch since your last fetch, saving you from silently overwriting a teammate's work. There is no good reason to ever type --force in 2026.
gfa (git fetch --all --prune)
gfa
Refresh every remote, prune deleted remote branches. Run before you start anything that depends on knowing the current state of the world. The --prune half is what makes the next section work.
the weekly five
The commands that aren't in your fingers yet but should be.
git switch and git restore (the new commands)
git switch main
git switch -c new-feature # create + switch
git restore --staged file.txt # unstage
git restore --source=abc123 file.go # restore single file from any commit
switch and restore split the four jobs checkout used to do. Safer because they can't accidentally do the wrong one. The one I reach for most is restore --source=<sha> <path>. Translation: "grab this single file from three commits ago without touching anything else."
interactive rebase with autosquash
git commit --fixup=abc123 # fixup commit targeting abc123
git commit --fixup=abc123 # another one, still targeting
# ... keep working ...
git rebase -i --autosquash main # all fixups slot into place automatically
This is the single biggest workflow win I've found in ten years of git. While reviewing your own PR you find a bug four commits back. Don't fix it on top. git commit --fixup=<sha> creates a commit targeting the offender. Keep working. When you're done: git rebase -i --autosquash main reorders and squashes everything for you. PR history stays clean. No // fix bug in earlier commit commits.
Install git-absorb (brew install git-absorb) and it picks the target sha for you by looking at which lines you changed. The flow becomes:
# edit files to fix the bugs
git absorb --and-rebase
# done.
The first time it works on a six-commit branch you'll wonder why it isn't built into git.
git reflog, the universal undo
git reflog
git reset --hard HEAD@{5}
Every change to HEAD is logged for 90 days. Bad rebase? reflog. Deleted branch? reflog. reset --hard to the wrong commit? reflog. There is almost nothing in git you can't undo if you know about it. I've never met anyone who used it as much as they should.
git worktree
git worktree add ../proj-hotfix hotfix/prod-down
git worktree list
git worktree remove ../proj-hotfix
Need to fix a prod bug while halfway through a feature? Don't stash. worktree add gives you a second checkout in a sibling directory, sharing the same .git. Same repo, two working trees, both editable, no stash gymnastics. I use it constantly for "let me review your PR" without leaving my own branch.
branches sorted by recency
git config --global alias.recent \
"for-each-ref --sort=-committerdate refs/heads/ \
--format='%(HEAD) %(color:yellow)%(refname:short)%(color:reset) \
%(color:green)(%(committerdate:relative))%(color:reset) %(contents:subject)'"
git recent | head -10
git branch lists alphabetically, which is useless. git recent lists by last-commit-date, which is exactly what you want when you're trying to remember the name of "that branch from Tuesday."
the cleanup ritual
Run this weekly. If you've ever scrolled through 80 stale branches looking for the one you actually want, you already know why.
the easy half: real merges
gfa
git branch --merged main | grep -v '\*\|main\|master' | xargs -n1 git branch -d
Deletes every local branch whose tip commit is already in main. Works only if your team uses merge commits. Most don't.
the hard half: squash-merges
GitHub's "Squash and merge" creates a brand-new commit on main with a different SHA. git branch --merged won't catch your local branch because its commits literally aren't in main's history.
The workaround: after gfa, any branch whose tracked remote was deleted shows as [gone]. Those are your merged-and-deleted PRs.
# git-gone: delete local branches whose remote tracking branch is gone
git-gone() {
git fetch --prune
local gone
gone=$(git for-each-ref --format '%(refname:short) %(upstream:track)' refs/heads \
| awk '$2 == "[gone]" {print $1}')
if [ -z "$gone" ]; then
echo "No gone branches"
return
fi
echo "$gone"
echo -n "Delete these? [y/N] "
read -r confirm
[[ "$confirm" == "y" ]] && echo "$gone" | xargs -r git branch -D
}
Or install (brew install git-absorb). Auto-fixup commits without picking SHAs.
(
brew install lazygit). TUI for the operations that are tedious on CLI: partial commits, interactive add, stash management, conflict resolution.I don't reach for lazygit daily, but the day I do, usually a five-way merge conflict, it pays for itself immediately.
bonus: two AI shell helpers for the stuff git can't tell you
Git can tell you what changed. It can't tell you the syntax for the find command you needed two minutes ago. Two short zsh functions wrap an AI CLI so the answer lands in the terminal instead of in a chat tab. p prints to stdout (for reading), d pre-types a command into your next prompt (for running).
# p: one-shot AI answer printed to terminal (math, facts, regex, syntax)
p() {
emulate -L zsh
setopt NO_GLOB
if [ $# -eq 0 ]; then
echo "usage: p <question or task>" >&2
return 1
fi
pi -p --no-session --append-system-prompt 'Answer in ONE line. No preamble, no explanation, no markdown, no code fences. For shell/kubectl/git/etc requests output only the command. For factual or math questions output only the answer.' "$*" \
| tr -d '\000-\037' \
| sed 's/^[[:space:]]*//;s/[[:space:]]*$//'
}
alias p='noglob p'
# d: AI suggests a shell command, pre-typed into next prompt (review + Enter)
d() {
emulate -L zsh
setopt NO_GLOB
local query="$*"
local prompt="You are a command line expert. The user wants to run a command but they don't know how. Here is what they asked: ${query}. Return ONLY the exact shell command needed. No explanation, no markdown, no code blocks. Just the raw command."
local cmd
cmd=$(droid exec -m glm-4.6 -r off --output-format text --disabled-tools execute-cli -- "$prompt" \
| tr -d '\000-\037' \
| sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
print -z -- "$cmd"
}
alias d='noglob d'
Swap pi and droid for whatever AI CLI you have: claude -p, llm, gh copilot suggest, ollama run. The pattern is what matters, not the backend.
why split into two functions
Different jobs:
p (read) | d (run) | |
|---|---|---|
| Output goes to | stdout | next prompt buffer |
| You then... | read it | edit / press Enter |
| Best for | "what's the regex for X" | "find files larger than 100MB" |
the trick: print -z is what makes d safe
print -z pushes text onto the zsh line editor, i.e. into your next prompt, pre-typed and ready. Compared to the alternatives:
| Strategy | Speed | Safety | Friction |
|---|---|---|---|
eval "$(...)" | fastest | bad, auto-runs model output | none |
Pipe to pbcopy | medium | safe | switch focus, paste |
| Print to stdout | medium | safe | select + copy + paste |
print -z | fastest | safe, you press Enter | none |
Same trick Ctrl-R history search uses. Native zsh. You always see and approve the command before it runs.
what it feels like
$ p git rebase abort
git rebase --abort
$ p whats the syntax for git log since a date
git log --since="2 weeks ago"
$ d find all .log files modified in the last hour
# next prompt now shows, cursor at the end:
$ find . -type f -name "*.log" -mmin -60█
$ d remove all local branches whose remote is gone
# next prompt:
$ git fetch --prune && git for-each-ref --format '%(refname:short) %(upstream:track)' refs/heads | awk '$2 == "[gone]" {print $1}' | xargs git branch -D█
A two-letter command, and the answer is already on the line where you wanted it.
the three defensive details
emulate -L zsh; setopt NO_GLOB # function-local zsh defaults, no globbing
alias p='noglob p' # `p list *.log files` won't glob-expand `*.log`
tr -d '\000-\037' | sed 's/[trim]//' # strip control chars (incl. ANSI), trim whitespace
noglob is the one most people miss. Without it, d list all *.log files would have zsh expand *.log against the current directory before the function ever sees it. With noglob, the glob characters pass through literally.
A single-function variant of this, with a heuristic that picks stdout vs pre-typed automatically, lives in this TIL.
ten years in, the surprise
After a decade, the command I run most isn't commit. It isn't push. It's gst, hundreds of times a day, between every other operation. The most-used git command in my shell is the one that does nothing.
SOCIAL SHARE CARD GENERATOR