So you’ve been coding locally, and now you’re ready to share your project with the world (or just back it up securely) by pushing it to GitHub. This is a fundamental task for any developer using Git.

This guide walks you through the steps to connect your local Git repository to a brand-new repository on GitHub using the command line.

Prerequisites

Before you begin, make sure you have:

  1. A GitHub account
  2. Git installed on your local machine
  3. Your project folder ready, which is already a local Git repository (i.e., you have already run git init and made at least one git commit).

Step 1: Create a New GitHub Repository

First, you need a destination for your code on GitHub.

  1. Go to GitHub and log in.
  2. In the upper-right corner, click the $+$ sign and select New repository.
  3. Fill in the details:
    • Repository name: Give it the same name as your local project folder for consistency.
    • Description (Optional): Add a brief description.
    • Visibility: Choose Public or Private.
    • Crucially: Do not check the box to initialize the repository with a README, .gitignore, or license. Since you are pushing an existing project, an empty repository works best.
  4. Click Create repository.

After creation, GitHub will display a “Quick Setup” page with instructions. Look for the section titled “…or push an existing repository from the command line.”

Step 2: Connect Your Local Repository to GitHub

Now, open your terminal (Git Bash, Command Prompt, or your preferred terminal) and navigate to the root directory of your local project.

Bash

cd /path/to/your/local/project

2.1 Add the Remote Origin

The git remote add origin command tells your local Git where the remote (GitHub) version of your project lives.

Copy the HTTPS or SSH URL from the GitHub Quick Setup page (e.g., https://github.com/USERNAME/REPO-NAME.git) and run:

Bash

git remote add origin YOUR_REMOTE_URL
ex:
git remote add origin https://github.com/USERNAME/REPO-NAME.git

Replace YOUR_REMOTE_URL with the URL you copied.

You can verify that the remote was added correctly:

Bash

git remote -v

This should output the fetch and push URLs for your new origin.

2.2 Rename the Main Branch (If Necessary)

Older local repositories might still use master as the default branch name. GitHub’s current standard is main. It’s good practice to align them.

To rename your local branch to main:

Bash

git branch -M main

Step 3: Push Your Code to GitHub

This is the final step where you upload your local commits to the remote repository.

Use the git push command with the following flags:

Bash

git push -u origin main
  • git push: Executes the push operation.
  • -u: Sets the upstream branch. This means Git remembers that your local main branch should track the remote origin/main branch, allowing you to use simpler git push and git pull commands later.
  • origin main: Specifies that you are pushing the local main branch to the remote named origin.

You may be prompted to enter your GitHub username and Personal Access Token (PAT).

Success!

Once the push is complete, refresh the GitHub repository page in your browser. You will see all your project files and commit history!

You have successfully uploaded your existing local Git project and created a new GitHub repository! Happy coding!

By Ray Lee (System Analyst)

iDempeire ERP Contributor, 經濟部中小企業處財務管理顧問 李寶瑞

Leave a Reply

Your email address will not be published. Required fields are marked *