Manage Branches easily using Git Worktree
102 segments
so say you're working on a new feature
and as part of this feature you've made
some big changes to the code base you've
added some files you've deleted some
maybe added a couple of new dependencies
and at this point say a teammate wants
you to check out another branch on your
system or maybe you've noticed a small
regression while building your feature
and you want to see if your feature
introduced the regression or if it
already existed on the base branch and
since get allows you to only have one
active branch per directory you would
have to save your work by stashing it
and then you would jump to the branch
that maybe your teammates asking you to
check out and then you would jump back
to your branch and then you would do a
get stash pop so that your changes that
you were in the middle of making are not
lost
but take this as an example I've made a
change to the dependency list in this
branch so if I switch to the branch that
my team in wants me to check out I would
have to reinstall the dependencies after
checking out his branch do the testings
and then when I moved back to my feature
branch I would have to reinstall all
these dependencies again since the
dependency has changed here so one way
to solve this would be to have both the
branches available on your system so
that you don't have to keep installing
the dependencies when you switch these
branches and one way to do this would be
to reach lone your project in a
different folder with that branch
checked out so that you can just switch
between folders where different branches
are checked out maybe you one is your
teammates branch and the other one is
your feature branch and we could do this
manually but get provides a nifty little
command called get work tree there are
allows you to do this automatically so
how does get work tree work so let's say
I
and in the middle of making my changes
and someone's asking me to check out the
master branch for this for this
repository I in serve stashing now I can
use the gate
worked recommend to add a new directory
on my system let's call that master and
get here would re clone my repository in
this master folder and we also need to
specify the branch that you want to
check out automatically in the folder as
well so in this case gate would check
out the repository in the master folder
and would check out the master branch so
on running this we can see that the work
tree was created so now if I hop out of
my current project folder I can see that
I have my current project folder but I
also have the new project folder that we
just created using get work tree call
master so I can move into master and
then I can see that my project was
reclone and the master branch was
checked out automatically so now I can
do my testing maybe check out someone
something that my team mate is asking me
to check out and then I can go back to
my initial project folder and I have all
my changes untouched and I didn't have
to make any big changes in terms of
cleanup or reinstalling dependencies and
the reason why I calls a cleaner
approach is that the work tree command
lets you see all the actively present
work trees on your system for instance
it's showing me that my new features
checked out in this project folder and
my master the branch is checked out in
this project folder and maybe after some
time I figure that this is like
redundant now I've done my testing and I
can't remove it I can use the gate work
tree command again to remove a work tree
so in this case which is master and with
that the cleanup is done the folder that
was created by gate work tree has been
cleaned from the system and I don't have
to manage
on my own anymore so this is how you can
use get work tree to jump between
branches when you have a lot of unsaved
changes and jumping between branches is
going to be a hassle
Ask follow-up questions or revisit key timestamps.
This video explains the limitations of using standard Git branching when dealing with unsaved changes or heavy dependencies, and introduces 'git worktree' as a more efficient solution. By allowing multiple branches to be checked out simultaneously in separate directories, 'git worktree' avoids the need for stashing work or repeatedly reinstalling dependencies, offering a cleaner workflow for managing multiple tasks.
Videos recently processed by our community