Managing Remotes
A remote is just a saved nickname for another copy of the repository, usually on a server. Ravi's clone got one automatically. Asha started with git init, so her repository knows about no remotes until she adds one. Either way, a handful of commands manage the list.
The rule
A remote is a nickname for a URL; add, list, rename, re-point or remove it.
git remote -v # list remotes and their URLs
git remote add origin https://github.com/asha-dev/recipe-box.git
git remote set-url origin git@github.com:asha-dev/recipe-box.git # new URL
git remote rename origin github # change the nickname
git remote remove github # forget a remoteKey facts:
originis only a convention. It is the name Git gives the place you cloned from, and the name most people choose when adding their main remote. Nothing about it is special.- A repository can have many remotes. When you contribute to someone else's project, it is common to have
originfor your own copy andupstreamfor the original. - Removing a remote deletes nothing on the server. It only removes the nickname, and the remote-tracking branches, from your local repository.
git remote -vshows two lines per remote, one marked(fetch)and one(push). They are normally the same URL.
Remotes also create remote-tracking branches such as origin/main. These are your repository's memory of where each branch was on the remote the last time you talked to it. They update only when you fetch, pull or push, never on their own.
Inspect one remote in detail with:
git remote show originIt lists the remote's branches and which of your local branches push to and pull from them.
The shortcut
Treat remotes like contacts in a phone: one name, one address. Wrong address? set-url. Wrong name? rename. Not needed? remove. Unsure? git remote -v and look.
Nothing yet
Asha's repository began with
git init, sogit remote -vprints nothing.
A common error is remote origin already exists, which appears when you run git remote add origin twice. You do not need to add it again; use set-url to change the address.
Drills
Answer fast — trust the rule.
Which command lists your remotes with their URLs?
git remote add origin <url> fails with "remote origin already exists". Asha wants the new URL. What should she run?
What happens on GitHub when Asha runs git remote remove origin?
What is origin/main?
Remember
- A remote is a nickname for a URL; add, list, rename, re-point or remove it.
originis a convention, not magic;upstreamis common for an original project.- Removing a remote changes nothing on the server.
