Git Remote

A remote is a version of a repository stored on a server — usually GitHub, GitLab, or Bitbucket. While the local repository lives on a personal computer, the remote repository lives on the internet and acts as a shared hub for all collaborators.

The git remote command manages these connections. It does not transfer any files — it simply stores the address (URL) of the remote repository so that commands like git push and git pull know where to send or receive data.

Real-life analogy: A remote is like saving a friend's phone number. Saving the number does not make a call — it just stores the address so a call can be made later when needed.

Viewing Current Remotes

git remote

Shows the names of all configured remotes. Typically outputs:

origin

For more details including the URL:

git remote -v

Output:

origin  https://github.com/ravikumar/my-website.git (fetch)
origin  https://github.com/ravikumar/my-website.git (push)

The (fetch) URL is where commits are downloaded from, and the (push) URL is where commits are uploaded to. Usually they are the same.

Adding a Remote

To link a local repository to a GitHub repository for the first time:

git remote add origin https://github.com/username/repository-name.git
  • origin is the default name given to the remote. It can technically be any name, but origin is the universal convention.
  • Replace the URL with the actual GitHub repository URL

Complete Example: Connecting a Local Repo to GitHub

# Step 1: Initialize a local repository
mkdir my-portfolio
cd my-portfolio
git init

# Step 2: Create a file and commit it
echo "# My Portfolio" > README.md
git add README.md
git commit -m "Initial commit"

# Step 3: Add the remote (GitHub repo must already exist)
git remote add origin https://github.com/ravikumar/my-portfolio.git

# Step 4: Confirm the remote was added
git remote -v
# Output:
# origin  https://github.com/ravikumar/my-portfolio.git (fetch)
# origin  https://github.com/ravikumar/my-portfolio.git (push)

# Step 5: Push to GitHub
git push -u origin main

Changing a Remote URL

If the GitHub repository was renamed or moved, the remote URL needs to be updated:

git remote set-url origin https://github.com/ravikumar/new-name.git

Verify the change:

git remote -v

Renaming a Remote

To rename a remote from origin to something else:

git remote rename origin upstream

Removing a Remote

To disconnect a remote from a local repository:

git remote remove origin

This does not delete the remote repository on GitHub — it only removes the connection from the local machine.

Working with Multiple Remotes

A local repository can be connected to more than one remote. This is common in open-source contribution workflows where there is both the personal fork (origin) and the original project (upstream).

# Add personal fork as origin
git remote add origin https://github.com/myusername/project.git

# Add the original project as upstream
git remote add upstream https://github.com/originalauthor/project.git

# View both
git remote -v
# Output:
# origin    https://github.com/myusername/project.git (fetch)
# origin    https://github.com/myusername/project.git (push)
# upstream  https://github.com/originalauthor/project.git (fetch)
# upstream  https://github.com/originalauthor/project.git (push)

With this setup:

  • git push origin sends code to the personal fork
  • git pull upstream downloads the latest changes from the original project

HTTPS vs SSH URLs

GitHub supports two URL formats for remotes:

TypeExample URLAuthentication
HTTPShttps://github.com/user/repo.gitUsername and password (or personal access token)
SSHgit@github.com:user/repo.gitSSH key pair (more secure, no password needed each time)

For beginners, HTTPS is simpler to set up. SSH is preferred for daily development work as it does not require entering a password with every push or pull.

Summary

The git remote command manages connections between local and remote repositories. The default remote is named origin. Use git remote add origin <url> to connect a local repo to GitHub for the first time. After adding a remote, git push and git pull can be used to sync code between the local machine and GitHub.

Leave a Comment

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