Spin Up a Git Worktree for an Emergency Hotfix
Keep your feature branch intact while patching production in a separate working tree.
When to use
You are midway through a large feature, but a production regression needs a quick patch. Worktrees provide a clean checkout without juggling stashes.
Pre-flight
- Confirm disk space for another copy of the repository.
- Know which commit or branch represents the deployment you must patch (for example,
mainor a release tag).
Steps
-
Create a worktree pointing at the branch that mirrors production.
git fetch origin main git worktree add ../repo-hotfix origin/main -
Switch into the new directory and create a hotfix branch.
cd ../repo-hotfix git checkout -b hotfix/rollbar-init -
Implement and test the fix without disturbing your original working directory.
git commit -am "fix: ensure rollbar initializes before use" -
Push the branch for review and merge as usual.
git push -u origin hotfix/rollbar-init -
Once merged and deployed, clean up the worktree and branch.
cd ../command-library-astro-app git worktree prune git branch -d hotfix/rollbar-init git push origin --delete hotfix/rollbar-init
Verification
git worktree listno longer shows the hotfix directory after cleanup.- Monitoring or release dashboards confirm the hotfix is live.
Follow-up
- Document the incident or update the runbook so the next responder knows the context.
- Consider adding automated tests to prevent the regression from resurfacing.