Parallel Programming In Git
You can avoid switching your Git branches and improve your workflow with one, simple concept known as Git worktree in less than 2 minutes.
Join the DZone community and get the full member experience.Join For Free
There are many Git Tips and Git Best Practices that can help you in your day to day activities. You will save you valuable time, and with these Git tricks, you can improve your workflow.
In this post you will learn Parallel Programming in Git (in 2 minutes)
So let's start!
Imagine, You are working on a large legacy project in the Git repo. And at some hours you have to support the project and other hours you have to add new features.
And to be honest, this is quite common in many organizations.
Now you can not switch between branches all the time - it might take quite a long time to complete the switch as it is a large project. And if you do switch via your IDE, it might crash.
Let me share one another example - You can in the middle of your adding a new feature, and at the same time, a new live bug reported, which is critical to fix. How yo deal with this.
There are a few ways to achieve this like you can stash your changes. But in this case, one of the best way is to work on bug fixing and adding new features without switching the branches. And yes, it is possible in Git.
You can use the Worktrees to achieve parallel programming in Git.
In this post, I will share 3 Worktrees Git commands so you can achieve the parallel programming.
Before that, let's see the basics of worktree. Git worktree command allows you to have multiple working directories associated with one git repo. A git repository can support multiple working trees, allowing you to check out more than one branch at a time.
Now let's see three Git commands to manage worktree.
By default you have only one worktree; it is known as the Primary / Main worktree. And a newly created worktree known as Linked worktree.
View Your Worktrees
Before creating a worktree, you can view how many worktree you currently have. You can use a simple command.
git worktree list
It will display all the worktrees available in your Git repo.
Add a New Worktree
Let’s add a new worktree and see how you can use it.
git worktree add <your new work tree name>
The above command will create a new worktree. You can confirm it by simply list all your worktree with the git worktree list command.
Here is the screenshot.
As you can see in the above screenshot, you have two worktree in the same repo.
Technically at the same time, you can fix some bugs and add new features in the Git repo without checkout any other branch.
Delete a Worktree
Once you are done with your new feature, You can simply delete your worktree with a simple command.
git worktree remove <your worktree name>
Git is a powerful version control system. There is no doubt that with Git Best Practices, you can improve the git workflow and overall efficiency.
By default, there is only one working directory with your repo. And with the help of Git worktree, you can have multiple working directories.
In Linked working trees, you can not have the same branch name-checked out.
Hope with these simple commands, you can achieve parallel programming.
Let me know your thoughts.
Opinions expressed by DZone contributors are their own.