You are right in the middle of working on a new feature, and your boss comes in demanding that you fix something immediately. You may typically want use git stash
to store your changes away temporarily. However, at this point your working tree is in a state of disarray (with new, moved, and removed files, and other bits and pieces strewn around) and you don't want to disturb your progress.
By adding a worktree, you create a temporary linked working tree to make the emergency fix, remove it when done, and then resume your earlier coding session:
$ git worktree add -b emergency-fix ../temp master
$ pushd ../temp
# ... work work work ...
$ git commit -a -m 'emergency fix for boss'
$ popd
$ rm -rf ../temp
$ git worktree prune
NOTE: In this example, the fix still is in the emergency-fix branch. At this point you probably want to git merge
or git format-patch
and afterwards remove the emergency-fix branch.