Sub topic 2
Listing and configuring remotes
This chapter assumes you can commit, create and switch branches, and run Git commands in a terminal. You do not need to know how fetch, pull, or push work yet. Later chapters cover them.
What a remote entry holds
A remote is a short name that points to another copy of your repository, usually on a hosting service or a shared server. Git stores each remote as a name plus a URL. The name saves you from typing the full URL in every command.
Most remotes are called origin. Git uses that name automatically when you run git clone, and it records the URL you cloned from. The name has no special meaning beyond that. You can rename it, and a repository can have several remotes.
Listing remotes
Run git remote to print only the names. Add -v to see the URLs too:
git remote -vA typical result looks like this:
origin [email protected]:team/project.git (fetch)
origin [email protected]:team/project.git (push)Each remote appears twice because Git can use one URL for fetching and another for pushing. Usually the two are identical. To see more detail about one remote, including its branches and whether your local branches are tracking them, run git remote show origin. That command contacts the server, so it needs network access.
Adding, changing, renaming, and removing
These commands cover the whole life of a remote entry:
git remote -v
git remote add origin https://example.com/team/project.git
git remote set-url origin [email protected]:team/project.git
git remote rename origin upstream
git remote remove upstreamHere is what each one does:
git remote add <name> <url>creates the entry. Use it when you started withgit init, or when you want a second remote, such as the original project you forked from.git remote set-url <name> <url>changes the URL and keeps the name. Use it when a project moves or when you switch between HTTPS and SSH.git remote rename <old> <new>changes the name. It also renames the matching remote-tracking branches and updates your branch configuration, so you do not have to repair anything by hand.git remote remove <name>deletes the entry. It also deletes the remote-tracking branches and configuration that belong to it. Your local branches and commits are not touched.
The two URL styles in the example are the common ones. An https:// URL and an scp-style SSH URL (git@host:path) can point to the same repository. Chapter 3 covers how each one authenticates.
Where the configuration lives
Git keeps these entries in .git/config. After git remote add, the file contains a section like this:
[remote "origin"]
url = https://example.com/team/project.git
fetch = +refs/heads/*:refs/remotes/origin/*The fetch line is a refspec. It tells Git to copy each branch from the remote's refs/heads/ into your refs/remotes/origin/. That mapping is why you see names such as origin/main, which the next chapters explain. You can edit the file by hand, but the git remote commands are safer because they keep related settings consistent.
Common mistakes
- Adding a name that exists.
git remote add origin ...fails witherror: remote origin already exists.Useset-urlto change the address. - Mistyping a URL.
git remote adddoes not check the URL. The error appears later, on your first fetch or push. Rungit remote -vafter adding a remote to check what you typed. - Expecting
removeto delete the server copy. It only removes your local reference to the remote.
Exercise
A repository has an origin remote with an HTTPS URL. You want to use SSH instead, without losing any branches or history. Which command do you run, and why is git remote remove origin followed by git remote add origin ... a worse choice?
Show answer
Run git remote set-url origin [email protected]:team/project.git. It changes only the URL. Removing the remote also deletes its remote-tracking branches and the branch configuration that depends on it, so you would have to rebuild that state.