Sub topic 1
What a Remote Is and Where origin Comes From
Prerequisites
This chapter assumes you can already do the following:
- Create commits with
git addandgit commit, and read history withgit log. - Create and switch branches.
- Describe in general terms what merging and rebasing do.
- Run commands in a terminal.
What a remote is
A remote is a named bookmark that points to another copy of your repository. That copy usually lives on a server such as GitHub, GitLab, or a company Git host. It can also be a folder on disk or another machine you reach over SSH.
Git is distributed. Your local repository holds the full history, and so does every other clone. The remote is not special to Git. It is a repository that your team agrees to treat as the shared meeting point.
A remote entry stores two things:
- A short name, such as
origin. - A URL that tells Git where the other repository is.
Git also stores rules, called refspecs, that say which remote branches map to which local references. You will see how those work in the next chapters.
Where origin comes from
origin is only a default name. When you run git clone, Git does three things:
- It copies the repository from the URL you gave.
- It saves that URL as a remote named
origin. - It creates remote-tracking branches such as
origin/main, and checks out a localmainthat tracksorigin/main.
You can change the name with git clone -o upstream <url>, but most projects keep origin. If you start with git init instead of cloning, you have no remotes at all until you add one.
Listing and changing remotes
The git remote command manages these entries. Use -v to see the URLs.
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 line does:
| Command | Effect |
|---|---|
| `git remote -v` | Lists each remote with its fetch and push URLs. |
| `git remote add <name> <url>` | Creates a new remote. |
| `git remote set-url <name> <url>` | Changes the URL, for example from HTTPS to SSH. |
| `git remote rename <old> <new>` | Renames the remote and its remote-tracking branches. |
| `git remote remove <name>` | Deletes the remote and its remote-tracking branches. |
The output of git remote -v looks like this:
origin [email protected]:team/project.git (fetch)
origin [email protected]:team/project.git (push)Fetch and push are listed separately because a remote can use different URLs for each. That is rare, but it is why you see two lines.
Common errors
fatal: remote origin already exists.You rangit remote add originon a repository that already has one. Usegit remote set-urlto change it.fatal: 'origin' does not appear to be a git repository. The remote is missing or the name is misspelled. Rungit remote -vto check what exists.- Renaming surprises. After
git remote rename origin upstream,origin/mainbecomesupstream/main. Scripts and branch settings that mentionoriginmust be updated. - Removing a remote with unpushed work.
git remote removedeletes only the bookmark and its remote-tracking branches. Your local commits stay, but you lose the link to the server.
Check your understanding
Exercise
You created a repository with git init and git remote -v prints nothing. What must you do before you can push to a server, and why did a cloned repository not need this step?
Show answer
Run git remote add origin <url> to record the server's URL under a name. A cloned repository already has an origin remote, because git clone saves the source URL under that name automatically.
Exercise
Your team moves its repository from HTTPS hosting to SSH access at a new URL. Which command updates your existing clone, and does it change any commits?
Show answer
Run git remote set-url origin <new-ssh-url>. It changes only the stored URL. Your commits and branches stay the same.