Git worktree

| git

Git worktree is not just a command, it’s the frist step to a new way to work. It enables you to be in different branches at the same time. Lets explain what I mean.

What it is

git worktree allows you to have multiple working trees for a single Git repository. This means you can have multiple instances of your repository on your file system, each with its own working directory, but all sharing the same Git history. This makes it easier to work with multiple branches, as you can have a separate working directory for each branch.

Why I use it

Git worktrees are extremely useful when you work with different branches at the same time. The main strength is the possibility to jump from one branch to another, without the need to stash or commit the pending changes. This avoids the creation of temporary1 commits, that in most cases will remain in the commit history.

How many times have you had to resolve an urgent bug in production while you were developing something new? It can happen, in this case you stash or commit all your unstaged code, switch to your main branch, fix the bug, commit it and then return to the previous branch and retrieve all your stashed code. This procedure can be tedious and time-consuming operation.

With worktree you can handle this case more easily. Each time you have to abandon what you are doing and make some implementation to another branch, all you have to do is change the directory. Make all the changes you have to do and then return to the previous working directory. And you will find all your unstaged changes still there.

How I use it

Create work tree

To create a worktree run:

git worktree add <pat_to_directory> <branch_name>

Where <path_to_directory> will be the new location of the work tree and <branch_name> is the initial branch of the worktree.

I found it useful to put all my working directories in a directory named forest. Usually I use a custom script that creates the work tree in the forest directory and copies, from the main working directory, all the env/secrets files needed for the project, to the newly created working directory.

Hop between working directories

To hop from one working directory to another, quicker than cd /path/to/working_directory/ into it, I wrote this useful alias

cd $FDIR/$(ls $FDIR | fzf --height "25%" --header "Choose the workingtree" --reverse --border)

Where the $FDIR contains the path to the forest directory. With the use of fzf we can list all the worktrees, and with a few characters, you can identify the directory and change location. worktree_comand

Remove a work tree

Usually, before doing the removal I list all working trees/directories, git worktree list, and then I proceed with the removal, git worktree remove. This operation simply deletes the worktree directory.

Note: you could delete the worktree directory with the rm command, but by doing this you could leave something pending. In this case, you can remove all pending things by executing git worktree prune.

  1. “There is nothing more permanent than a temporary solution” ↩︎