My Github repo has 'main' and 'master' branches - what is their purpose?

Viewed 4831

I was trying to push some local unversioned code into a repo and get it up onto GitHub for accessibility reasons.

I followed the suggestions here

cd <local_dir>
git init
git add .
git commit -m "initial commit"

Then I created a new repo on github and did

git remote add origin https://github.com/...
git pull origin master --allow-unrelated-histories
git push --force-with-lease

But I now see on github that I have 2 branches 'main' and 'master' I guess github created 'main' when it created the repo and I created 'master' when I synced from my local repo

Do I need both?

Can I just merge master into main and then delete master?

At present it is just confusing me

Note: I have now tried (locally)

git branch -m master main
git push origin HEAD

but I'm getting an error

To https://github.com/<the-repo>
 ! [rejected]          HEAD -> main (non-fast-forward)
error: failed to push some refs to 'https://github.com/<the-repo>'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Whatt am I missing?

2 Answers

After events of 2020 in the USA, GitHub decided to rename the default Git branch main (details). Git and GitHub do not enforce any branch name, but a repository needs a default branch; how this branch is named is up to you.

As for your 2nd question, settling on main is fine. Before doing any changes, check on which branch your commits are :

  • if you made your commits on master, you'll have to merge master to main, then delete master
  • if you made your commits on main, there is nothing on master and and can delete it.

You can :

  • do this on your workstation and push changes to GitHub (my preferred choice)
  • work on GitHub and pull from there

EDIT to answer the extra question : Git is great in that it has fairly informative error messages with suggestions :

  • Updates were rejected because the tip of your current branch is behind its remote counterpart. This means the remote branch (on GitHub) has changes your local branch has not, which is why Git refuses to push.
  • Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. This is the typical solution.

Git doesn't need any specific branch.

Older versions of Git (and thus GitHub), used to create a "master" branch by default. In later versions (IIRC, around October 2020), the default branch was renamed to "main", in order to use more inclusive language.

From a technical perspective, you could keep both, decide on one, or even drop them both and decide your default branch is called SpongeBob.

Related