title: What does `git branch -M` do with a single argument?
body: <p>I'm trying to upload a local repo to GitHub.  After creating a new repo on the site, GitHub gives the following instructions to execute locally:</p>
  <pre><code>git remote add origin https://github.com/MyOrg/repo-name.git
  git branch -M main
  git push -u origin main
  </code></pre>
  <p>When I execute them, I get an error at the <code>git branch</code> step:</p>
  <pre><code>$ git remote add origin https://github.com/MyOrg/repo-name.git
  $ git branch -M main
  error: refname refs/heads/trunk not found
  fatal: Branch rename failed
  </code></pre>
  <p>The <code>-M</code> flag to <code>git branch</code> is a combination of <code>-m</code> (move) and <code>-f</code> (force) to forcibly rename a branch.  The standard command for this is of the form</p>
  <pre><code>$ git branch -M &lt;oldbranch&gt; &lt;newbranch&gt;
  </code></pre>
  <p>Here, however, GitHub has given me only a single argument.  I assume, because no one involved can be bothered to make clear, that this one argument is <code>&lt;newbranch&gt;</code>.</p>
  <p>The <a href="https://git-scm.com/docs/git-branch" rel="nofollow noreferrer">docs</a> say only that</p>
  <blockquote>
  <p>With a -m or -M option, <code>&lt;oldbranch&gt;</code> will be renamed to <code>&lt;newbranch&gt;</code>. If <code>&lt;oldbranch&gt;</code> had a corresponding reflog, it is renamed to match <code>&lt;newbranch&gt;</code>, and a reflog entry is created to remember the branch renaming. If <code>&lt;newbranch&gt;</code> exists, -M must be used to force the rename to happen.</p>
  </blockquote>
  <p>with no explanation of what happens when <code>&lt;oldbranch&gt;</code> is not specified.  I assume that there's some secret algorithm that <code>git branch</code> uses to determine what it should use for <code>&lt;oldbranch&gt;</code>.  Knowing this would help me figure out what causes the error I get and how to fix it.  Am I allowed to know how <code>git branch -M</code> handles a single argument?</p>
  <p><strong>Resolution</strong></p>
  <p>@bk2204 suggests that <code>git branch</code> implicitly takes <code>&lt;oldbranch&gt;</code> to be the current branch if it's not provided as part of the command.  This makes enough sense to assume, though an authoritative source would still be great.</p>
  <p>@phd then suggests several ways of determining the current branch.  In my case:</p>
  <pre><code>$ git branch --show-current
  trunk
  
  $ git rev-parse HEAD
  HEAD
  
  $ git symbolic-ref HEAD
  refs/heads/trunk
  </code></pre>
  <p>Since the error I got trying to follow GitHub's instructions cites <code>refs/heads/trunk</code> explicitly, I further assume that <code>git branch</code> calls <code>git symbolic-ref HEAD</code> in order to determine the current branch.  Again, an authoritative confirmation of this would be awesome.</p>
  <p>According to its <a href="https://git-scm.com/docs/git-symbolic-ref" rel="nofollow noreferrer">documentation</a>, <code>git symbolic-ref</code> returns the filepath associated with the symbolic reference &quot;HEAD&quot;.</p>
  <p>In my case, the returned filepath <code>refs/heads/trunk</code> does not exist.  Instead, I have a file <code>refs/heads/master</code>:</p>
  <pre><code>$ ls -A1h refs/heads/
  master
  origin/
  </code></pre>
  <p>Lacking guidance from GitHub, I must assume that this is what they want me to rename &quot;main&quot;.  I can do that with <code>git branch -M</code> and explicitly supplying it as <code>&lt;oldbranch&gt;</code>:</p>
  <pre><code>$ git branch -M master main
  $ ls -A1h refs/heads/
  main
  origin/
  </code></pre>
  <p>After this is done, the command</p>
  <pre><code>$ git push -u origin main
  </code></pre>
  <p>works as intended, and the repo is pushed to GitHub.</p>