DEV Community

Ahmet Ustun
Ahmet Ustun

Posted on • Originally published at ahmetustun.Medium

Changing Remote in Git Repositories

Photo by [Lorenzo Herrera](https://unsplash.com/@lorenzoherrera?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/photos/p0j-mE6mGo4?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

Git is a powerful version control system that allows developers to collaborate on code and track changes made to a project over time.

Sometimes, you may need to change the remote URL associated with a Git repository. In this article, we’ll explore how to change the remote URL of a Git repository with code samples.


Checking the Current Remote URL

Before changing the remote URL, it’s essential to verify the current remote URL associated with your Git repository.

You can do this by using the following command:

# Verify the current remote URL
git remote -v
Enter fullscreen mode Exit fullscreen mode

This command will display the current remote URL associated with your Git repository.


Adding a New Remote URL

To add a new remote URL to your Git repository, you can use the following command:

# Add a new remote URL
git remote add <name> <URL>
Enter fullscreen mode Exit fullscreen mode

Replace with a name for your new remote and with the URL of your new remote repository.

git remote add newRemote https://github.com/user/newrepo.git
Enter fullscreen mode Exit fullscreen mode

Removing an Old Remote URL

To remove an old remote URL from your Git repository, you can use the following command:

# Remove an old remote URL
git remote rm <name>
Enter fullscreen mode Exit fullscreen mode

Replace with the name of the old remote repository, you want to remove.

git remote rm oldRemote
Enter fullscreen mode Exit fullscreen mode

Setting a New Remote URL as Default

To set a new remote URL as the default for your Git repository, you can use the following command:

# Set a new remote URL as default
git remote set-url origin <URL>
Enter fullscreen mode Exit fullscreen mode

Replace with the URL of your new remote repository.

git remote set-url origin https://github.com/user/newrepo.git
Enter fullscreen mode Exit fullscreen mode

Conclusion

Changing the remote URL of a Git repository can be done easily with the right commands without losing any of your existing code.

Make sure to check the current remote URL before making any changes, and set the new remote URL as default to avoid any confusion in the future.

Top comments (0)