How to Push Existing Code to a new GitHub Repository



How to Push Existing Code to a new GitHub Repository

How to Push Existing Code to a new GitHub Repository

I get lots of developers asking me how to push existing code in an existing project to a remote GitHub repository.

There are two ways to push an existing project to a GitHub repo:

– The easy way
– The proper way

The easy way? Simply clone the existing GitHub repo, copy your project code into the clone repository, and then push back to the server. Your existing project will be added to the GitHub repo in the cloud.

This way works if there are existing files in the GitHub repo.

Alternatively, you can do a git init in your existing project, then create a new GitHub repository, and add the GitHub repo as a remote. Then you push.

A lot can go wrong in this approach. Often the branch name of main or master is incorrect and can cause a problem.

Furthermore, if there is code in the repo already, this will fail. You’ll need to force the Git push.

Personally, I think the easy way to add existing code to a GitHub repo is the right approach. Others say adding the remote reference is the right way to add a project to an existing GitHub server.

Let me know what you think in the comments!

Comments are closed.