Parallel Programming In Git
Tags: git, github
Published: 2020-10-12 05:56:20

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)** 


In case you want to [download 31 Advanced Git tips](https://acompiler.com/Git-Tips/), you can download in a printable [pdf form here](https://acompiler.com/Git-Tips/).

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 at 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 ways 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.

---

##Worktree

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.

---

##Worktree Terminology

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 Git command](https://acompiler.com/git-commands/). 

`git worktree list`

It will display all the worktrees available in your Git repo.

![alt text](https://dzone.com/storage/temp/14025460-1601875657808.png)

---

##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.

![add a work tree image](https://dzone.com/storage/temp/14025461-1601875917153.png)


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>`

![Delete work tree image](https://dzone.com/storage/temp/14025464-1601876159944.png)

---

##Conclusion 

Git is a powerful version control system. There is no doubt that  [with Git Best Practices, you can improve the git workflow](https://acompiler.com/git-best-practices/) 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.

---