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

Re: [funny] "git checkout -t origin/xyzzy" seems to misbehave

From
Daniel Barkalow <barkalow@iabervon.org>
Date
Sep 21, 2008, 18:36 UTC
Message-ID
<alpine.LNX.1.00.0809211406150.19665@iabervon.org>
In-Reply-To
<7v1vzd29i3.fsf@gitster.siamese.dyndns.org>
On Sun, 21 Sep 2008, Junio C Hamano wrote:
Show 7 quoted lines
> When you
> 
>  * are on a branch "foo" that is not "bar",
>  * have "origin/bar",
>  * and already have a local branch "bar",
> 
> "git checkout -t origin/bar" seems to misbehave.

It's always been the case if the thing that fails is changing the ref for HEAD, you're left with the index and working tree changed but the ref unchanged. OTOH, the C conversion lost the early checks that a new branch name is plausible in advance of trying anything.

commit c36a025b20ac752f8960bc36dcbab98ca1824657
Author: Daniel Barkalow <barkalow@iabervon.org>
Date:   Sun Sep 21 14:25:31 2008 -0400
    Check early that a new branch is new and valid
    
    If you fail to update refs to change branches in checkout, your index
    and working tree are left already updated. We don't have an easy way
    to undo this, but at least we can check things that would make the
    creation of a new branch fail. These checks were in the shell version,
    and were lost in the C conversion.
    
    The messages are from the shell version, and should probably be made nicer.
    
    Signed-off-by: Daniel Barkalow <barkalow@iabervon.org>
diff --git a/builtin-checkout.c b/builtin-checkout.c
index 9377a1c..4497b70 100644
--- a/builtin-checkout.c
+++ b/builtin-checkout.c
@@ -580,6 +580,18 @@ no_reference:
 		return checkout_paths(source_tree, pathspec);
 	}
 
+	if (opts.new_branch) {
+		struct strbuf buf;
+		strbuf_init(&buf, 0);
+		strbuf_addstr(&buf, "refs/heads/");
+		strbuf_addstr(&buf, opts.new_branch);
+		if (!get_sha1(buf.buf, rev))
+			die("git checkout: branch %s already exists", opts.new_branch);
+		if (check_ref_format(buf.buf))
+			die("git checkout: we do not like '%s' as a branch name.", opts.new_branch);
+		strbuf_release(&buf);
+	}
+
 	if (new.name && !new.commit) {
 		die("Cannot switch branch to a non-commit.");
 	}
Previous: Alex Riesen
Message 3 of 3 in “[funny] "git checkout -t origin/xyzzy" seems to misbehave”
  1. Junio C HamanoSep 21, 2008
  2. Alex RiesenSep 21, 2008
  3. Daniel BarkalowSep 21, 2008

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.