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

Re: "Branch exists" error while trying to rename a non-existing branch to an existing one

From
Kaartic Sivaraam <kaarticsivaraam91196@gmail.com>
Date
Jul 10, 2017, 14:14 UTC
Message-ID
<1499696071.1647.2.camel@gmail.com>
In-Reply-To
<xmqqd1998mks.fsf@gitster.mtv.corp.google.com>
On Sun, 2017-07-09 at 11:57 -0700, Junio C Hamano wrote:
Show 7 quoted lines
> This is borderline "meh" at least to me.  An argument against a
> hypothetical version of Git that "fixes" your issue would be that no
> matter what the source of renaming is, as long as 'master' exists,
> "branch -m" shouldn't overwrite it, and it is a good thing to remind
> the user that 'master' exists and the user meant to rename it to
> something else.
> 

I'm not against the fact of reminding the user about an existing branch. I'm with the fact that, warn him when he really has to care about a branch being overwritten i.e., when he tries to rename an "existing" branch to one that refers to another existing branch.

I found this behaviour odd as I try to relate it with the 'mv' command. It's behaviour is as follows,

    $ ls
    file  some_file
    $ mv nothing file
    mv: cannot stat 'nothing': No such file or directory

If I haven't missed anything the following patch seems to fix the problem,

diff --git a/builtin/branch.c b/builtin/branch.c
index 48a513a84..2869aaca8 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -476,7 +476,10 @@ static void rename_branch(const char *oldname, const char *newname, int force)
         */
        clobber_head_ok = !strcmp(oldname, newname);
 
-       validate_new_branchname(newname, &newref, force, clobber_head_ok);
+       if(ref_exists(oldref.buf))
+               validate_new_branchname(newname, &newref, force, clobber_head_ok);
+       else
+               die(_("Branch '%s' does not exist."), oldname);
 
        reject_rebase_or_bisect_branch(oldref.buf);
-- 
Kaartic
Previous: Junio C Hamano
Message 3 of 3 in “"Branch exists" error while trying to rename a non-existing branch to an existing one”
  1. Kaartic SivaraamJul 9, 2017
  2. Junio C HamanoJul 9, 2017
  3. Kaartic SivaraamJul 10, 2017

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.