Pushing Existing Repository to GitHub

Let’s say that you have a local Git repo that you have been using to manage your code for a while. Now, you wish to publish it to  GitHub so others can look at it and participate in development. How will you do that?

Well, the key to pushing to any remote repo for the first time is to make sure that the remote repo is completely empty. Normally, you will use “git init –bare” to create a bare/empty repo on the remote server. In GitHub, you use the web GUI to create a repo. There, make sure that you do not choose Initialize this repository with a README checkbox. This will create a bare repo in GitHub.

After the repo is created in GitHub, run these commands from your local Git repository. Let’s say that the URL for the newly created GitHub repo is “https://github.com/bobby/MyRepo.git”.

git remote add MyGitHub https://github.com/bobby/MyRepo.git
git push MyGitHub master

Then, enter your GitHub user ID and password.

So, the trick is not to create the README file when you create the repository in GitHub. Otherwise, the repository will not be bare and when you try to do a push, you will get an error message “Updates were rejected because the tip of your current branch is behind”.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.