Imagine that you're working on some branch that is out of sync with master
and you have already made a lot of changes. All of the sudden you realise that you want to move some of the files to a new branch that will be sourced from master
(i.e. changes are needed by other members of the team).
Git Worktrees
To do that, you first need to create a git
worktree
:
git worktree add <folder> master
This will basically create a folder with a repository and checkout master
branch. It also creates a link between the new and the "original" repository folder.
You can use git worktree list
to list worktrees and git worktree remove
to remove them. For more information about git worktree
please refer to the official documentation.
Moving files
As mentioned earlier, git worktree
creates a link between local folders. That means that your stashes are the same between them. So you can just stash interactively necessary hunks with:
git stash -p
or you can git add
unwanted changes, and git stash -k
to stash the unstaged files.
Now you need to switch to the new worktree with master
and create a new branch:
cd <folder>
git checkout -b new_branch
and do git stash apply
. It's better to use apply
most of the time and not pop
since if you have some conflicts you can always abort without losing stashed changes.
Top comments (0)