git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: git worktree fails to recreate existing branch even with -B

From
Duy Nguyen <pclouds@gmail.com>
Date
Feb 15, 2016, 09:41 UTC
Message-ID
<20160215094154.GA11698@lanh>
In-Reply-To
<344AE670-BF9A-4779-AFB0-50B870E1D488@jetbrains.com>
On Tue, Feb 09, 2016 at 05:54:01PM +0300, Kirill Likhodedov wrote:
Show 17 quoted lines
> Git doesn’t allow me to execute 
>     git worktree add -B <branch> <path> <start-point>
> where <branch> already exists in the repository.
> 
> The command prints the following:
>     Preparing <path> (identifier <branch>)
>     fatal: Refusing to point HEAD outside of refs/
> 
> I’m trying to create a worktree on an existing branch <branch>,
> which should point to <start-point>. This obviously should fail with
> “-b”, but I use “-B” and expect it to be reset to <start-point> as
> mentioned in the docs:
> 
>     By default, -b refuses to create a new branch if it already exists.  
>     -B overrides this safeguard, resetting <new-branch> to <branch>.
> 
> Do I miss something or there is a bug?
According to the man page, this looks like a bug.
Show 11 quoted lines
> Steps to reproduce:
> 
> git init wt
> cd wt
> echo 'asd' > a.txt ; git add a.txt ; git commit -m initial
> git branch br1
> git worktree add -B br1 ~/temp/br1 master
> 
> Error message:
> Preparing /Users/loki/temp/br1 (identifier br1)
> fatal: Refusing to point HEAD outside of refs/
GIT_TRACE=2 gives me

trace: built-in: git 'symbolic-ref' 'HEAD' '' fatal: Refusing to point HEAD outside of refs/

So we pass wrong argument to symbolic-ref. The '' should be 'refs/heads/br1'. This patch seems to fix it.

-- 8< --
diff --git a/builtin/worktree.c b/builtin/worktree.c
index 475b958..d5b319f 100644
--- a/builtin/worktree.c
+++ b/builtin/worktree.c
@@ -202,7 +202,7 @@ static int add_worktree(const char *path, const char *refname,
 
 	/* is 'refname' a branch or commit? */
 	if (opts->force_new_branch) /* definitely a branch */
-		;
+		strbuf_addf(&symref, "refs/heads/%s", refname);
 	else if (!opts->detach && !strbuf_check_branch_ref(&symref, refname) &&
 		 ref_exists(symref.buf)) { /* it's a branch */
 		if (!opts->force)
-- 8< --
--
Duy
Previous: Kirill LikhodedovNext: Nguyễn Thái Ngọc Duy
Message 2 of 6 in “git worktree fails to recreate existing branch even with -B”
  1. Kirill LikhodedovFeb 9, 2016
  2. Duy NguyenFeb 15, 2016
  3. 1/2 worktree: fix "add -B"Nguyễn Thái Ngọc Duy, Feb 15, 2016
  4. 2/2 worktree add -B: do the checkout test before update branchNguyễn Thái Ngọc Duy, Feb 15, 2016
  5. Junio C HamanoFeb 15, 2016
  6. Duy NguyenFeb 16, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.