Create a throwaway worktree with a detached HEAD

basics

// what it does

-d checks out a detached HEAD — at your current commit, or at any commit-ish you name (a tag, an old SHA, a release). Perfect for building an old version, testing, or letting an experiment run without creating a branch you'll have to clean up later.

// shell

$ git worktree add -d ../scratch
$ git worktree add -d ../myapp-v2.1 v2.1.0

// gotcha

Commits made on a detached HEAD belong to no branch: once the worktree is removed and pruned, they become eligible for garbage collection. If a throwaway experiment turns out to matter, run git switch -c keep-this inside the worktree before tearing it down.

// resources