Sub topic 12
Inspecting, Renaming, Changing, and Removing Remotes
This chapter assumes you have a local repository with at least one commit, an origin remote already added, and a basic idea of what a remote is. You should also know the difference between HTTPS and SSH remote URLs. Here you will learn the commands that manage remotes after the first setup.
What a remote stores
A remote is a name stored in your repository's configuration, paired with one or more URLs. When you run git remote add, Git writes a [remote "name"] section into .git/config. Git also keeps remote-tracking branches such as origin/main. These are local references that record where the remote's branches were the last time you fetched or pushed.
Inspecting remotes
List the names of your remotes:
git remoteAdd -v to see the fetch and push URL for each one:
git remote -vThe output looks like this:
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (fetch)
origin [email protected]:YOUR-USERNAME/YOUR-REPO.git (push)For more detail, run git remote show origin. It contacts the server and reports the remote's HEAD branch, which local branches track which remote branches, and whether your branches are ahead or behind. Because it queries GitHub, it needs working authentication and a network connection.
Changing a remote's URL
Use set-url when the address changes. Common reasons are switching from HTTPS to SSH, or a repository that was renamed or moved to another owner.
git remote set-url origin https://github.com/YOUR-USERNAME/YOUR-REPO.git
git remote -vThis changes only the stored URL. Your commits, branches, and upstream settings stay as they are. Run git remote -v afterward to confirm the change.
Renaming a remote
The name origin is only a convention. Rename it with git remote rename:
git remote rename origin githubGit also updates the related settings. Remote-tracking branches move from origin/main to github/main, and branch upstream settings that pointed at origin now point at github. From then on, use the new name in commands such as git push github main.
Removing a remote
git remote remove githubThis deletes the remote's configuration and all of its remote-tracking branches from your local repository. It does not touch the repository on GitHub, and it does not touch your local branches or commits. If you remove a remote by mistake, add it again and run git fetch to restore the tracking branches.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `error: remote origin already exists.` | You ran `git remote add origin` when `origin` is already defined. | Run `git remote -v` to check it. Use `git remote set-url` to change the URL, or choose another name. |
| `fatal: No such remote: 'origin'` | You renamed or removed it, or never added it. | Run `git remote` to list the real names. |
| `fatal: 'origin' does not appear to be a git repository` | The remote name is missing or its URL is wrong. | Check `git remote -v` and fix the URL with `set-url`. |
Putting it together
This sequence switches a remote from SSH to HTTPS, renames it, and removes it again. Run git remote -v after each step to watch the result.
git remote -v
git remote set-url origin https://github.com/YOUR-USERNAME/YOUR-REPO.git
git remote rename origin github
git remote remove github
git remoteThe last command prints nothing, because no remotes remain.
Exercise
Your repository has a remote named origin that uses an HTTPS URL. You want to use SSH instead and also rename the remote to github. Which commands do you run, and what happens to the branch origin/main?
Show answer
Run git remote set-url origin [email protected]:YOUR-USERNAME/YOUR-REPO.git, then git remote rename origin github. The rename moves the remote-tracking branch from origin/main to github/main, and upstream settings that pointed at origin now point at github. Confirm with git remote -v.
Exercise
You run git remote remove github. Is the repository on GitHub deleted, and what is lost locally?
Show answer
The GitHub repository is not deleted. Locally, Git removes the remote's configuration and its remote-tracking branches, such as github/main. Your local branches and commits remain.