Working on mulitple braches at once using git worktree

Working on multiple branches at once

Git worktree enables you to have multiple working directories linked to the same repository. Each directory can have its own branch checked out. And you can edit the code for each feature without changing…


This content originally appeared on DEV Community and was authored by Shubham Kumar

Working on multiple branches at once

Git worktree enables you to have multiple working directories linked to the same repository. Each directory can have its own branch checked out. And you can edit the code for each feature without changing or stashing the changes you did on each branch.

Worktree creation

The following command will create a new directory, debug with branch1 checked out.

You can browse the ../debug directory and change the code as per your development for branch1.

git worktree add ../debug branch1

pwd
echo somenewfeature on branch 1 > somenewfeature
cat somenewfeature

/home/cold/Projects/Personal/gitworktreedemo/debug
somenewfeature on branch 1

In the meanwhile you can keep editing the root directory with the main branch.

pwd
echo somenewfeature on main 1 > somenewfeatureonmain
cat somenewfeatureonmain

/home/cold/Projects/Personal/gitworktreedemo/main
somenewfeature on main 1

You can run the git commands going inside each directory.

pwd
git add .
git status

/home/cold/Projects/Personal/gitworktreedemo/main
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file: somenewfeatureonmain


pwd
git add .
git status

/home/cold/Projects/Personal/gitworktreedemo/debug
On branch branch1
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file: somenewfeature

List existing worktrees

You can list your worktrees from the main as well as the debug directories.

git worktree list

/home/cold/Projects/Personal/gitworktreedemo/main e0ccbca [main]
/home/cold/Projects/Personal/gitworktreedemo/debug 42ad28a [branch1]

Remove a worktree

When you are finally done with the feature branch, you can delete the worktree using remove command.

git worktree remove ../debug
ls ..
git worktree list

main
/home/cold/Projects/Personal/gitworktreedemo/main e0ccbca [main]

Any commit you did on the feature branch will automatically be reflected in the main codebase.

git checkout branch1
git log --oneline

A   somenewfeatureonmain
82cad2d done
42ad28a A commit on branch1
e0ccbca A commit on main

Advice on working with worktrees

As the new directory will not be ignored by default, it is best to create your worktree directories out of git root

The way I create my worktrees is I create a new directory with project name. Then I clone my actual repository inside this directory and keep my worktrees on the same level as project.

For example, my main project is cloned inside main directory and I created debug directory in the same level as main.

tree .

.
├── debug
│   └── somenewfeature
└── main
    └── somenewfeatureonmain

2 directories, 2 files


This content originally appeared on DEV Community and was authored by Shubham Kumar


Print Share Comment Cite Upload Translate Updates
APA

Shubham Kumar | Sciencx (2025-03-02T14:50:00+00:00) Working on mulitple braches at once using git worktree. Retrieved from https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/

MLA
" » Working on mulitple braches at once using git worktree." Shubham Kumar | Sciencx - Sunday March 2, 2025, https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/
HARVARD
Shubham Kumar | Sciencx Sunday March 2, 2025 » Working on mulitple braches at once using git worktree., viewed ,<https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/>
VANCOUVER
Shubham Kumar | Sciencx - » Working on mulitple braches at once using git worktree. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/
CHICAGO
" » Working on mulitple braches at once using git worktree." Shubham Kumar | Sciencx - Accessed . https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/
IEEE
" » Working on mulitple braches at once using git worktree." Shubham Kumar | Sciencx [Online]. Available: https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/. [Accessed: ]
rf:citation
» Working on mulitple braches at once using git worktree | Shubham Kumar | Sciencx | https://www.scien.cx/2025/03/02/working-on-mulitple-braches-at-once-using-git-worktree/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.