How to push fresh code to GitHub?

Bylde
2 min readOct 28, 2021

Are you a beginner with GitHub and fascinated with its code collaboration features? Do you want to try it and don’t know where to start exactly?

Well, you can always go to GitHub and learn about its CLI or download a UI for your machine and start there.

But what if you already have some awesome code running on your local machine, and you would like it to be pushed to GitHub.

Let's just go to GitHub and create a repository:

Now go to your local folder where your code is located.

If you have recently created a nice project on your local environment, and you want to upload it to GitHub, follow this commands on your terminal, and you will be good to go.

Remember you have to first CD into your project folder

git init
git add .
git commit -m "initial commit"
git remote add origin https://github.com/harshalone/strapi-v4.git
git push -u origin master

If you face issues on GitHub, like history difference for example.

If the problem is “main and master are entirely different commit histories.”, the following will works

git checkout master   
git branch main master -f
git checkout main
git push origin main -f

For the ones who have problem to merge into main branch (Which is the new default one in Github) you can use the following:

git checkout master  
git branch main master -f
git checkout main
git push origin main -f

The following command will force both branches to have the same history:

git branch [Branch1] [Branch2] -f

--

--