Git main/ master ?

Alfred Wang
3 min readMay 10, 2021

My little blog so I can refer to myself when this happens

So recently I have this problem where I git pushes to master branch, not main and cant merge the two.

Go to GitHub, create a repository.

Go to my folder and run git init,git add . ,git commit -m "first commit",

git remote add origin my@repository,

and git push -u origin master Now I know, I need to create a main branch, so I also run git checkout -b main which runs successfully, but then when I run git push --set-upstream origin main it errors out.

So, I thought I have to always create a readme along with the GitHub repo, however, I can also choose to let it me empty with out the default read me.

So with no branches at all, everything is up to me at the LOCAL end.

git init

git add .

git commit -m “first commit”

Now my default branch is master. This is the point after your first initial commit, I can choose if I want to keep that or change it to main.

If the branch itself I want to keep it the same, I can simply add the remote and push

git remote add origin my@repo

git push -u origin master

Now master will be my primary branch, so I can keep using it as the primary branch.

On the other hand if i want main as primary branch

I should be doing

git remote add origin my@repo

git branch -M main

git push -u origin main

When ever I go to GitHub and create a new repo with readme.md , it does have an initial branch, and it is called main.

So i should decide whether to start locally with a empty copy of the same GitHub repo, start locally with a repo that I may already have.

I made the mistake of pushing origin main and thus created a main branch and master branch, which all my commits are actually not visible to GitHub main page sqaures.

Since I want to keep the branch “main” as the actual main, the solution is this

git branch -M main

git pull origin main — allow-unrelated

git push -u origin main

This way it folded my remote repo and local repo together with the same branch and the same contents.

While running into git issues, I also google about what the difference is between fork and clone.

Fork is done on github account, cloning is done using Git.

When I fork a repo, I create copy of the original repo and it remains on my github account, but if I clone a repo, it is actualy in my local.

Changes made to the forked repo can be merged with the original repo through — pull request. Pull tells the owner and let them know “here are the changes I’ve made”, and it will show different color for the add/deleted files.

On the other hand, changes made to the local (cloned) can be pushed to the upstream repo, however, to push you must have the write access. If user doesn't have write access, then we have t go through a fork request.

https://www.toolsqa.com/git/difference-between-git-clone-and-git-fork/#:~:text=Forking%20is%20a%20concept%20while,files%20to%20the%20local%20machine.

https://www.toolsqa.com/git/git-delete-tag/

--

--