Techtrekking

How to publish local repository to Github

By Pravin

Please Follow below steps to push local repository to github.

Step#1.

Create new empty repository on github https://github.com/new Please make sure you do not select Initialize this repository with a README checkbox. This is to ensure we dont get error while pushing local changes to github

Step#2.

Create local repository.

echo "# any comment you want to add" >> README.md git init git add . git commit -m "first commit" git branch -M main git remote add origin https://github.com/XXX-your-username-XXX/XXX-your-repo-name-XXX.git git push -u origin main

Starting October 1, all new GitHub repositories will create a default branch named main, and GitHub will no longer create a master branch for you.

What if I had added readme.md file while creating repository.

In this case, you will get following error

To https://github.com/cyour-username-/your-repo-name.git ! [rejected] main -> main (fetch first) error: failed to push some refs to 'https://github.com/your-username-/your-repo-name.git' hint: Updates were rejected because the remote contains work that you do hint: not have locally. This is usually caused by another repository pushing hint: to the same ref. You may want to first integrate the remote changes hint: (e.g., 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

To override github repository you can use --force parameter

git push origin main --force
Comments
No comments yet. Be the first to comment!
Leave a Comment
Your comment will be visible after approval.