#100DaysOfCode - Day 4: Git Worktree!

#100DaysOfCode - Day 4: Git Worktree!

1 min

It’s day 4 of 100 days of code, and today I learned about a great git feature at work: git worktree!

It’s fairly simple: you run git worktree add ../new_branch_directory to create a new directory from your current project with a copy of your current branch (make sure to specify a directory outside of the current project with ..). Then, Git will make that directory for you with your branch, and all you need to do is access that directory to play with the branch. This is a great utility for when you need to open the solution in an IDE which takes a while to change between switched branches, or even if you just like to avoid the git commandline (I get it, having the mental separation of separate windows for old VS. new is helpful)

In my case, I was working on a fairly complex change, in that it affected many different files, and my committed work had gotten pretty messy in its differences from dev. In hoping to clean it up to make referencing back to the files changes a bit easier, I decided to backtrack and redo my commits. My (terrible) workaround or solution was to re-clone the repository, and check-out the branch there, but git mergetree would’ve been a much more elegant solution. I realized how silly my solution was, and realized developers have to have been in a similar boat, so that’s what led to this discovery!

I wish I would’ve found it ealier, as it would’ve allowed me to merge in the early changes of my messy branch, without needing to push to the remote and pull down on my second cloned repository! Hope someone else doesn’t do the same ugly workaround I did :)

~ Moxnr

Written on October 26, 2020