gitgithub

I want to push my code to github on the main branch not on the master branch


I have a folder with code files. I wanted to push them to github using git. This is what I've done:

  1. I created a new repository on github.

  2. I initialized a git repo in my local directory.

  3. I staged my files.

  4. I made my initial commit.

  5. Then I run- git remote add origin https://github.com/Lamphrangmi-Lamin/Bootcamp-Survey.git

  6. It says on branch master but I wanted it to be on 'main' branch instead.

  7. After that I run this command- git push -u origin main

8.And it shows me this-

   error: src refspec main does not match any
   error: failed to push some refs to 'https://github.com/Lamphrangmi-Lamin/Bootcamp-Survey.git'

What I wanted was to have my files on the main branch.


Solution

  • You need to create main branch first:

    git checkout -b main
    git push -u origin main
    

    You can then delete the old master branch:

    git branch -d master
    

    For future, you can configure the name of git's default branch (assuming you are running version 2.28 or later):

    git config --global init.defaultBranch main
    

    You could also keep the branch named master in your local repo, but push to main on the remote side:

    git push -u origin master:main