Skip to content

git remote

Managing remote repositories with git remote — adding, renaming, and removing upstream connections.

What you'll learn

  • How to add, list, rename, and remove remotes
  • The origin and upstream convention
  • Changing a remote URL (HTTPS to SSH)
  • Viewing remote branches and tracking

Concept

What Is a Remote?

A remote is a named pointer to a repository hosted elsewhere — on GitHub, GitLab, a private server, or even another directory on your machine. Remotes let you push and pull changes between repositories.

git remote -v
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)

Each remote has a name. origin is the conventional name for the main remote, automatically created when you clone.

Adding a Remote

git remote add origin https://github.com/username/repo.git
git remote add upstream https://github.com/original-author/repo.git

The upstream remote is commonly used in the forking workflow: origin points to your fork, upstream points to the original repository.

Listing Remotes

git remote              # list remote names
git remote -v           # list with URLs (fetch and push)
git remote show origin  # detailed info about a remote

git remote show origin displays:

  • The URL
  • The remote branches
  • Which local branches track remote branches
  • Whether the remote is up to date

Renaming a Remote

git remote rename origin github

This changes the remote name from origin to github. All tracking references update automatically.

Changing a Remote URL

A common scenario: you set up a repo with HTTPS and want to switch to SSH (to avoid password prompts):

git remote set-url origin git@github.com:username/repo.git

Or switch from SSH back to HTTPS:

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

Removing a Remote

git remote remove origin

This removes the remote and all its tracking branches. Your local commits and branches are not affected.

Remote Branches

Remotes have branches too. After fetching, you can see them:

git fetch origin
git branch -r          # list remote branches
# origin/main
# origin/feature-login
# origin/develop

git branch -a          # list all branches (local + remote)

Remote branches are referenced as origin/main, origin/feature-login, etc. You cannot commit directly to them — you fetch, merge, and push.

Tracking Branches

When you clone, your local main tracks origin/main. You can set up tracking for other branches:

git branch --set-upstream-to=origin/develop develop
# or:
git checkout -b develop origin/develop

Tracking enables git push and git pull without specifying the remote and branch.

Multiple Remotes

You can have multiple remotes for different purposes:

git remote add origin    https://github.com/you/repo.git      # your fork
git remote add upstream  https://github.com/team/repo.git      # original
git remote add backup    https://github.com/you/repo-backup.git # backup

Push to different remotes as needed:

git push origin main      # push to your fork
git fetch upstream        # get changes from the original
git push backup main      # push to your backup

Example

              # --- Set up the forking workflow with two remotes ---
git clone https://github.com/YOUR-username/project.git
cd project

# origin is already set to your fork
git remote -v

# Add the original repo as 'upstream'
git remote add upstream https://github.com/ORIGINAL-username/project.git
git remote -v
# origin    https://github.com/YOUR-username/project.git (fetch)
# origin    https://github.com/YOUR-username/project.git (push)
# upstream  https://github.com/ORIGINAL-username/project.git (fetch)
# upstream  https://github.com/ORIGINAL-username/project.git (push)

# Get changes from the original repo
git fetch upstream
git branch -r  # see upstream's branches

# Merge upstream changes into your local main
git checkout main
git merge upstream/main
git push origin main  # push the updated main to your fork

# --- Change from HTTPS to SSH ---
git remote set-url origin git@github.com:YOUR-username/project.git
git remote -v  # verify the URL changed

# --- Detailed remote info ---
git remote show origin
# * remote origin
#   Fetch URL: git@github.com:YOUR-username/project.git
#   Push  URL: git@github.com:YOUR-username/project.git
#   HEAD branch: main
#   Remote branches:
#     main     tracked
#     develop  tracked
#   Local branches configured for 'git pull':
#     main merges with remote main

# --- Rename and remove ---
git remote rename origin myfork
git remote remove backup
            

Sets up the forking workflow with origin and upstream remotes, fetches and merges from upstream, switches from HTTPS to SSH, shows detailed remote info, and demonstrates renaming and removing remotes. The origin/upstream convention is standard for open-source contributions.

Try it

Common mistakes

The mistake

Confusing origin and upstream in the forking workflow

The fix

In the forking workflow: origin points to YOUR fork (where you push), upstream points to the ORIGINAL repo (where you fetch updates). You push to origin and fetch from upstream. Mixing them up means you either push to a repo you cannot write to, or fetch from your own fork instead of the original.

The mistake

Forgetting to fetch before checking out a remote branch

The fix

Remote branches only appear after you fetch. Run git fetch first, then git branch -r to see them, then git checkout -b local-name origin/remote-name to create a local tracking branch. If you try to checkout a remote branch that you have not fetched, Git will say it does not exist.

Related Snippets

Related Cheatsheets

Practice git remote

4 exercises

Practice git remote

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors