Effective Git - Worktrees

Worktrees can save time switching between different feature branches

Published on

You know this situation: you are working on new feature branch of your product. A bunch of files has been changed, nothing has settled yet and lots of uncommitted changes. This is how work in progress looks like. Everything could be alright, but then you are asked to check out that high priority issue that a customer reported.

What I did in this situation

Introducing Worktrees

I was reading git release notes, and I stumbled upon something I haven’t heard before in the git context: worktree. It’s not a new feature. It was introduced back in 2015, but I discovered it a couple of weeks ago. It reminded me of the time when I used Perforce, and I had to create a Workspace for everything I was working on (okay, it wasn’t that bad).

The help for git help worktree gives you a very short summary of some operations available for worktrees.

1NAME
2       git-worktree - Manage multiple working trees
3
4SYNOPSIS
5       git worktree add [-f] [--detach] [--checkout] [--lock] [-b <new-branch>] <path> [<branch>]
6       git worktree list [--porcelain]
7       git worktree lock [--reason <string>] <worktree>
8       git worktree prune [-n] [-v] [--expire <expire>]
9       git worktree unlock <worktree>

Sounds interesting to me. Let’s check it out.

IMHO worktrees have a couple of advantages over another git clone:

BTW: the current project directory is a worktree too.

There might be other good solutions to support that sort of workflows. Git is versatile and powerful. It has a lot to offer, and there are so many different ways to achieve the same result. Worktrees are a great addition, and they make good sense. Your mileage may vary.

Thanks for reading!

Feel free to share, leave a comment or ignore it.

#Git   #Programming   #Tips   #Tricks   #Tools   #TIL  

comments powered by Disqus