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 or losing context.
Pre-flight
- Confirm disk space for another copy of the repository working files (Git objects are shared, so overhead is minimal).
- Know which commit or branch represents the deployment you must patch (for example,
mainor a release tag).
Steps
-
Fetch the latest from remote and create a worktree pointing at the branch that mirrors production.
git fetch origin main git worktree add ../repo-hotfix origin/mainThis creates a new directory
../repo-hotfixchecked out toorigin/mainin a detached HEAD state. -
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 ../your-main-repo git worktree remove ../repo-hotfix 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.
Related worktree scenarios
Deep dive
For a complete reference of all git worktree commands, flags, and best practices, see Understanding Git Worktree: Commands, Flags, and Best Practices.