In Git, the remote URL connects your local repository to a remote one—typically on platforms like GitHub, GitLab, or Bitbucket. Setting the correct remote URL allows you to push and pull code between your local machine and the remote repository.
In this guide, you’ll learn how to set, view, and change the remote URL of a Git repository.
🔹 Check Existing Remote URL
To see the current remote URLs configured for your repository:
git remote -v
Example output:
origin https://github.com/username/repo.git (fetch)
origin https://github.com/username/repo.git (push)
✅ Set a New Remote URL
If you’re setting the remote for the first time:
git remote add origin <repository-URL>
Example:
git remote add origin https://github.com/username/myproject.git
origin
is the default name for the remote, but you can name it anything.
🔄 Change Existing Remote URL
If you need to update the remote URL (e.g., switching from HTTPS to SSH or changing repositories):
git remote set-url origin <new-URL>
Example (switching to SSH):
git remote set-url origin gi*@gi****.com:username/myproject.git
🧪 Test the New Remote
After setting or changing the remote URL, test it by fetching from the remote:
git fetch origin
If it works without errors, the URL is correctly configured.
📝 Remove a Remote (Optional)
To remove an existing remote:
git remote remove origin
Then you can re-add it with the correct URL.
🧠 Tip: HTTPS vs SSH
Protocol | URL Format | Requires Login |
---|---|---|
HTTPS | https://github.com/user/repo.git | Yes (username/password or token) |
SSH | gi*@gi****.com:user/repo.git | No (uses SSH keys) |
Summary
Task | Command |
---|---|
View remote URL | git remote -v |
Add a new remote | git remote add origin <url> |
Change an existing remote | git remote set-url origin <new-url> |
Remove a remote | git remote remove origin |
Setting or updating your Git remote URL correctly ensures smooth collaboration and code synchronization. Whether you use HTTPS or SSH, keeping your remote setup in sync with your project needs is essential.