Spin Up a Git Worktree for an Emergency Hotfix

Keep your feature branch intact while patching production in a separate working tree.

worktreehotfixemergencyworkflowparallel development

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, main or a release tag).

Steps

  1. 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/main

    This creates a new directory ../repo-hotfix checked out to origin/main in a detached HEAD state.

  2. Switch into the new directory and create a hotfix branch.

    cd ../repo-hotfix
    git checkout -b hotfix/rollbar-init
  3. Implement and test the fix without disturbing your original working directory.

    git commit -am "fix: ensure rollbar initializes before use"
  4. Push the branch for review and merge as usual.

    git push -u origin hotfix/rollbar-init
  5. 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 list no 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.