Logo

dev-resources.site

for different kinds of informations.

Improving Your Productivity With Git Worktrees

Published at
1/11/2025
Categories
git
beginners
tutorial
devops
Author
implosion
Categories
4 categories in total
git
open
beginners
open
tutorial
open
devops
open
Author
9 person written this
implosion
open
Improving Your Productivity With Git Worktrees

Working on applications and services with many moving parts is hard and time consuming: you might be responsible for long running test suits. You might be responsible of peer reviewing multiple Merge Requests at a time or even, you might be responsible of fixing a critical bug found in production that can't wait. These are all scenarios we have faced before and have used git-switch to help move between branches. However, this is very limiting since you can only work on one branch at the time and your dev environment will have to change across branches. There is a better way.

What is Worktree

git-worktree is a way to work on multiple branches at a time in a non-blocking pattern. We can checkout multiple branches and we can open each on it's on Integrated Development Environment (IDE) or Code Editor.

# Checkout a new branch based on the current branch
git worktree add /path/to/new-branch
Enter fullscreen mode Exit fullscreen mode
# Way to create a new branch where branch and dir name are different
git worktree add /path/to/branch -b my-new-branch
Enter fullscreen mode Exit fullscreen mode
# To work on an exisiting branch
git worktree add /path/to/branch existing-branch
Enter fullscreen mode Exit fullscreen mode
# Deliting a worktree
git worktree remove /path/to/branch
Enter fullscreen mode Exit fullscreen mode

Git worktrees are synced to the main repo (where the checkout) happened which means we can manage them from there. Further, object references (refs) are also shared across the main worktree (repo where the checkout happened) and other worktrees.

Important

Git manages the worktrees for us. This means that it maintains metadata to remove and cleanout stale worktrees. To avoid this, we have to use the lock feature of worktree and/or configure the git-gc (Garbage Collection) utility.

git worktree add /path/to/new-branch --lock

# Or

git worktree lock --reason "I am not done with you" /path/to/new-branch
Enter fullscreen mode Exit fullscreen mode

So, now that we know how to create a new worktree, how can worktree help us where switch can't?

Switch VS Worktree

Checkout Process

  • git-switch
# git-checkout
git add .
git commit -m "Committing before moving to another branch"
# git push -u origin current-branch

# Checkout a new branch based on main
git switch -c hotfix/broken-link main

# Or
git add .
git stash
git switch -c hotfix/broken-link main

Enter fullscreen mode Exit fullscreen mode
  • git-worktree
# By default, worktrees checkout the new branch from the main branch unless specifid otherwise.
git worktree add ../urgent-fix --reason "To fix a broken link in our UI" --checkout hotfix/broken-link 

Enter fullscreen mode Exit fullscreen mode

We can reduce the amount of time we take to get started on the new branch as we don't have to stage or commit anything.

Non-Blocked Workflows.

Because you can only checkout one branch at a time with git-switch, if you are running for example, a deployment , you can't make changes to the source code without risking introducing syntax errors that the deployment command line interface (CLI) will pick up and crash the deployment.

git worktree add ../deployment-test --reason "To test my deployment for failure" --checkout my-working-branch

make -f ../deployment-test/Makefile

Enter fullscreen mode Exit fullscreen mode

Using worktrees allows us to delegate the deployment testing to another worktree with our working branch checked out while we can continue to work on the current copy we are on in the main worktree.

Quick Experimentation

Using git-switch to set an experimentation branch can be time consuming and expensive as mentioned before. Additionally, if we are experimenting with new packages and configurations, these are things that are not usually tracked and are added to the .gitignore file to avoid accidental commits. To avoid these issues, we can leverage git-worktree.

# Checkout a throw-away branch for a quick experiment
git worktree add ../my-experiment --checkout working-branch --detach

Enter fullscreen mode Exit fullscreen mode
# Remove the branch when done 
git worktree remove ../my-experiment

Enter fullscreen mode Exit fullscreen mode

Conclusion

As developers, we are always moving fast and living dangerously. It is always a positive when we can find ways to help us continue to do so with reduced risks. Git worktrees is a great way to speed up your workflow and experiment in a non-blocking, non-destructive manner.

Hope you found this guide useful and as awesome as I did.

References

  1. https://git-scm.com/docs/git-worktree
  2. https://ftp.gnu.org/old-gnu/Manuals/make-3.80/html_node/make.html
  3. https://git-scm.com/docs/git-config#Documentation/git-config.txt-gcworktreePruneExpire
git Article's
30 articles in total
Favicon
πŸ›‘οΈ Security Measures: Safeguarding Your Codebase πŸ”’
Favicon
Unlock Your Coding Potential with the GitHub Copilot Global Bootcamp!
Favicon
Kickstart Your Developer Journey: A Beginner’s Guide to Software Development Success
Favicon
Git Commands Every Developer Must Know πŸ”₯
Favicon
check out this!
Favicon
Git Merge VS Git Rebase: Which One Should YOU Use?
Favicon
A quick and simple guide on how to make branches for open-source development
Favicon
Improving Your Productivity With Git Worktrees
Favicon
GitHub Makeover: Create a Profile README That Stands Out and Connects! πŸ‘¨β€πŸ’»
Favicon
How to Fix Git Issues: Committing and Pushing Without Pulling Causes Stuck Branches
Favicon
Undo Mistakes in Git: Revert, Reset, and Checkout Simplified
Favicon
My First npm Package!
Favicon
Mastering Git and GitHub: A Guide for New Team Members
Favicon
GIT hack: Sort and show recent branches
Favicon
GIT
Favicon
πŸŽ‰ Simplify Laravel CRUD Operations with Ease! πŸš€
Favicon
Why I Stopped Using Plain Git Pull (And Why You Should Too)
Favicon
Why I Built commit-ai: A Story About Git Security and Team Safety
Favicon
How to Link git to GitHub via SSH on Windows
Favicon
I built Zeet. A Git-like version Control System
Favicon
Effective Git Branch Merging for Teams πŸš€
Favicon
Mastering Git Workflows: Beyond Basic Commands
Favicon
Como enviar somente novos commits em uma branch que jΓ‘ mesclada.
Favicon
Getting Git to Work on Apple Silicon
Favicon
Git avanzado: ΒΏQuΓ© es cherry pick? πŸ’
Favicon
Git Cheatsheet that will make you a master in Git !
Favicon
How to upgrade Git to latest version on macOS
Favicon
Windows dotted paths
Favicon
Using git Conditionals to Manage Your Git Identities
Favicon
Can a Python Server (Serving HTML with Jinja2) Interact and Modify Files in a Jenkins Pipeline?

Featured ones: