简体   繁体   中英

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.

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
  1. I created new repository on github.

When you do this with the GitHub web interface, you can choose to create a repository that has an initial commit, or one that has no commits at all. If you choose to make no commits at all yet, this repository will also have no branches (this is probably the setup you chose and is likely the setup you should have chosen).

  1. Then I initialized my directory where all the files are.

I assume you mean you ran git init on your laptop or other (local) computer here. That creates a new, totally empty repository. In such a repository, no commits exist.

For a branch to exist, the branch name must identify a commit hash ID. Since there are no commits (yet), there are no branch names. This puts this new repository in the same kind of situation as the GitHub repository. This is all fine.

Note, however, that despite the fact that there are no branches , you are still on a branch . You are simply on a branch that does not exist . You can choose, or change, the name of this branch at this time, using git checkout -b —or, assuming your local Git version is at least 2.23, git switch -c .

If you do nothing at this time, the name of the branch that you are on—the branch that does not exist—is whatever is built in to your Git as the default: probably master . This is not a problem; it's just something to keep in mind.

  1. Then I staged all my files I wanted.
  2. Then I made my initial commit with a commit message.

This is also all fine: making that first commit causes the name of the branch to spring into being. Whatever nonexistent branch you were on, you are still on, but now it exists. The branch name now identifies this first commit. The branch now exists, and its name now matters—but its name can be changed any time you like.

  1. Then I run [ git remote add ...]

This is also fine: it creates the named remote, pairing it with the supplied URL. Nothing else happens at this point. No branches are created or destroyed, for instance.

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

I assume you mean that git status says that. That's because the nonexistent branch you had until step 4 was named master , so the name that was created in step 4 was master .

The easiest thing to do now is rename master to main :

git branch -m main
  1. After that I run this command- git push -u origin main

Since the name main does not exist in your own Git repository yet, this gives you an error message: the rather cryptic-looking src refspec main does not match any . That is because you supplied a refspec (namely main ) that expanded to main:main , which means send my commits to origin , and ask them to create or update their main , based on my existing main . As you do not have an existing main , you got this error.

If you rename your existing master to main , the refspec you supplied— main:main , abbreviated as just main —will now match your one existing branch named main , and will work.

If you create a second branch named main , the git push will also work. You will now have two local branches, one named master and one named main . You should then switch to the main name and delete the master name that you presumably do not want.

If you created a commit on GitHub, this will all fail

If you get an error that your git push -u origin main is not a fast-forward , this means that in step 1, you created your GitHub repository with some initial commit, which created a main branch there on GitHub already. You must now decide whether that initial commit on GitHub has any value.

If that initial commit has no value, use git push -f origin main to forcibly discard the initial GitHub commit (and any commits anyone else has added, if anyone else has access to this repository) in favor of your own initial commit.

If it does have value, you'll need to somehow combine your own initial commit with GitHub's initial commit. That will be a subject for a separate question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM