git-worktree: Working on multiple branches at the same time
Tags: programming, productivity, git, tooling
Published: 2022-03-13 15:50:15

---
title: git-worktree: Working on multiple branches at the same time
published: true
description: When you are working on some code and need to switch to a different branch, it seems there are only messy solutions for not losing your work. Learn more about git-worktree to checkout multiple branches at once in the same project folder.
tags: programming, productivity, git,tooling 
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/53ayhv69mcpmaifj4u5f.jpg
---
Cover photo by <a href="https://unsplash.com/@switch_dtp_fotografie?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Lucas van Oort</a> on <a href="https://unsplash.com/s/photos/branches?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
  

## My struggles
I can't count how many times this happened to me while working on a project: I am in the middle of developing a feature on a branch and I need to switch to another branch to make some modifications (maybe a change asked in a review, forgot something in my submission, etc.). I `git stash` my current work, I switch branch and I do some work. I often get carried away by having to work longer than anticipated on that other branch or I jump on a quick call with a colleague or whatever. I come back to my initial work and two situations often happen: I forgot I stashed code so I get a little scared for 10 seconds when stuff is missing/broken or there is some weird state that prevents me from switching back to my initial branch.

I always had a love/hate relationship with `git stash` for those reasons. I learned I was not the only one when discussing it with colleagues. One of them shared with me what he does: do a temporary commit, switch branch, come back to the original branch and revert back before that faux commit. Another way of doing it but I am not a fan for some reasons: you can still forget to revert back, there is no "traces" of it if you don't check the history (similar to `git stash` if you don't list them) and you can potentially shoot yourself in the foot when reverting back to a commit.

## How does it work

When using `git clone` or `git init` with [non-bare repos](https://stackoverflow.com/questions/5540883/whats-the-practical-difference-between-a-bare-and-non-bare-repository), there is a main worktree created in the folder with the `main` branch checked out. Everything that is not `.git` inside the current folder is the main working tree. I have heard developers call that the working space, the current workspace but the correct term is a working tree. 

Enters [`git-worktree`](https://git-scm.com/docs/git-worktree): this command lets you create additional working trees called `linked worktree`. You specify a folder and `git-worktree` will create a linked worktree inside of it so you can checkout any existing or new branch inside of it. All you have to do then is `cd` into that folder and you are now working on another branch, on a workingtree parallel to the main one. No more getting confused or messing up things.

## How I use it
In a project folder, I create a `worktrees` subfolder and inside of it I create a folder named after the branch I want to checkout inside that new linked worktree.

![Screenshot of my terminal after creating a git worktree and using ls command to see the content](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mzzi69mgxq7g84mg0q6t.png)

## Some use cases for the command 

- You have tests that take a while to run but you can't switch to a branch because that would change files and introduce unwanted behaviors.
- You are working on a feature and need to hop on another  branch to fix some code on a submitted merge request but there is uncommitted work.
- You want to try something quick while working on code you don't want to lose.

## Getting started
Here are some useful `git-worktree` commands from the [npm tldr](https://www.npmjs.com/package/tldr) page:


![Terminal screenshot of the result of the npm tldr command for git-worktree](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inuhkkk92qocb24grrcm.png)

## Next step
I would like to fully use `git-worktree` with Docker. I will explore possibilities on having different containers running in parallel from different worktrees without having to manually change the `docker-compose.yml` ports. Hit me up if you have a found a solution to this.

## Conclusion
`git-worktree` allows us to work on multiple branches in parallel without forcing us to commit or stash between checkouts. I am more than happy to ditch `git stash` and to never look back.

I am always eager to learn from others so let me know how you work with `git-worktree` or in which scenarios it is helpful to you. Maybe you have an even better solution?