How to Add Origin in Git: Connect Your Local Repo to GitHub

When working with Git, “origin” is the default name for a remote repository—usually the one hosted on GitHub, GitLab, or Bitbucket. Adding an origin allows you to push and pull code between your local machine and the remote repo.

In this post, you’ll learn how to add a remote origin in Git, verify it, and start syncing your code with the cloud.


🔧 What Is “Origin” in Git?

  • origin is simply a reference name pointing to the remote repository.
  • It’s most commonly used to connect your local project with a GitHub repository.
  • Once set, you can push changes using simple commands like git push origin main.

✅ Step-by-Step: How to Add Origin in Git

🔹 Step 1: Create a GitHub Repository (If Needed)

Go to GitHub and create a new repository:

  • Click New repository
  • Name your repo
  • Choose public or private
  • Click Create repository

Don’t initialize it with a README if you’ve already got local files.


🔹 Step 2: Open Your Terminal or Git Bash

Navigate to your project folder:

cd path/to/your/project

Initialize Git (if you haven’t already):

git init

🔹 Step 3: Add the Remote Origin

Use the git remote add command with your GitHub repo URL:

git remote add origin https://github.com/your-username/your-repo.git

Replace the URL with the one for your repository. You can copy this from the “Clone” button on GitHub.


🔹 Step 4: Verify the Remote

Check that origin has been added successfully:

git remote -v

You should see something like:

origin  https://github.com/your-username/your-repo.git (fetch)
origin  https://github.com/your-username/your-repo.git (push)

🔹 Step 5: Push Code to Origin

If this is your first push:

git add .
git commit -m "Initial commit"
git push -u origin main

The -u flag sets origin/main as the default upstream so you can simply use git push next time.


🔄 Change or Update an Existing Origin

If you need to change the origin URL (e.g., you renamed your GitHub repo):

git remote set-url origin https://github.com/your-username/new-repo.git

🛠 Remove and Re-add Origin (If Needed)

To remove and add origin again:

git remote remove origin
git remote add origin https://github.com/your-username/your-repo.git

🧠 Summary of Common Commands

ActionCommand
Initialize Gitgit init
Add origingit remote add origin <repo-url>
View current origingit remote -v
Change origin URLgit remote set-url origin <new-url>
Remove origingit remote remove origin
Push to origingit push -u origin main

🏁 Conclusion

Adding an origin in Git is a key step in linking your local project to a remote repository like GitHub. Once connected, you can easily push and pull code, collaborate with others, and keep your project in sync across environments.

Sharing Is Caring:

Leave a Comment