Open a Git Branch as a Separate Folder with git worktree

Switching branches swaps the files in a project folder. With git worktree, a branch gets its own folder, so we can browse and edit two branches side by side.
Git
Tools
Tutorial
Author

Abdullah Al Mahmud

Published

November 28, 2026

A box for a single Git repository connects to two folders. The folder named project shows the main branch with report.md. The folder named project-draft shows the draft branch with report.md and notes.md.

One repository, two folders, each showing its own branch.

A file manager shows only the files of the branch that is currently checked out. To look at another branch, we usually switch to it, and the files in the folder change under our feet.

git worktree offers an easier way: it opens a branch as a separate folder. Both folders stay available, and we can browse them like any other folders.

The problem with switching

Suppose we have a project with two branches, main and draft, and draft holds an extra file, notes.md. On main, we cannot see it:

ls
report.md

We could run git switch draft, look at the file, and switch back. That gets tiring, and unfinished changes can follow us from one branch to the other or block the switch.

Give the branch its own folder

From inside the project folder, we run:

git worktree add ../project-draft draft
Preparing worktree (checking out 'draft')
HEAD is now at 4f9ea19 Add notes

The command has two parts:

  • ../project-draft is the new folder, created next to the project folder.
  • draft is the branch to open in it.

Now the new folder has the files of draft:

ls ../project-draft
notes.md
report.md

The original folder is still on main. We can open project-draft in the file manager, in an editor or in another terminal, and keep both in view.

Tip

Create the new folder outside the project folder, as ../ does here. A folder inside the project would show up as untracked content in the first branch.

To list all the folders and their branches:

git worktree list
/home/user/project        b2a07bd [main]
/home/user/project-draft  4f9ea19 [draft]

Edit and commit

A commit made in a folder goes to that folder’s branch. Let’s add a line to notes.md:

cd ../project-draft
echo 'More text.' >> notes.md
git add notes.md
git commit -m "Extend notes"
[draft 999734f] Extend notes
 1 file changed, 1 insertion(+)

The [draft ...] part confirms where the commit went. There is no need to copy anything back: both folders share one repository, so main’s folder can already see the commit.

cd ../project
git log --oneline draft
999734f Extend notes
4f9ea19 Add notes
b2a07bd Add report

If the project has a remote, we push from either folder. The first push of a new branch needs its name:

git push -u origin draft

Things to know

One branch, one folder

A branch can be checked out in only one folder at a time. Trying to switch to draft in the first folder now fails:

git switch draft
fatal: 'draft' is already checked out at '/home/user/project-draft'

This is helpful: it stops two folders from changing the same branch behind each other’s back.

Only committed files appear

The new folder is filled from the branch’s commits. Uncommitted changes, untracked files and ignored files stay in the original folder. If a script needs an ignored file, such as a local settings file, we copy it over ourselves.

It is not a second clone

The new folder holds no copy of the history. Its .git is a small file that points back to the main repository:

cat ../project-draft/.git
gitdir: /home/user/project/.git/worktrees/project-draft

So we should keep the main project folder where it is.

Create a new branch and its folder together

The -b option makes a new branch, starting from the current one:

git worktree add -b experiment ../project-experiment
Preparing worktree (new branch 'experiment')
HEAD is now at b2a07bd Add report

Remove the folder

When we have finished, we remove the folder with Git:

git worktree remove ../project-experiment

If the folder has changes or new files that are not committed, Git refuses, so that we do not lose them:

fatal: '../project-experiment' contains modified or untracked files, use --force to delete it

We commit or delete those files and try again. Removing the folder keeps the branch and its commits.

If we have already deleted a folder in the file manager, Git still remembers it. This clears the record:

git worktree prune

Switch or worktree?

Task Better choice
A quick look at another branch, with nothing unfinished git switch
Browsing two branches side by side git worktree
Urgent fix while the current work is half done git worktree
Comparing the output of two versions git worktree

Quick reference

git worktree add ../folder branch          # open an existing branch
git worktree add -b newbranch ../folder    # new branch in a new folder
git worktree list                          # show folders and branches
git worktree remove ../folder              # remove a folder
git worktree prune                         # forget deleted folders

R-bloggers

R posts from Stat Mania are also shared on R-bloggers, a hub of R blogs.