Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'<Branch>' is already checked out at '</other/location>' in git worktrees

I started using git worktrees. It seems to work, but I'm getting this error when attempting to check out a branch in the cloned worktree:

fatal: '<branch>' is already checked out at '</other/location>' 

How do I get around this without deleting the .git/worktrees directory?

like image 698
Harald Nordgren Avatar asked Jan 09 '17 09:01

Harald Nordgren


People also ask

How to remove worktree in Git?

To remove a locked worktree, specify --force twice. With add , create a new branch named <new-branch> starting at <commit-ish> , and check out <new-branch> into the new worktree. If <commit-ish> is omitted, it defaults to HEAD . By default, -b refuses to create a new branch if it already exists.

How do Git worktrees work?

A Git worktree is a linked copy of your Git repository, allowing you to have multiple branches checked out at a time. A worktree has a separate path from your main working copy, but it can be in a different state and on a different branch.

What is work tree?

The working tree is the set of all files and folders a developer can add, edit, rename and delete during application development. The status command can provide insight into how the Git working tree behaves. More colloquially, developers often refer to the Git working tree as the workspace or the working directory.


2 Answers

Git won't let you check out the same branch twice, because if you do, and then go to one of the two work-trees and make a new commit, you'll set yourself up for misery when you go back to the other work-tree.

If you have actually removed the other work-tree, simply run git worktree prune to make Git realize this. If you have not actually removed the other work-tree, don't check it out twice: it's no fun.

like image 140
torek Avatar answered Oct 05 '22 12:10

torek


If you search for "worktree" in the manual git-checkout(1) you'll find

git checkout --ignore-other-worktrees <branch> 

But you probably just want to do the following so when you move (commit to) the branch in one worktree, the HEAD of the other worktree is not moved.

git checkout --detach <branch> 
like image 41
Qian Avatar answered Oct 05 '22 10:10

Qian