64

I am trying to add an existing project to GitHub using the command line. I am in the relevant working directory in the terminal and am trying to use the git init -b main command.

Initially, I was getting an error relating to xcode:

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

I tried xcode-select --install but the software was unavailable from the update server, so I downloaded 'Command Line Tools for Xcode 12' from https://developer.apple.com/download/more/.

Now on entering git init -b main I am getting the following:

error: unknown switch `b'
usage: git init [-q | --quiet] [--bare] [--template=<template-directory>] [--shared[=<permissions>]] [<directory>]

--template <template-directory>
                      directory from which templates will be used
--bare                create a bare repository
--shared[=<permissions>]
                      specify that the git repository is to be shared amongst several users
-q, --quiet           be quiet
--separate-git-dir <gitdir>
                      separate git dir from working tree

I am running git version: 2.24.3 (Apple Git-128)

Any help much appreciated!

1
  • 3
    Just say git init and then change master to main if you want to.
    – matt
    Nov 11, 2020 at 13:44

5 Answers 5

70

The -b flag is only available in version 2.28 or later, you need to upgrade your Git.

On debian-based Linux systems such as Ubuntu, do the following:

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt update
sudo apt install git -y
51

git 2.24 doesn't have option -b/--initial-branch. It was added in git 2.28. You need to upgrade to use the option.

Or, as @matt said, create a repo and then rename the branch:

git init repo
cd repo
git branch -m master slave
2
  • 12
    error: refname refs/heads/master not found. fatal: Branch rename failed Instead: cd repo; git checkout -b main Dec 21, 2020 at 23:21
  • 1
    @RobinA.Meade I saw that a commit must already exist so the branch master exists
    – fguillen
    Jul 21, 2021 at 14:09
36

prior to git v2.28

git init                              # ①
git symbolic-ref HEAD refs/heads/main # ②

① After git init, the branch master does not actually exist. Branches get created only when they have at least one commit.

② This updates .git/HEAD to contain ref: refs/heads/main instead of ref: refs/heads/master. Alternatively, git checkout -b main.

git v2.28+

As @phd said, the -b/--initial-branch option was added in git v2.28. git 2.28 also introduces a config option to specify your preferred default branch:

git config --global init.defaultBranch main

Learn more about the new init.defaultBranch setting in GitHub's blog post.

16

In case you need to install the latest git version (in Ubuntu)

sudo add-apt-repository -y ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git -y

Ref: https://gist.github.com/YuMS/6d7639480b17523f6f01490f285da509

-2

This is happening due to the unavailability of a tool called Xcode Select, running xcode-select --install in your terminal fixes these issues with Git not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.