You are mid-feature and an urgent bug needs a fix on another branch. You don't want to stash or commit half-done work. What do you do?
IntermediateScenarioBranching and Merging Workflows
Answer
Situation read → Switching branches would disturb your working tree.
What I'd do →
- Create a second working directory on the same repo:
git worktree add ../hotfix-wt -b hotfix/login origin/main
cd ../hotfix-wt
- Fix, test, push from there.
- Clean up:
git worktree remove ../hotfix-wt.
Why → Worktrees share one object database, so there is no second clone and no stash juggling. Each branch can only be checked out in one worktree at a time.
Interview tip → Worktrees are also great for running a long test suite on one branch while coding on another.
More on Branching and Merging Workflows
You run `git pull` and Git stops with a merge conflict in `app.py`. What do you do?BeginnerYour feature branch is three weeks old and `main` has moved a lot. Merge `main` in, or rebase onto it?IntermediateThe team argues about fast-forward merges versus merge commits on `main`. How do you settle it?BeginnerA teammate branched from the wrong base, and their PR shows 40 unrelated commits. How do you fix it?IntermediateBefore merging, you want to see exactly what the feature branch adds compared to `main`. Which commands?BeginnerHalfway through a merge you realise you pulled the wrong branch. How do you back out?Beginner