Have you ever been in the middle of coding a feature, and out of nowhere you get a notification that production is down? If you’re using Git, you now have a few options: commit your work, stash your work, or discard your work. All these come with their problems depending on the situation.
If you commit half-baked code, you may want to come back later and rebase to clean up your commit history. Stashing can cause problems because what if you need to stash extra code later before switching back to your original feature work?
I’ll throw away my current changes if they’re small, but otherwise, no thanks.
I recently stumbled upon Git worktree, and it has been an amazing boost in my productivity.
Git worktree allows us to check out many branches in a git repository. This lets us switch between different branches in a Git repository without losing our non-committed work. Git does this by creating new, separate directories called a “linked worktree” that gets associated with a single “main worktree” (the directory created using git clone
or git init
).
To checkout a new worktree, we use the worktree add
command: git worktree add <path> <branch_name>
. Worktrees get created as new directories. We give the add command a path to where we want the new directory created and which git branch we want to checkout.
Like git checkout -b
, we can create brand new branches by adding the -b
argument (git worktree add -b <branch_name> <path>
).
We can see all the worktrees we’ve created using the git worktree list
command.
With our different linked worktrees created, we can change code and switch between isolated instances of our codebase. The changes done in one worktree do not affect the other worktrees.
What about if we want to commit and push our changes out to Github? No problem! Git worktree is a way to check out many branches. So, once we’re done making changes in our worktree, we can git add
, git commit
, and git push origin
like if we are working on a different branch.
The changes would show up on Github under the <branch_name>
we gave in git worktree add <path> <branch_name>
.
When we are done with a worktree, we can call git worktree remove <path_to_worktree>
to delete it.
I keep all worktrees in a separate directory ~/.worktree
, outside of my general projects directory ~/projects
, to reduce clutter and keep “main worktrees” explicit from “linked worktrees”.
I am constantly switching between branches and code, and Git worktree has been a huge help to my workflow!