简体   繁体   中英

How to deal with main and master in Github

I have opened a Git repository with readme file in Github, and then use below to push local code to this repository

git push origin master 

It shows two branchs: main and master, but with git branch, it only shows main branch

$ git branch
* main

As main branch is empty, I use below to force the commit to main

git push --force origin main

Now there are two same branches: main and master in the repository, but still with git branch, only show *main.

When I clicked the master, it shows:

This branch is even with main.

I use below to delete master, but the error as below:

$ git push main --delete origin/master
fatal: 'main' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

What should I do now to delete one of them?
And why the main not replaced by the master?
Later should I use git push origin main instead of master to push changes?
Thanks.

If you want to do this from the UI, you can go to the branches section of the repository and delete the branch you want to delete.

在此处输入图像描述

There is delete icon on right most side of a branch name.

在此处输入图像描述

If you want to do on git:

git push origin --delete master

The reason why you see only main in your local setup is, when you clone any repo only the default branch is cloned. For having other branch on git you have to fetch and pull the other branch.

For pushing changes after deleting master, you can use git push origin main to push changes in main branch.

This shows that
main is your master there is no other branches in your repo

$ git branch
* main

Try this if it works

git checkout master
git branch

You can delete a branch like this.
You must not be in that branch if are gonna delete it

git branch -D branch_name

Try this once it worked for me.

First

$ git branch -M main

It basically renames the master branch to main.

-M               move/rename a branch, even if target exists

Second

Force push the commit to main branch in remote repository.

$ git push -u -f origin main

===========================================================

This is output I got back.
$ git push -u -f origin main
Total 0 (delta 0), reused 0 (delta 0), pack-reused 0
To https://github.com/roxylius/gdfgdfgdf.git
 + 2926bd4...35f720a main -> main (forced update)
branch 'main' set up to track 'origin/main'.

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