Back to Top

Unlocking Simplicity: A Guide to Git worktree

Updated 16 February 2024

The Git worktree feature in Git allows you to maintain multiple workspaces (work trees) for a single Git repository.

First of all, it’s possible to work on different tasks simultaneously by having multiple distinct copies of your project on your machine.

Additionally, each copy can have its branch and commits.

Advantage of Git worktree - 

Let’s explore the advantages of Git worktree. Then, we will see how to configure it in our code editor.

  • Isolation of Changes: Each work tree is independent of others, allowing you to work on different branches or commits simultaneously without interference.
  • Branching and Merging: Work trees enable easy branching and merging.
    You can switch between branches quickly, test features in isolation, and merge changes back together when ready.
  • Parallel Development: Multiple team members can work on different features or bug fixes simultaneously using separate work trees, promoting parallel development.
  • Version History: Work trees allow you to explore the project’s version history by switching between different commits or branches.
    This is crucial for understanding changes over time and reverting to a specific state if needed.
  • Experimentation and Testing: Work trees facilitate trying out changes without affecting the main project.

    You can create a new branch, make changes, test them, and roll back the branch if the changes are not satisfactory.

Setup - 

We can handle git worktrees using a user interface, which requires an extension, or manage them using git commands for greater flexibility.

Here we will see all Git worktree management with the git command line interface. 

Start your headless eCommerce
Find out More

To create a worktree - 

Step First open git bash at the command prompt at your root directory of the project.  and before creating of worktree switch or create your desired branch.

git worktree add -b <new-branch-name> <path-to-directory>

Below are grouped commands for creating the worktree in the same directory of the existing project and again open in vs code.

Window cmd

git worktree add -b testworktreename ../testworktreedir & cd testworktreedir &  code .

Mac command prompt

git worktree add -b testworktreename ..testworktreedir && cd testworktreedir && code .

Linux ubuntu

git worktree add -b webkulWorkTree ../webkulWorkTreeDir & cd webkulWorkTreeDir &  code .

let’s a breakdown of the command:

  • git worktree add: This is the main command for creating a new worktree.
  • -b <new-branch-name>: This option creates a new branch with the specified name. If the branch already exists, it will switch to that branch.
  • <path-to-directory>: Specify the path to the directory where you want to create the new worktree.

After the creation of the worktree, checkout at your given path for worktree will you find a new project with the given worktree name. 

Here you can you can choose your existing branch or can create a new branch or whatever you want as an independent project of the previous project’s copy.

To get the git worktree list - 

Git worktree list, the command displays a list of connected worktrees, including their linked branches and commits details.

git worktree list [-v | --porcelain [-z]]

The following options are :

  • -v: Provides more detailed information, including the commit hash and branch name.
  • –porcelain: Formats the output in a machine-readable way, making it easier to parse by scripts or other tools.
  • -z: When combined with –porcelain, it ends lines with a null character instead of a newline character, which can be advantageous for handling paths with spaces.

Here are a few examples:

  • git worktree list: This will display a list of worktrees with basic information.
  • git worktree list -v: This will provide more detailed information, including the commit hash and branch name.
  • git worktree list –porcelain: This will format the output in a machine-readable way.
  • git worktree list –porcelain -z: The command operates like the previous one, but it ends lines with a null character instead of a newline character.

To remove existing worktree - 

The git worktree remove the command is employed to eliminate a connected worktree in Git.

The syntax is as follows:

git worktree remove [-f]

Options explained:

  • -f: this “force” option permits the removal even if the worktree has uncommitted changes.

    Without using the -f option, Git will reject the removal of the worktree if it contains uncommitted changes.
  • <worktree-name>: Specify the path to the worktree that you want to remove.

These are a few useful commands to play with worktree; however, you can also explore each command of worktree from the official git document.

Conclusion: The Git worktree feature in Git makes it easier for developers to work on multiple tasks at the same time by allowing separate.

Additionally, it enables developers to switch between different branches without affecting the main working directory.

Independent workspaces for different branches or commits, streamlining the development process, and improving workflow management.

Start your  Headless Development with Webkul.
Happy Coding !!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.

Back to Top

Message Sent!

If you have more details or questions, you can reply to the received confirmation email.

Back to Home