Stop Stashing. Use a Second Working Directory for the Interruption.
"Can you look at the login bug on main? Five minutes." You are forty minutes into a feature, with edits in six files that do not compile yet. The usual answer is git stash , switch, fix, switch back, git stash pop — and a stash you forget about, or a pop that conflicts with something you did in between. There is a better tool, and it has been in Git since 2.5: a second working directory for the…
The article discusses the benefits of using Git worktrees to manage interruptions and improve workflow while developing. Instead of using the `git stash` command to temporarily save changes, it suggests creating a second working directory for the interruption. This involves checking out the feature branch into a new directory that shares the same .git database, allowing the original feature branch to remain untouched.
After completing the work in the new directory, the original branch can be returned to with no loss of context or conflicting changes. Git enforces that only one branch can be checked out in one worktree at a time, preventing race conditions. When the hotfix is merged, the worktree can be easily removed without affecting the main branch.
The article also highlights other use cases for worktrees, such as code reviews, long builds, and comparing versions. However, it cautions that worktrees should not be used as a substitute for branches when creating a sandbox for experiments with the .git itself.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.