I have uncommitted and staged changes in my main branch. I want to put those existing changes in a different worktree, so that my main branch/folder stays clean. How do I do that?
I’ve tried variations of git worktree add -b new-branch ../feature
but it doesn’t move my changes to the new worktree, just keeps them in the original main folder.
Edit for clarification: I’m in the middle of a large merge, and there are a dozen unresolved file conflicts. I want a way to pause my progress on the merge and get back to it when I can, but I can’t stash unmerged files. In the past I’ve just added them all and committed them to a different branch, then fixed the conflicts in later commits, but then I’m not able to use merge tools to help me resolve the conflicts.
9
I have uncommitted and staged changes in my main branch. I want to put those existing changes in a different worktree, so that my main branch/folder stays clean.
I’ve tried variations of git worktree add -b new-branch ../feature but it doesn’t move my changes to the new worktree, just keeps them in the original main folder.
The actual files are the “worktree”. git worktree add
will share your stash with other worktrees, that lives in the shared .git
directory, but it will not share uncommitted changes because those are the worktrees. You get freshly checked out files.
If you wish to move uncommitted changes to a new worktree, stash them, then apply them in to your new worktree.
But you don’t need a new worktree. The simplest thing to do is to put it in a new branch with the same worktree.
git checkout -b new_branch
Now you can add your uncommitted changes, apply your stashed changes, and commit them. When you’re done you can switch back to main. It’s ok to commit them as a Work In Progress (WIP) and revise the commit later with git commit --amend
.
If you only want to commit some of the changes to the new branch, use git add -p
to make partial commits.
2