# Git Worktrees: The Best Git Feature You’ve Never Heard Of

URL:: https://levelup.gitconnected.com/git-worktrees-the-best-git-feature-youve-never-heard-of-9cd21df67baf
Author:: James Pulec
## Highlights
> When I first started using git professionally, one habit I had was to frequently stash code. I’d be working on a particular feature and I’d see a Hipchat alert (yes, this was before Slack) about an error being thrown in production. I’d take a look at the stacktrace in Sentry, and find the offending section of code. From there I’d return to my terminal, stash my changes, open a new bugfix branch off master, and begin trying to reproduce the error locally. ([View Highlight](https://read.readwise.io/read/01fddppkbwwmsxvn9wp5tpsvas))
> Git Worktrees are a feature that allow you to have a single repository with multiple checked out working branches at the same time. Now that may not sound that cool, but let me lay out an example. ([View Highlight](https://read.readwise.io/read/01fddpq2zefn62403vbtjb5xrj))
> I have a copy of the `ApertureScience` repository located at `/home/James/Aperture`. I’m going to begin working on a new feature, `cake`. Before I start this work, I’m first going to run `git worktree add cake`. This will create a new directory for me to do my work located at `/home/James/Aperture/cake`. I spend a few hours working on my `cake` feature.
> A wild error appears in our production app. Probably a rogue test subject causing trouble… Better go deal with that.
> I stop working on the `cake` feature and run the command `git worktree add bugfix` inside the `/home/James/Aperture` directory. This almost instantly creates a new working copy of the repository at `/home/James/Aperture/bugfix` on a new branch called `bugfix`. I `cd` into that directory and run whatever commands I need to boot up the application. Once I discover the bug and figure out what changes need to be made to fix, I commit the changes, push up my branch for review, and then return to the feature work I was doing on `/home/James/Aperture/cake`. No stashing hassle, and no worrying that my most recent pre-commit hooks aren’t running. ([View Highlight](https://read.readwise.io/read/01fddps94dww8d9d9hzs2tfg2n))
> Effectively, you can work on as many things in parallel as you’d like and can feel free to just pause your work and come back later. ([View Highlight](https://read.readwise.io/read/01fddpscn98r3nt7g0wha1nct8))
> It’s possible that being able to work on more things at once may also be detrimental to your productivity. You might wind up splitting your focus and try to do too many things in parallel. ([View Highlight](https://read.readwise.io/read/01fddpt1qep84x46f0m8byvdwc))
> `/home/James/worktrees/.bare` — This is the actual git repository, a bare instance. This stores only the repo metadata and has no actual working tree. I keep this in a separate hidden directory so that I can create new worktrees from this folder, but don’t accidentally try to check out a branch in the `worktrees` directory, which is an operation that can only be done in a working tree.
> `/home/James/worktree/.git` — This file just points to the `.bare` directory.
> `/home/James/worktree/master` — I always keep a copy of master, mostly so that if something comes up I can spin up a local copy of whatever is running in production.
> `/home/James/worktree/hotfix` — This branch is kept as a place where I perform hotfixes. It happens with enough frequency that I keep a dedicated worktree around for it.
> `/home/James/worktree/<feature>` — I have a number of different feature worktrees, which are divided by particular feature areas of the codebase. ([View Highlight](https://read.readwise.io/read/01fddpw9w2280p5xbfjhnrzftc))
> All in all, this style of working has made it so much easier for me to context switch when it is necessary, and provides me with small chunks of work that I can get done in less than 1 hour increments. ([View Highlight](https://read.readwise.io/read/01fddpws96gwzjffqkfhymry8y))
---
Title: Git Worktrees: The Best Git Feature You’ve Never Heard Of
Author: James Pulec
Tags: readwise, articles
date: 2024-01-30
---
# Git Worktrees: The Best Git Feature You’ve Never Heard Of

URL:: https://levelup.gitconnected.com/git-worktrees-the-best-git-feature-youve-never-heard-of-9cd21df67baf
Author:: James Pulec
## AI-Generated Summary
Supercharge your Development Workflow
## Highlights
> When I first started using git professionally, one habit I had was to frequently stash code. I’d be working on a particular feature and I’d see a Hipchat alert (yes, this was before Slack) about an error being thrown in production. I’d take a look at the stacktrace in Sentry, and find the offending section of code. From there I’d return to my terminal, stash my changes, open a new bugfix branch off master, and begin trying to reproduce the error locally. ([View Highlight](https://read.readwise.io/read/01fddppkbwwmsxvn9wp5tpsvas))
> Git Worktrees are a feature that allow you to have a single repository with multiple checked out working branches at the same time. Now that may not sound that cool, but let me lay out an example. ([View Highlight](https://read.readwise.io/read/01fddpq2zefn62403vbtjb5xrj))
> I have a copy of the `ApertureScience` repository located at `/home/James/Aperture`. I’m going to begin working on a new feature, `cake`. Before I start this work, I’m first going to run `git worktree add cake`. This will create a new directory for me to do my work located at `/home/James/Aperture/cake`. I spend a few hours working on my `cake` feature.
> A wild error appears in our production app. Probably a rogue test subject causing trouble… Better go deal with that.
> I stop working on the `cake` feature and run the command `git worktree add bugfix` inside the `/home/James/Aperture` directory. This almost instantly creates a new working copy of the repository at `/home/James/Aperture/bugfix` on a new branch called `bugfix`. I `cd` into that directory and run whatever commands I need to boot up the application. Once I discover the bug and figure out what changes need to be made to fix, I commit the changes, push up my branch for review, and then return to the feature work I was doing on `/home/James/Aperture/cake`. No stashing hassle, and no worrying that my most recent pre-commit hooks aren’t running. ([View Highlight](https://read.readwise.io/read/01fddps94dww8d9d9hzs2tfg2n))
> Effectively, you can work on as many things in parallel as you’d like and can feel free to just pause your work and come back later. ([View Highlight](https://read.readwise.io/read/01fddpscn98r3nt7g0wha1nct8))
> It’s possible that being able to work on more things at once may also be detrimental to your productivity. You might wind up splitting your focus and try to do too many things in parallel. ([View Highlight](https://read.readwise.io/read/01fddpt1qep84x46f0m8byvdwc))
> `/home/James/worktrees/.bare` — This is the actual git repository, a bare instance. This stores only the repo metadata and has no actual working tree. I keep this in a separate hidden directory so that I can create new worktrees from this folder, but don’t accidentally try to check out a branch in the `worktrees` directory, which is an operation that can only be done in a working tree.
> `/home/James/worktree/.git` — This file just points to the `.bare` directory.
> `/home/James/worktree/master` — I always keep a copy of master, mostly so that if something comes up I can spin up a local copy of whatever is running in production.
> `/home/James/worktree/hotfix` — This branch is kept as a place where I perform hotfixes. It happens with enough frequency that I keep a dedicated worktree around for it.
> `/home/James/worktree/<feature>` — I have a number of different feature worktrees, which are divided by particular feature areas of the codebase. ([View Highlight](https://read.readwise.io/read/01fddpw9w2280p5xbfjhnrzftc))
> All in all, this style of working has made it so much easier for me to context switch when it is necessary, and provides me with small chunks of work that I can get done in less than 1 hour increments. ([View Highlight](https://read.readwise.io/read/01fddpws96gwzjffqkfhymry8y))