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?

Feargal Hogan
  • 169
  • 1
  • 8
  • 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. – TheGeorgeous Nov 26 '20 at 11:03
  • https://www.biteinteractive.com/of-git-and-github-master-and-main/ – matt Feb 26 '22 at 14:19

3 Answers3

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.
Httqm
  • 799
  • 7
  • 13
  • 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. – IMSoP Nov 26 '20 at 11:30
  • I've added an update on the end of the original question. Would appreciate further comments on that if possible? – Feargal Hogan Nov 26 '20 at 12:48
  • 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? – buhtz Sep 27 '22 at 12:44
  • 1
    @buhtz 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. – Lukasz032 Apr 08 '23 at 23:42
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.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • 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? – Feargal Hogan Nov 26 '20 at 11:06
  • @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://stackoverflow.com/q/2003505/2422776 for some extra details) – Mureinik Nov 26 '20 at 11:10
  • 1
    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. :-) – torek Nov 26 '20 at 19:06
  • 1
    Git's default branch is still called master. It is GitHub that made the change if you use their UI. – qwr Sep 12 '22 at 19:24
0

Just do one thing were this main or master showing in the folder/repo goto the view section click on hidden file and you found one file called .git just delete it and your problem will solve