You have uncommitted changes and want to switch to a different branch to test something without losing your current work. Which situation best fits using git worktree instead of git stash?
Think about whether you want to work on two branches at the same time with your changes visible.
git worktree allows you to have multiple working directories checked out at once, so you can work on different branches simultaneously without stashing or losing changes. git stash temporarily saves changes but requires you to switch branches and then reapply the stash.
You run the following commands in a git repository:
echo 'change' >> file.txt git stash
What will git stash list show immediately after?
git stash listThink about what stash numbering starts at and what happens after the first stash.
The first stash is saved as stash@{0}. The list shows the most recent stash first with a message including the branch and commit info.
You want to create a new worktree for branch feature in a directory ../feature-worktree. Which sequence of commands correctly achieves this?
Remember that git worktree add creates and checks out the branch automatically.
git worktree add creates the new directory and checks out the branch there. You then change directory to work in it. Listing worktrees shows all active worktrees.
You try to switch branches with git checkout feature but get this error:
error: Your local changes to the following files would be overwritten by checkout:
What is the best way to handle this if you want to keep your changes but also switch branches?
Consider how to keep your current changes visible and work on another branch simultaneously.
git worktree add allows you to have multiple branches checked out at once without needing to stash or commit your current changes. This avoids the error and keeps your work intact.
You are developing two features in parallel. You want to switch between them frequently without losing work or creating many commits. Which approach is best?
Think about how to keep work visible and avoid repeated stashing or committing.
git worktree allows you to have multiple branches checked out in separate directories, so you can work on both features simultaneously without stashing or committing incomplete work.