Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - Bare repo cannot have a worktree for master branch - WHY?

I'm working on some server-side software to do a merge. By using git worktree it's possible to check out a given branch for a bare repo and merge another branch into it. It's very fast, even with large repositories.

The only exception seems to be merging to master. When I do git worktree add /tmp/path/to/worktree master I get an error:

fatal: 'master' is already checked out at '/path/to/bare/repo'

But this is clearly not true, git worktree list gives:

/path/to/bare/repo (bare)

...and of course, there is no work tree at that path, just the bare repo files you'd expect.

UPDATE: I got in touch with the git maintainers, and they have agree that this might be a bug. I have a preliminary patch from them to test. In addition, I have also been able to reproduce the desired behavior without the patch.

At this point I'm not entirely sure what the boundary condition or root cause is, and there might be a fix forthcoming from git.

like image 328
mtutty Avatar asked Oct 05 '16 20:10

mtutty


People also ask

What is a git Worktree And why would you require to use multiple Worktrees?

A git repository can support multiple working trees, allowing you to check out more than one branch at a time. With git worktree add a new working tree is associated with the repository, along with additional metadata that differentiates that working tree from others in the same repository.

How do I add Worktree?

add worktree. Add a config option worktree. guessRemote that allows users to configure the default behaviour for themselves. With add , if no branch argument, and neither of -b nor -B nor --detach are given, the command defaults to creating a new branch from HEAD. If worktree.

What is the purpose of git Worktree?

Git Worktrees are a feature that allow you to have a single repository with multiple checked out working branches at the same time.


Video Answer


1 Answers

Turns out that this is a bug in git, beginning with the worktree implementation in 2.5 and higher.

The bare repository still has a HEAD reflink. Whatever that link points to is considered by git (up to and including 2.10) to be the default branch for new cloners, and is (wrongly) treated as if it's on an active work tree.

I have received a patch from the git maintainers to fix this behavior, and it seems to work. Alternatively, it should be possible to use update-ref on the bare repo to switch from master temporarily.

I'll be testing both of these options.

like image 104
mtutty Avatar answered Sep 21 '22 08:09

mtutty