Remote Repositories: add, rename, remove, set-url, show and ls-remote
What a remote is and how to manage them: URLs and protocols, adding, listing, renaming and removing remotes, changing URLs and separate push URLs, remote-tracking branches and origin/HEAD, inspecting a remote with show and ls-remote, origin and upstream in a fork, and insteadOf URL rewriting.
kvi5hnuCode Sprout team member8 min readOn this page
So far everything has happened in one repository. Collaboration means exchanging commits with other repositories: a shared repository on GitHub, GitLab or Bitbucket, a company server, or a teammate's machine. Git calls each such repository a remote: a short name, such as origin, for a URL. This article covers managing remotes. The following articles cover cloning, fetching and pulling, and pushing.
In this course's sandbox there's no network, so the remote repositories are folders on the same machine. Git treats a local path exactly like a URL: every command works the same with https://github.com/....
Remotes are names for URLs
A remote is just configuration: a name, a URL, and rules for which branches to fetch. git remote add creates one, and git remote -v lists them:
git init -q --bare server/trail-guide.git # stands in for a repository on a hosting service
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git remote add origin ../server/trail-guide.git
echo "=== git remote, git remote -v"
git remote
git remote -v
echo "=== the configuration it wrote"
git config get --show-names --all --regexp '^remote\.'
git push -q -u origin main
echo "=== after pushing: a remote-tracking branch"
git branch -a
git branch -vv
=== git remote, git remote -v
origin
origin ../server/trail-guide.git (fetch)
origin ../server/trail-guide.git (push)
=== the configuration it wrote
remote.origin.url ../server/trail-guide.git
remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
=== after pushing: a remote-tracking branch
* main
remotes/origin/main
* main 7fb62a0 [origin/main] Start project
originis only a convention: the namegit clonegives the remote it cloned from. You can call remotes anything.-vshows the URL used for fetch and for push. They're the same unless you set a separate push URL.- The
fetchline is a refspec:+refs/heads/*:refs/remotes/origin/*means "copy every branch on the remote to a localorigin/<name>". Article 20 explains refspecs. - After the push,
remotes/origin/mainappeared. That's a remote-tracking branch: your repository's record of wheremainwas onoriginthe last time you talked to it.-ualso setorigin/mainasmain's upstream, whichgit branch -vvshows in brackets.
A bare repository, created with git init --bare, has no working tree: just the contents of .git. Servers and hosting services store repositories this way, because nobody edits files there directly. Pushing into a non-bare repository's checked-out branch is refused by default, since it would make that working tree inconsistent.
Remote-tracking branches
Remote-tracking branches, such as origin/main, are local, read-only references that Git updates whenever you fetch, pull or push. They let you compare your work with the remote without contacting it:
git log main..origin/mainlists commits on the remote you don't have yet. That's after a fetch.git statusreports "Your branch is ahead of 'origin/main' by 2 commits" by comparing with the upstream.- You can't commit on
origin/main.git switch origin/mainrefuses, andgit switch --detach origin/main(orgit checkout origin/main) gives you a detached HEAD. Create a local branch from it instead:git switch -c fix origin/main, or simplygit switch <name>whenorigin/<name>exists (article 19).
They're stored under refs/remotes/origin/, separate from your branches under refs/heads/, so main and origin/main never collide.
origin/HEAD
origin/HEAD records the remote's default branch, usually main. It lets you write origin to mean origin/main, as in git log origin. git clone sets it, and since Git 2.48, git fetch creates it if it's missing. If the default branch changes on the server, from master to main for example, update it with git remote set-head origin --auto, which asks the remote. The setting remote.origin.followRemoteHEAD (or fetch.followRemoteHEAD for all remotes, Git 2.56) can make git fetch warn about such changes, or follow them automatically.
URLs and protocols
| Form | Example | Notes |
|---|---|---|
| HTTPS | https://github.com/acme/trail-guide.git | works through most firewalls. Authenticates with a token, usually through a credential helper |
| SSH | git@github.com:acme/trail-guide.git | the user@host:path short form. Authenticates with an SSH key |
| SSH, URL form | ssh://git@github.com/acme/trail-guide.git | needed to give a port: ssh://git@host:2222/path.git |
| local path | /srv/git/trail-guide.git or ../server/trail-guide.git | another folder on the same machine or a network drive |
| file URL | file:///srv/git/trail-guide.git | like a local path, but copies data the normal network way instead of hard-linking files |
| git protocol | git://example.org/trail-guide.git | old, read-only and unauthenticated. Rarely used now |
The .git at the end is optional on most hosting services. Article 21 covers authenticating with HTTPS tokens and SSH keys.
Managing remotes
git init -q --bare server/trail-guide.git
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git remote add origin ../server/trail-guide.git
git push -q -u origin main
echo "=== rename: remote-tracking branches and upstreams follow"
git remote rename origin upstream
git branch -a
git branch -vv
echo "=== set-url: the repository moved"
git remote set-url upstream ../server/moved.git
git remote get-url upstream
echo "=== a separate push URL"
git remote set-url --push upstream ../server/push-here.git
git remote -v
echo "=== remove: its remote-tracking branches and upstream settings go too"
git remote remove upstream
git branch -a
git branch -vv
=== rename: remote-tracking branches and upstreams follow
* main
remotes/upstream/main
* main 7fb62a0 [upstream/main] Start project
=== set-url: the repository moved
../server/moved.git
=== a separate push URL
upstream ../server/moved.git (fetch)
upstream ../server/push-here.git (push)
=== remove: its remote-tracking branches and upstream settings go too
* main
* main 7fb62a0 Start project
| Command | Effect |
|---|---|
git remote add <name> <url> | add a remote. -f fetches from it immediately |
git remote rename <old> <new> | rename it, including its remote-tracking branches and every branch's upstream setting |
git remote remove <name> (or rm) | delete it, with its remote-tracking branches and upstream settings. The remote repository itself is untouched |
git remote set-url <name> <url> | change the URL, for example after a repository is renamed or moved, or to switch between HTTPS and SSH |
git remote set-url --push <name> <url> | push to a different URL from the one you fetch from |
git remote get-url <name> | print the URL. --push prints the push URL |
A repository renamed on GitHub keeps redirecting the old URL for a while, but update it with set-url anyway: redirects stop working once someone creates a new repository with the old name.
Inspecting a remote
git init -q --bare server/trail-guide.git
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git tag -a v1.0 -m "First release"
git switch -q -c lake; git commit -q --allow-empty -m "Start lake page"; git switch -q main
git remote add origin ../server/trail-guide.git
git push -q -u origin main lake --tags
echo "=== git remote show origin"
git remote show origin
echo "=== git ls-remote origin: every reference on the remote"
git ls-remote origin
echo "=== branches only, and tags without the ^{} lines"
git ls-remote --branches origin
git ls-remote --tags --refs origin
=== git remote show origin
* remote origin
Fetch URL: ../server/trail-guide.git
Push URL: ../server/trail-guide.git
HEAD branch: main
Remote branches:
lake tracked
main tracked
Local branches configured for 'git pull':
lake merges with remote lake
main merges with remote main
Local refs configured for 'git push':
lake pushes to lake (up to date)
main pushes to main (up to date)
=== git ls-remote origin: every reference on the remote
7fb62a00d1cf36c15a275d3c885e307de615b0e9 HEAD
0c2eed68b9ea3c4a8aa913976d209f4af059ced5 refs/heads/lake
7fb62a00d1cf36c15a275d3c885e307de615b0e9 refs/heads/main
341ed399ae2dd28d2f7e5c217dfafe9f7b18553e refs/tags/v1.0
7fb62a00d1cf36c15a275d3c885e307de615b0e9 refs/tags/v1.0^{}
=== branches only, and tags without the ^{} lines
0c2eed68b9ea3c4a8aa913976d209f4af059ced5 refs/heads/lake
7fb62a00d1cf36c15a275d3c885e307de615b0e9 refs/heads/main
341ed399ae2dd28d2f7e5c217dfafe9f7b18553e refs/tags/v1.0
git remote show origincontacts the remote and reports its URLs, default branch, branches and whether they're tracked, and how your local branches pull and push, including whether they're up to date.-nskips contacting the remote.git ls-remotelists the remote's references and the commit each points to, without fetching anything. It's useful for checking whether a branch or tag exists on the server, or in scripts. For an annotated tag, the extra^{}line shows the commit the tag points to.--refsleaves those lines out.--branches(--headsin older versions) and--tagsfilter, and a pattern narrows further:git ls-remote origin 'refs/tags/v1.*'.
Several remotes: forks and upstream
A repository can have any number of remotes. The common case is a fork: you copy someone else's project to your own account on a hosting service, clone your fork, and add the original project as a second remote, conventionally called upstream:
git clone git@github.com:sam/trail-guide.git # your fork: origin
cd trail-guide
git remote add upstream https://github.com/acme/trail-guide.git
git fetch upstream # get the original project's new work
git switch main
git merge --ff-only upstream/main # bring your main up to date
git push origin main # and your fork's main
You push feature branches to origin, your fork, and open pull requests to the original. You fetch from upstream to keep up with the project. Some people set the upstream remote's push URL to something invalid, such as git remote set-url --push upstream no-pushing, so they can never push there by accident. Article 22 covers forking workflows.
Other uses for several remotes: a mirror on a second hosting service, a deployment target, or a teammate's repository to fetch a branch from before it's pushed to the shared one. git fetch --all fetches from every remote.
Rewriting URLs: insteadOf
url.<base>.insteadOf rewrites URLs that start with a prefix, for every repository. Common uses are switching all GitHub HTTPS URLs to SSH, or defining a short alias:
git config --global url."git@github.com:".insteadOf "https://github.com/"
git config --global url."git@git.example.com:".insteadOf "work:" # git clone work:team/app.git
pushInsteadOf applies only to pushes, so you can fetch over HTTPS and push over SSH. git ls-remote --get-url <remote> shows the URL after rewriting, which helps when a remote doesn't connect where you expect.
What's current
git config get/set(Git 2.46) are the modern forms for reading and changing remote settings directly.git remotesubcommands are still the simplest way.- Since Git 2.48,
git fetchcreatesorigin/HEADwhen it's missing, andremote.<name>.followRemoteHEAD(withfetch.followRemoteHEADas a default since 2.56) controls whether fetch warns about or follows changes to the remote's default branch. git ls-remote --branchesis the newer spelling of--heads.- Hosting services increasingly require token or SSH authentication and reject account passwords over HTTPS (article 21).
Common questions
What's the difference between origin/main and origin main?
origin/main is a remote-tracking branch, a local reference to where main was on origin at your last fetch. origin main, two words, is a remote name and a branch name passed to a command, as in git push origin main or git pull origin main.
Does git remote remove delete the repository on GitHub?
No. It only deletes the remote's configuration and remote-tracking branches from your local repository.
How do I switch a remote from HTTPS to SSH?
git remote set-url origin git@github.com:acme/trail-guide.git. Check with git remote -v.
Why doesn't a new branch on the remote show up in git branch -a?
Remote-tracking branches only update when you fetch. Run git fetch, then look again. git ls-remote origin shows the remote's current branches directly.
Can a repository have no remote? Yes. Git works fully offline, and many repositories never have one. You add a remote when you want to share.
Summary
- A remote is a name for another repository's URL.
git remote -vlists remotes, andoriginis the conventional name for the one you cloned from. - Remote-tracking branches such as
origin/mainrecord where the remote's branches were at your last fetch, pull or push. They're read-only. git remote add,rename,remove,set-url(andset-url --push) andget-urlmanage remotes.git remote show <name>reports a remote's branches and tracking status, andgit ls-remote <name>lists its references without fetching.- In a fork,
originis your copy andupstreamthe original project. url.<base>.insteadOfrewrites URLs, for example from HTTPS to SSH.
Practice questions
You cloned a repository over HTTPS and now want to use SSH for it. Which command, and how do you check it?
Answer
git remote set-url origin git@github.com:acme/trail-guide.git
git remote -v
set-url changes both the fetch and push URL, unless a separate push URL is set.
A colleague says a tag v2.1 exists on the server, but git tag doesn't list it locally. How can you check the server without fetching?
Answer
git ls-remote --tags origin v2.1, or git ls-remote origin refs/tags/v2.1. It contacts the remote and lists matching references without downloading anything. git fetch --tags then brings it into your repository.
After git remote rename origin upstream, what happens to origin/main and to your main branch's upstream setting?
Answer
Both are updated: the remote-tracking branch becomes upstream/main, and main's upstream changes from origin/main to upstream/main. git branch -vv shows the new upstream.
Previous: Cherry-Picking Commits and Finding What's Missing Between Branches
Next: Cloning Repositories: Branches, Shallow, Partial and Sparse Clones

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.