Skip to content

Instantly share code, notes, and snippets.

@steipete
Last active May 3, 2025 12:12
Show Gist options
  • Save steipete/f87783c3ea90feba5c6920a243e2c4fe to your computer and use it in GitHub Desktop.
Save steipete/f87783c3ea90feba5c6920a243e2c4fe to your computer and use it in GitHub Desktop.
git ≥ 2.23‑style “nuke‑and‑pave”. Gets you out of everything, even worktrees!
fresh() {
# 0) escape half‑finished merges / rebases (no‑ops if nothing to abort)
git merge --abort 2>/dev/null || git rebase --abort 2>/dev/null || :
git fetch --prune --no-auto-maintenance --quiet origin && # 1 refs up‑to‑date fast  [oai_citation:0‡git-scm.com](https://git-scm.com/docs/git-fetch)
git switch --discard-changes --recurse-submodules -C main origin/main && # 2 hard‑reset + checkout incl. submodules 
git clean -ffdx && # 3 wipe every untracked/ignored artefact 
git for-each-ref --format='%(refname:short)' --merged main refs/heads \
| grep -v '^main$' | xargs -r git branch -D # 4 trash merged locals in one hit
clear
}
@steipete
Copy link
Author

steipete commented May 3, 2025

Lots of cool stuff in newer gits:

•	--no‑auto‑maintenance on git fetch sidesteps the automatic git maintenance run that fetch would normally trigger, so the network round‑trip isn’t followed by an unexpected GC stint  
•	--recurse‑submodules on git switch resets submodules to the commit recorded in origin/main—no need for a separate git submodule update run 
•	clean -ffdx (“force, force, directories, x = include .gitignored stuff”) leaves nothing behind from previous builds 
•	Deleting merged branches now uses git for‑each‑ref → single git branch -D instead of one git branch -D per branch, so it’s still just one extra Git invocation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment