Snippets
Git logo

Git

Git commands and recovery patterns I reach for in real projects. Daily-driver shortcuts and rescue moves for when things go sideways.

1

About these Git snippets

This is the Git reference I wish I had when I started working on teams. Not a beginner tutorial, not the entire man page. Just the commands and recovery patterns I reach for in real projects, from daily shortcuts to rescue moves for when a rebase goes sideways or someone force-pushes to the wrong branch.

Every snippet here has been tested on Git 2.30+ and works the same on macOS, Linux, and WSL. Where a command behaves differently across platforms, the snippet calls it out.

What's inside

  • Undo and recovery: undo the last commit while keeping your changes staged, recover a deleted branch from reflog, and clean untracked files without nuking anything you need.
  • History rewriting: squash the last N commits with interactive rebase, and set up a pretty one-line log alias you will use every day.
  • Advanced workflows: work on multiple branches simultaneously with git worktree (no more stashing and switching), and use git bisect to binary-search for the exact commit that introduced a bug.
  • Stash patterns: go beyond stash and pop with named stashes, partial stashing, and stash-based workflows for code review context-switching.

More patterns land here as I run into real situations at work. The goal is always the same: solve a specific Git problem in the fewest safe steps possible.

How to use them

Every snippet page shows the exact command sequence, explains what each flag does, and warns you when a command rewrites history or is destructive. Copy and run. For alias-style snippets, the page shows how to add them to your global .gitconfig so they are available in every repo.

One principle across all of these: I never include a command that silently destroys work. If something is destructive (like git clean -fd), the snippet always starts with the dry-run version first.

If you have a better pattern or a safer alternative, the code is open on GitHub. PRs and issues are welcome.