Git Remote

Updated · 10 min read
easyGit 2.53gitgit-remoteorigingithubinterview

Your phone keeps a contact list. You don't type a friend's full number every time you call. You save it once under a short name, "Advitha", and from then on you just say "call Advitha".

A remote is a saved contact for another copy of your repository, usually on a server such as GitHub, GitLab or Bitbucket. It is a short name, almost always origin, that stands for a long URL. Once it is saved, git push origin and git fetch origin know where to go. Up to now, everything you did stayed on your computer. Remotes are how your work reaches your team, and how theirs reaches you.

In this lesson, you will connect a local repository to a server, push to it for the first time, and manage the list of remotes.

A Stand-In for GitHub

This page can't create a real GitHub repository for you, so the examples use the next best thing: a bare repository in a folder next to the project. A bare repository is a repository with no working tree, only the .git content, which is exactly what GitHub, GitLab and Bitbucket store on their servers. Git talks to it with the same commands, and prints the same messages, as it does for a real server. Only the URL is a folder path instead of https://github.com/....

Terminal
git init --bare --quiet fee-tracker.git
ls fee-tracker.git
Output
HEAD
config
description
hooks
info
objects
refs

These are the same files you saw inside .git in "Inside the .git Folder". This folder now plays the part of the server.

Navika has been working on her own computer. Her repository has no remotes yet:

Terminal
mkdir fee-tracker
cd fee-tracker
git init --quiet
echo "Fee Tracker" > README.md
echo 'Term 1 fee: $12,000' > fees.txt
git add .
git commit --quiet -m "Add README and the term fee"
git remote
git remote -v
git log --oneline
Output
3976aa0 (HEAD -> main) Add README and the term fee

git remote and git remote -v print nothing, because there are no remotes. The only line is the one commit from git log.

Add a Remote

git remote add saves a name and a URL. The name origin is only a convention, but it is what everyone uses for "the main server copy":

Terminal
git remote add origin ../fee-tracker.git
git remote -v
Output
origin	../fee-tracker.git (fetch)
origin	../fee-tracker.git (push)

-v (verbose) shows the URL. It appears twice, because Git stores a URL for fetch (getting work) and for push (sending work). They are usually the same.

On a real server, the command looks like one of these, with the URL copied from the repository page:

URL styleExampleSigns in with
HTTPShttps://github.com/navika-anamaneni/fee-tracker.gitYour username and a personal access token, or a credential manager
SSHgit@github.com:navika-anamaneni/fee-tracker.gitAn SSH key you added to your account

Both reach the same repository. HTTPS works everywhere, including behind strict office firewalls. SSH, once its key is set up, doesn't ask for a password or token. The lessons on hosting platforms show how to set up each.

A remote is a name for another repository's address

Push for the First Time

The remote is saved, but the server is still empty. git push sends commits to it. The first time, -u also links the local main to the server's main, so later you can type just git push and git pull:

Terminal
git push -u origin main
Output
To ../fee-tracker.git
 * [new branch]      main -> main
branch 'main' set up to track 'origin/main'.
LineMeaning
To ../fee-tracker.gitThe remote the push went to
* [new branch] main -> mainThe local main created a new branch main on the server
branch 'main' set up to track 'origin/main'What -u did: main now has an upstream

In your terminal, a push also shows a few progress lines first, such as Enumerating objects and Writing objects. Git prints them only to a terminal, so they don't appear in the output on these pages.

Pushing has its own lesson, "Git Push", and upstreams have theirs, "Upstream and Remote-Tracking Branches".

See a Remote's Details

git remote show contacts the server and describes the connection:

Terminal
git remote show origin
Output
* remote origin
  Fetch URL: ../fee-tracker.git
  Push  URL: ../fee-tracker.git
  HEAD branch: main
  Remote branch:
    main tracked
  Local branch configured for 'git pull':
    main merges with remote main
  Local ref configured for 'git push':
    main pushes to main (up to date)

It tells you the URLs, the server's default branch (HEAD branch), which branches exist there, and how your local branches are connected to them for git pull and git push.

Rename, Change and Remove

Change the URL with set-url, for example when the repository moves, or when you switch from HTTPS to SSH:

Terminal
git remote set-url origin git@github.com:navika-anamaneni/fee-tracker.git
git remote -v
git remote set-url origin ../fee-tracker.git
Output
origin	git@github.com:navika-anamaneni/fee-tracker.git (fetch)
origin	git@github.com:navika-anamaneni/fee-tracker.git (push)

The last line sets it back to the stand-in folder, so the rest of the lesson keeps working.

Rename a remote with rename, and remove one with remove:

Terminal
git remote add backup ../fee-tracker-backup.git
git remote rename backup old-backup
git remote -v
git remote remove old-backup
git remote
Output
old-backup	../fee-tracker-backup.git (fetch)
old-backup	../fee-tracker-backup.git (push)
origin	../fee-tracker.git (fetch)
origin	../fee-tracker.git (push)
origin

Removing a remote only deletes the saved name and URL on your computer, along with your copies of its branches. It never deletes anything on the server.

Syntax

Terminal
git remote [-v]
git remote add <name> <url>
git remote set-url <name> <new-url>
git remote rename <old> <new>
git remote remove <name>
git remote show <name>
CommandIt is used to
git remoteList remote names
git remote -vList remotes with their URLs
git remote add <name> <url>Save a new remote
git remote set-url <name> <url>Change a remote's URL
git remote rename <old> <new>Rename a remote
git remote remove <name>Forget a remote (the server is not touched)
git remote show <name>Describe a remote and how branches are linked to it

Common Mistakes

error: remote origin already exists. You ran git remote add origin twice, or cloned a repository, which adds origin for you. Use git remote set-url origin <url> to change it.

Creating the server repository with a README, then pushing an existing project. The two histories have nothing in common, and the push is rejected. Create the server repository empty, or pull with --allow-unrelated-histories once to join them.

Typing the URL wrong. Git only finds out when it connects. git remote -v shows exactly what was saved. Copy the URL from the server's page instead of typing it.

Thinking git remote remove deletes the server repository. It only removes your local shortcut.

Putting a password or token inside the URL. It is then stored in plain text in .git/config. Use a credential manager or SSH keys, covered with hosting platforms.

Under the Hood

Remotes are stored in .git/config, in a section per remote:

Terminal
git config --get-regexp "^remote\."
git config --get-regexp "^branch\."
Output
remote.origin.url ../fee-tracker.git
remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
branch.main.remote origin
branch.main.merge refs/heads/main

remote.origin.url is the URL. remote.origin.fetch is a refspec, +refs/heads/*:refs/remotes/origin/*, which says "copy every branch on the server into my origin/ names". That is where origin/main comes from, as the lesson on remote-tracking branches explains. The branch.main.* lines were written by git push -u: they record that local main pulls from main on origin.

Interview Corner

What is a remote in Git?

A named reference to another copy of the repository, usually on a server, stored as a name and a URL (plus a fetch refspec) in .git/config. Commands like fetch, pull and push use the name instead of the URL.

What is origin?

The conventional name of the default remote. git clone creates it automatically, pointing to the URL you cloned from. It is not special to Git: it could be renamed or called anything.

How do you change the URL of a remote, for example from HTTPS to SSH?

git remote set-url origin <new-url>. Check the result with git remote -v.

What is the difference between HTTPS and SSH remote URLs?

They reach the same repository with different authentication. HTTPS uses a username with a personal access token or a credential manager, and works through most firewalls. SSH uses a key pair added to your account and never asks for a password once set up.

How do you connect an existing local project to a new GitHub repository?

Create an empty repository on GitHub, then run git remote add origin <url> and git push -u origin main in the project.

Practice

Challenge: a second remote. In the same project, a second empty server copy is needed for backups. Create a bare repository fee-tracker-backup.git next to the others, add it as a remote named backup, push main to it (without -u), and list the remotes with their URLs.

What does Git print? Before you open the solution, decide how many lines git remote -v prints, and whether the push says set up to track.

Show solution
Terminal
git init --bare --quiet ../fee-tracker-backup.git
git remote add backup ../fee-tracker-backup.git
git remote -v
git push backup main
Output
backup	../fee-tracker-backup.git (fetch)
backup	../fee-tracker-backup.git (push)
origin	../fee-tracker.git (fetch)
origin	../fee-tracker.git (push)
To ../fee-tracker-backup.git
 * [new branch]      main -> main

git remote -v prints four lines, a fetch and a push URL for each of the two remotes. The push created main on the backup server, but without -u there is no set up to track line: local main still pulls from origin.

Checkpoint. Create an empty repository on GitHub, GitLab or Bitbucket. Add it as origin to a project of your own with the HTTPS URL, push with -u, then switch the remote to the SSH URL with set-url and push again.

Quick check: Git remote

4 questions
1What is a remote?
2Which command saves a new remote called origin?
3Why does git remote -v show each URL twice?
4What does git remote remove origin delete?

Key Takeaways

Quick reference

CommandIt is used to
git remote -vList remotes and URLs
git remote add origin <url>Connect to a server repository
git push -u origin mainFirst push, linking main to origin/main
git remote show originShow details of a remote
git remote set-url origin <url>Change the URL
git remote rename <old> <new>Rename a remote
git remote remove <name>Remove a remote

Was this tutorial helpful?

Lesson 34 of 106