Learning About Git Worktrees

2022-05-26
Git
version control
development tools

Git Worktrees

A colleague of mine shared his article about Git worktrees on the work chat, and I thought it's an interesting tool worth reflecting in my blog for own educational reasons.

Essentially it's a way to work on several branches concurrently without a need to clone the whole repository, given that each branch is i a different (temporary) directory. For instance, if I'm currently working on a branch named new-product-page and I need to change something in a blog branch, I can do

git worktree add ../blog-update blog

to create a blog-update that would point to a blog branch (I could also use -b new-blog-branch to create a new branch).

Then here's how it'd look like in the listings:

># git worktree list
/Users/halien/devel/my/hydralien.net  1ec7851 [new-product-page]
/Users/halien/devel/my/blog-update    483c934 [blog]

># git branch
+ blog
  master
* new-product-page

And when the changes in the worktree are done, and it's no longer required, removing it is as easy as

git worktree remove blog-update

Here's also an official documentation page for it: https://git-scm.com/docs/git-worktree