How to Set or Change Remote URL in Git

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

ProtocolURL FormatRequires Login
HTTPShttps://github.com/user/repo.gitYes (username/password or token)
SSHgi*@gi****.com:user/repo.gitNo (uses SSH keys)

Summary

TaskCommand
View remote URLgit remote -v
Add a new remotegit remote add origin <url>
Change an existing remotegit remote set-url origin <new-url>
Remove a remotegit 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.

Sharing Is Caring:

Leave a Comment