Data report"State of code review 2024" is now liveRead the full report

How to change a remote in Git

Kenny DuMez
Kenny DuMez
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


Changing the remote in Git updates your repository's connection settings, whether due to a change in the repository's location, the migration of services, or correcting a mistake in the initial setup. This guide covers how to change remote URLs, rename remotes, and modify other remote-related settings effectively.

A "remote" in Git is a common repository that all team members use to push to and pull from, which typically resides on a server or is hosted on a platform like GitHub, GitLab, or Bitbucket and accessed using a service like Graphite. Each remote has a URL associated with it that points to the repository on the internet or network.

If you need to change the URL of an existing remote, for instance, if the repository has moved to a new URL or you need to switch from HTTPS to SSH, you can use the git remote set-url command.

Terminal
git remote set-url origin <new_url>

Replace <new_url> with the actual URL you want to set. This command updates the URL associated with the remote named origin.

After changing the remote URL, you can verify that the update was successful by listing the current remotes:

Terminal
git remote -v

This command will show all the remote names and their corresponding URLs, allowing you to confirm the changes.

Sometimes you might want to rename a remote, for example, if the name origin is not descriptive enough or if you have multiple remotes and need clearer names.

Terminal
git remote rename origin new-origin

This command changes the name of the remote from origin to new-origin. You can verify the change by running git remote -v to see the updated names.

Changing a remote branch name is a bit more involved because Git does not allow you to directly rename a remote branch. You must first rename the branch locally, push the new branch, and then delete the old branch from the remote.

If you are on the branch you want to rename:

Terminal
git branch -m new-branch-name

If you are not on the branch you want to rename:

Terminal
git branch -m old-branch-name new-branch-name

After renaming your local branch, push it to the remote:

Terminal
git push origin new-branch-name

Finally, delete the old branch from the remote:

Terminal
git push origin --delete old-branch-name

If you need to completely change the remote repository (for example, when you fork a repository and want to push your changes to the fork instead of the original), you would add a new remote and remove the old one.

Terminal
git remote add new-remote <new_remote_url>
Terminal
git remote remove origin
Terminal
git push new-remote main

Changing remote settings in Git allows you to manage your repository's network interactions. Whether you're updating a remote URL, renaming a remote, or switching to a different remote repository, Git provides the tools necessary to adapt your version control setup to meet changing project needs. These commands help ensure that your repository stays connected and aligned with the correct remote resources.

For further reading see this guide on Git remotes.

Graphite
Git stacked on GitHub

Stacked pull requests are easier to read, easier to write, and easier to manage.
Teams that stack ship better software, faster.

Or install our CLI.
Product Screenshot 1
Product Screenshot 2