How to git push an existing project to GitHub

Add an existing project to GitHub steps

To push a new project to an existing GitHub repository, follow these steps:

  1. Create a GitHub repository for the existing project.
  2. Copy the GitHub URL for the new repo to the clipboard.
  3. Perform a git init command in the root folder of the existing project.
  4. Add all of the existing project’s files to the Git index and then commit.
  5. Add the GitHub repo as a remote reference for the existing project.
  6. Perform a git push operation with the -u and -f switches.
  7. Verify that the existing project’s files have been pushed to GitHub.

GitHub push tutorial commands

Many DevOps professionals only want to know the Git commands needed to push their existing project to GitHub. To save those readers from going through the entire example, here are the Git commands used in this tutorial. These commands assume a push to a GitHub repo named existing-website, owned by a GitHub user named cameronmcnz:

  • git init
  • git add .
  • git commit -m "Add existing project files to Git"
  • git remote add origin https://github.com/cameronmcnz/example-website.git
  • git push -u -f origin master

The remote GitHub repository

To push an existing project to GitHub, you must first create a GitHub repository. To do this, simply click the green “Create repository” button in GitHub’s online console and provide a repository name.

For this example, we will name the repository example-website.

You must create a GitHub repo to manage your existing project’s files.

Obtain the repo’s GitHub URL

After you create the repository, click the green “Code” button. This reveals the GitHub URL for use over HTTPS connections.

Copy this value for use in a future step.

The GitHub URL is used to push the existing project to GitHub.

Initialize Git in the existing project

If the existing project does not already use Git, issue a git init command in the root folder. After the repository is initialized, add all of the project files to the Git index and perform a commit:

git add .
git commit -m "Add existing project files prior to the push to GitHub."

Add a remote reference for GitHub

To allow your existing project to synchronize with GitHub, issue a git remote add command to configure a reference from you local Git installation to the repository on GitHub. Note that the last segment of the git remote add command is your project’s GitHub URL.

git remote add origin https://github.com/cameronmcnz/example-website.git

Push the first commit to GitHub

With the remote reference added, you are ready to push your existing project to GitHub.

Simply issue a git push command with the name of the current branch along with the -u and -f switches.

Note that older Git repositories create a master branch by default, while newer ones use main. Amend the git push command accordingly.

git push -u -f origin master

The -u switch makes the remote GitHub repo the default for your existing project. The -f switch forces Git to overwrite any files that already exist on GitHub with your existing project’s files.

Verify the GitHub push

To verify that the existing project was pushed to GitHub successfully, log into the GitHub website and browse the repository. All of the files from your existing project should be visible on GitHub’s.

And that’s how easy it is to push an existing project to a GitHub repository.

 

App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close