Part V: Chapter 5.7

git remote

Adding, renaming, and removing remotes.

A remote is nothing more than a named entry in .git/config: a short name like origin paired with a URL and a refspec. You can have as many remotes as you need — forked repos, upstream projects, staging servers, or mirrors all work the same way.

Manage Your Remotes

Add a remote, rename it, or remove it. Watch .git/config update in real time on the right.

Configured remotes
originhttps://github.com/alice/myrepo.git
.git/config (relevant sections)
[remote "origin"]
url = https://github.com/alice/myrepo.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "main"]
remote = origin
merge = refs/heads/main
command log
$ git remote -v
origin https://github.com/alice/myrepo.git (fetch)
origin https://github.com/alice/myrepo.git (push)

The fork workflow

The most common reason to have two remotes is the open-source fork workflow. You have your own fork at origin and the authoritative upstream project at upstream.

You push to origin (your fork) and open pull requests from there. You fetch from upstream to keep your fork in sync with the project.

git remote -v
origin https://github.com/you/repo.git (fetch)
origin https://github.com/you/repo.git (push)
upstream https://github.com/org/repo.git (fetch)
upstream https://github.com/org/repo.git (push)

What the refspec does

The refspec in .git/config tells Git how to map remote branch names to local remote-tracking branch names:

[remote "origin"]
url = https://github.com/alice/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*

The + means force-update (allow non-fast-forward updates to the remote-tracking refs). The * globs map every remote branch to a local origin/branch tracking ref. You can add custom refspecs to fetch only specific branches.

git remote commands

Terminal
$# List all remotes with URLs:
$git remote -v
origin https://github.com/you/repo.git (fetch)
origin https://github.com/you/repo.git (push)
$# Add a new remote:
$git remote add upstream https://github.com/org/repo.git
$# Rename a remote:
$git remote rename upstream source
$# Change a remote's URL:
$git remote set-url origin git@github.com:you/repo.git
$# Remove a remote:
$git remote remove source
$# Show detailed info about a remote:
$git remote show origin
* remote origin
Fetch URL: https://github.com/you/repo.git
HEAD branch: main
Remote branch: main tracked
Local branch configured for 'git pull': main merges with remote main