Connecting Your Local Code to GitHub: Add vs. Set-URL

#tech

When working with Git and GitHub, one of the most common points of confusion is figuring out how to link your local folder to a remote repository—especially when Personal Access Tokens (PATs) are involved.

If you've ever found yourself wondering whether to use git remote add or git remote set-url, you are not alone. You actually hit the nail on the head: the choice entirely depends on whether Git already knows about your remote repository or not.

Let's break down exactly when to use each command, how to configure your local identity, and how to build these commands effortlessly.


1. The First-Time Connection: git remote add

You use git remote add when you have a brand-new local repository that has never been linked to a remote server (like GitHub) before.

Think of this as introducing two strangers. You are telling your local Git directory: "Hey, meet 'origin'. This is where I want you to send my code from now on."

The Command Structure

git remote add origin https://<YOUR_TOKEN>@github.com/aleh-skrypko/quest-log.git


2. The Quick Pivot: git remote set-url

You use git remote set-url when your local repository already has a remote connection, but you need to change the details.

The most common reasons to use this are:

  1. Your Personal Access Token expired, and you need to swap in a new one.
  2. You renamed your GitHub repository or moved it to a different account.

💡 A Quick Heads-Up: When using set-url, make sure you include the entire repository path at the end, not just the base GitHub URL. Git needs the full address to know exactly which project to update!

The Correct Command Structure

git remote set-url origin https://<YOUR_TOKEN>@github.com/aleh-skrypko/quest-log.git

If you only use https://<YOUR_TOKEN>@[github.com/](https://github.com/), Git will update the URL but won't know which repository to push to, resulting in an error when you try to upload your code.


3. Don't Forget Your Passport: Set Your Local Config

Before you push any code, GitHub needs to know who is authoring the commits. If you don't set your local config, Git might use your computer's default system username, making your GitHub contribution graph look empty.

Run these two commands inside your project terminal to set your identity locally:

git config --local user.name "Your Name"
git config --local user.email "[email protected]"

(Using --local ensures these settings apply specifically to this project without messing up your other repositories).


4. Step-by-Step Template Builder

To make things easy, here is a breakdown of how to construct your personal command, followed by a copy-paste template.

Steps to Build Your URL:

  1. Grab your Token: Generate a Personal Access Token (Classic or Fine-grained) from your GitHub Developer Settings.
  2. Get your Repo Path: Identify your username and repository name (e.g., aleh-skrypko/quest-log.git).
  3. Mash them together: Place your token right after https:// and before the @[github.com/](https://github.com/).

Interactive Templates

Simply copy the template you need and replace the placeholder values:

Scenario A: First time linking the project

# 1. Set your identity
git config --local user.name "YOUR_GITHUB_USERNAME"
git config --local user.email "YOUR_GITHUB_EMAIL"

# 2. Link the repository
git remote add origin https://<YOUR_TOKEN>@github.com/<USERNAME>/<REPO_NAME>.git

# 3. Push your code
git push -u origin main

git remote set-url origin https://<YOUR_TOKEN>@github.com/<USERNAME>/<REPO_NAME>.git

How to Verify It Worked

If you ever want to check what URL Git is currently tracking, run this command:

git remote -v

It will display the active URLs for both fetching and pushing, letting you confirm that your token and repository path are perfectly lined up!