How to Set Origin in Git: A Step-by-Step Guide

When working with Git and GitHub (or any remote Git server), one of the most common tasks you’ll perform is linking your local repository to a remote one. In Git terminology, this remote URL is often referred to as origin.

In this blog post, we’ll explore what origin is, how to set it, and how to update or verify it. Whether you’re starting a new project or cloning an existing one, understanding how to manage origin is a key part of your Git workflow.


🔹 What is origin in Git?

origin is the default name Git uses for a remote repository. When you clone a repository from GitHub or another Git host, Git automatically names that remote origin. This alias makes it easier to refer to the remote URL when you push or pull code.

You can also manually set or change the origin for a repository — and that’s what we’ll cover here.


✅ How to Set Origin in Git

If you’ve created a local Git repository (e.g., with git init) and want to link it to a remote repository, follow these steps:

Step 1: Create a Repository on GitHub (or Another Git Host)

  1. Go to GitHub and create a new repository.
  2. Don’t initialize it with a README if you already have files locally.

Step 2: Add the Remote Origin

Open your terminal, navigate to your local repo folder, and run:

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

Replace the URL with the correct HTTPS or SSH path to your GitHub repository.


Step 3: Push Your Local Code to Origin

If you’ve committed changes locally, push them to the remote:

git push -u origin main

💡 If your branch is called master, replace main with master.

The -u flag sets the upstream so you can use git push and git pull in the future without specifying the remote.


🔁 How to Change or Update Origin

If you made a mistake or need to switch the remote URL:

Check Your Current Remote

git remote -v

This shows the current remote(s) and their URLs.

Change the Origin URL

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

Now your local repo will push/pull from the new location.


🗑️ How to Remove and Re-add Origin

To remove the origin entirely:

git remote remove origin

Then you can re-add it as shown earlier.


📌 Summary of Common Commands

TaskCommand Example
Add origingit remote add origin https://github.com/user/repo.git
View current remotesgit remote -v
Change origin URLgit remote set-url origin https://new-url.git
Remove origingit remote remove origin
Push to origin (first time)git push -u origin main

Final Thoughts

Setting and managing the origin remote is a fundamental part of working with Git. Once it’s configured correctly, you’ll be able to seamlessly push your code to the cloud, collaborate with others, and manage your project more effectively.

Whether you’re just getting started or troubleshooting a remote URL issue, understanding how origin works will serve you well throughout your Git journey.

Sharing Is Caring:

Leave a Comment