Using Git Worktrees Instead of Multiple Clones

Developers often have multiple changes in progress – features, bug fixes, R&D spikes – current work interruptions that force pausing the work in progress to work on something in parallel. The question is what is the best source control approach to keep the work in progress while working on the parallel task? Stashing and committing are valid options; another option is to simply leave it as is and use a different workspace, enabling using multiple workspaces concurrently.

When requiring multiple workspaces of the same git repository, one can simply clone the repo multiple times. However, a useful and space-saving approach is to instead use git worktrees. Git worktrees allow multiple concurrent branch checkouts from the same repository.

Interestingly, all git workspace directories are worktrees – the first workspace created with a git clone is called the “main worktree”. Additional workspaces created with the git worktree command are called “linked worktrees”.

The branch and file usage in a linked worktree is the same as in the main worktree – a local working directory of a git repo for working with the files and committing them. However, the key benefits of git worktrees over cloning new repositories are:

1. — All worktrees can use updates from one worktree’s repo fetch. This allows updating other branches offline.
2. — Each worktree has full access to the branches in the repo. This allows merging or cherry picking among the branches.
3. — Less disk space usage. It is one git repository serving multiple workspaces instead of multiple git repositories cloned from the same remote repository.

One constraint to keep in mind: Git enforces a specific branch checkout in one worktree at a time. This means each worktree has its own branch.

It’s very simple to create and remove worktrees:

Create Worktree:

git worktree add <path> <branch>
<path> is a relative or absolute directory for the worktree working location.
<branch> is the branch name to checkout in the worktree location.
Review the git worktree command for additional add options (can select a commit and create a new branch, can avoid creating a branch [detached mode], and more).

Remove Worktree:

git worktree remove <path>
Removing a worktree does not affect any branches.

We can also list the worktrees for a repository:

git worktree list

Conclusion

Using git worktrees is a more efficient and functional approach to having multiple workspaces of the same repository.

About Intertech

Intertech is a Software Development Consulting Firm that provides single and multiple turnkey software development teams, available on your schedule and configured to achieve success as defined by your requirements independently or in co-development with your team. Intertech teams combine proven full-stack, DevOps, Agile-experienced lead consultants with Delivery Management, User Experience, Software Development, and QA experts in Business Process Automation (BPA), Microservices, Client- and Server-Side Web Frameworks of multiple technologies, Custom Portal and Dashboard development, Cloud Integration and Migration (Azure and AWS), and so much more. Each Intertech employee leads with the soft skills necessary to explain complex concepts to stakeholders and team members alike and makes your business more efficient, your data more valuable, and your team better. In addition, Intertech is a trusted partner of more than 4000 satisfied customers and has a 99.70% “would recommend” rating.