2

Can someone please explain what i am doing wrong here?

$ git --version
git version 2.18.2

$ git checkout master
error: pathspec 'master' did not match any file(s) known to git.

$ git branch
* (HEAD detached at origin/trunk)

$ git branch -m master
fatal: Invalid branch name: 'HEAD'

some context: this repo was created using git svn and i want to rename the origin/trunk branch to master.

$ git branch -a
* (HEAD detached at origin/trunk)
  remotes/origin/svn-branch1
  remotes/origin/svn-branch2
  ...
Spongman
  • 9,665
  • 8
  • 39
  • 58
  • 1
    Is it that thing to do with GitHub renaming `master` to `main`? Try `git checkout main` – DBedrenko Jun 18 '20 at 18:54
  • 1
    You don’t have any branches it seems. Create one with `git branch ` or in your case better `git checkout -b `. – mkrieger1 Jun 18 '20 at 18:57
  • no, i have tons of branches, just not one called 'master' (this repo was imported using `git svn`) – Spongman Jun 18 '20 at 18:58
  • Does this answer your question? [Create new branch based on current branch to work on a new feature](https://stackoverflow.com/questions/46706821/create-new-branch-based-on-current-branch-to-work-on-a-new-feature) – mkrieger1 Jun 18 '20 at 18:58
  • Ah, that's vital information. – mkrieger1 Jun 18 '20 at 18:59
  • no, i don't want to create a new branch, i want to rename the existing one. i think the quoted commands indicate that. – Spongman Jun 18 '20 at 19:00
  • What command did you use to invoke `git-svn`? What is the output of `git branch -a`? – mkrieger1 Jun 18 '20 at 19:01
  • 1
    See https://stackoverflow.com/questions/58635202/cloning-a-git-svn-repository-leads-to-disappearing-branches – mkrieger1 Jun 18 '20 at 19:03
  • @DBedrenko That's not consistent with the information displayed here, so it appears to be entirely unrelated. – David Z Jun 18 '20 at 19:04
  • Yes you *do* want to create a new branch, you just don’t know it yet :) In Git, "branch" does not exactly mean the same as in SVN, it’s just a movable reference to a commit. – mkrieger1 Jun 18 '20 at 19:07

2 Answers2

2

TL;DR

Use:

git checkout -b master origin/trunk

followed by whatever you will do to copy this Git repository elsewhere. Feel free to delete the remote-tracking name origin/trunk if you're not going to do any further SVN imports:

git branch -d -r origin/trunk

You can repeat this for each SVN branch name, if you like.

Long

Some things you need to know:

  1. You don't have any branches,1 at the moment:

    $ git branch
    * (HEAD detached at origin/trunk)
    
  2. You do have some remote-tracking names (Git calls these remote-tracking branch names, but they are not branch names, so I like to omit the word branch here). Your SVN import created these:

    $ git branch -a
    * (HEAD detached at origin/trunk)
      remotes/origin/svn-branch1
      remotes/origin/svn-branch2
    

The first line is of course a repeat, but it indicates that there is a remotes/origin/trunk. The extra word remotes here is from the fact that the full names of each of these is refs/remotes/origin/name; Git normally omits both refs/ and remotes/, except for git branch -a which only omits refs/ on the remote-tracking names (for no apparent reason).

Now, git checkout won't automatically create a (local) branch name unless there's one matching remote-tracking name. Your SVN import did not create an origin/master, though it did create an origin/trunk.

Normally, remote-tracking names exist in your Git repository because of branch names that existed in some other Git repository. If that were the case, you'd need to rename those branches in the other Git repository. But in your case, there is no other Git repository (yet)—there is only an SVN repository. In the SVN repository, there was a name trunk and your conversion copied that name here.

What you need to do is create the local branch name master. The simplest command is git checkout -b master starting-point, as that will also put you on that branch (which you likely want). The starting point is the commit to which you want this new name master to refer.


1In this context, the word branch means branch name. In other contexts in Git, it means other things. Git branches do not correspond very well to SVN branches. See also What exactly do we mean by "branch"?

torek
  • 448,244
  • 59
  • 642
  • 775
0

With Git 2.39 (Q4 2022), The error message will be clearer:

cannot rename the current branch while not on any

See commit 77e7267 (26 Oct 2022) by Rubén Justo (rjusto).
(Merged by Taylor Blau -- ttaylorr -- in commit 0c02561, 30 Oct 2022)

branch: error copying or renaming a detached HEAD

Signed-off-by: Rubén Justo

In c847f53 ("Detached HEAD (experimental)", 2007-01-01, Git v1.5.0-rc1 -- merge) an error condition was introduced in rename_branch() to prevent renaming, later also copying, a detached HEAD.

The condition used was checking for NULL in oldname, the source branch to rename/copy.
That condition cannot be satisfied because if no source branch is specified, HEAD is going to be used in the call.

The error issued instead is:

fatal: Invalid branch name: 'HEAD'

Let's remove the condition in copy_or_rename_branch() (the current function name) and check for HEAD before calling it, dying with the original intended error if we're in a detached HEAD.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250