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

3

8

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?

Ritaritardando answered 26/11, 2020 at 10:55 Comment(2)
Yes, you can. Git uses master as the name of the default branch. Github decided to change it main recently. You can choose to pick either.Urine
biteinteractive.com/of-git-and-github-master-and-mainHypocycloid
D
9

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.
Defant answered 26/11, 2020 at 11:21 Comment(4)
It's worth noting that if you choose master, you'll need to go into the repository setting's and set it to be the "default branch" before you can delete main. I believe it will complain if you try to delete it while it's marked as the default.Snowbound
I've added an update on the end of the original question. Would appreciate further comments on that if possible?Ritaritardando
How does GitHub decided which branch names is the default? How does it react if there is a main and master branch in a repo? Which one is treated as default and shown on the projects landing page?Coverall
@Coverall there is an option in the repository's Settings -> Branches section, it's called literally Default branch - you need to click on "edit" and select a branch as default.Lorenelorens
C
1

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.

Calbert answered 26/11, 2020 at 11:4 Comment(4)
As a relative newbie, sticking to a single name would be preferable for now. 'main' sounds good. So do i make that change locally and push the change to github?Ritaritardando
@FeargalHogan yup, pretty much. You can rebase main on top of master to make sure you have all the commits you need, and them remove `master both locally and remotely (see https://mcmap.net/q/12217/-how-do-i-delete-a-git-branch-locally-and-remotely/2422776 for some extra details)Calbert
GitHub have made that change already; the primary authors of Git itself have not. There's a technical reason: a bunch of stuff that needs to be fixed in the test suite first. I try to avoid touching the politics of this myself. :-)Steato
Git's default branch is still called master. It is GitHub that made the change if you use their UI.Embosser
P
0

You must have put yourself in a complex mess of things doing this and that just to get out of this hinge, however, the more you do this the more you tend to end in more mess. Finding myself in such same situation, I had to do the following:

  1. Went to my Github account and deleted the whole repository (NOTE: you should still have the source code in your local machine), then I created a new one, copying the new link to the repo.

  2. Returned to my source code on my local device and changed the name of the default repository to main using the command - "git config --global init.defaultBranch main"

  3. Next, I updated the repo link on my remote code using the command - "git remote set-url origin [new repo url]"

  4. Then finally, i re-pushed the code to the newly created repo with a default of main with the command - "git push -f -u origin main"

This made all clean-ups intact with no other issue of navigating around like a headless chicken in correcting the issue. Take care.

Paduasoy answered 10/9, 2024 at 12:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.