A remote is just a name for a URL. Inside .git/config each remote
has a section:
[remote "origin"]
url = git@github.com:foo/bar.git
fetch = +refs/heads/*:refs/remotes/origin/*
The git remote command reads and edits this list.
Core commands
git remote # list names
git remote -v # with URLs and directions
git remote show origin # detailed info: branches, tracking
git remote add upstream <url> # add a remote
git remote rename origin foo # rename
git remote remove upstream # remove
git remote set-url origin <new> # change URL without re-cloning
Typical output of git remote -v:
origin git@github.com:me/bar.git (fetch)
origin git@github.com:me/bar.git (push)
upstream git@github.com:original/bar.git (fetch)
upstream git@github.com:original/bar.git (push)
The two directions (fetch and push) can point to different URLs (rare case: pull from one, push to another). Usually they are the same.
Why "origin"
Just a name. Git defaults to cloning from origin because that is
the convention. You can rename it:
git remote rename origin upstream
In the fork scenario (see fork-vs-clone):
originis your fork (me/repo).upstreamis the original repository (original/repo).
More detail in upstream-vs-origin.
Change URL without re-cloning
Scenario: the repository moved from GitHub to GitLab. No need to clone again:
git remote set-url origin git@gitlab.com:foo/bar.git
git fetch
Everything continues to work, history intact.
Separate push URL and fetch URL
Sometimes you need to pull from a public mirror but push to a private one:
git remote set-url origin https://github.com/foo/bar.git
git remote set-url --push origin git@gitlab.com:foo/bar.git
Now git fetch goes to GitHub and git push goes to GitLab. Rare,
but useful during migrations.
Pitfalls
git remote adddoes NOT fetch automatically. After adding, you needgit fetch <name>, otherwise the new remote's branches will not appear.- The URL is stored in
.git/configin plain text. If the URL contains a password (https://user:password@...) it sits there unencrypted. Use SSH or a credential helper instead. - Remote names are local. Your
upstreammay point to one place, your colleague's to another. That is normal, but when talking in a team, refer to the URL, not the name.