How to change the URI (URL) for a remote Git repository?

To change the URL (URI) of a remote Git repository, follow these steps:

1. Check Current Remote Configuration

List all remotes and their URLs:

git remote -v
# Example output:
# origin  https://github.com/user/old-repo.git (fetch)
# origin  https://github.com/user/old-repo.git (push)

2. Update the Remote URL

Use git remote set-url to change the URL of the remote (e.g., origin):

git remote set-url origin https://github.com/user/new-repo.git

3. Verify the Change

Confirm the URL has been updated:

git remote -v
# Output:
# origin  https://github.com/user/new-repo.git (fetch)
# origin  https://github.com/user/new-repo.git (push)

Additional Scenarios

Change Only Fetch or Push URL

Update the fetch or push URL separately:

git remote set-url --push origin https://github.com/user/new-repo.git  # Push URL
git remote set-url --fetch origin https://github.com/user/new-repo.git # Fetch URL

Add a New Remote (Instead of Renaming)

If you want to keep the old remote and add a new one:

git remote add new-remote https://github.com/user/new-repo.git

Switch from HTTPS to SSH

Example for changing protocol:

git remote set-url origin git@github.com:user/new-repo.git

Key Notes

  • Remote Name: Replace origin with your remote’s name (e.g., upstream).
  • Authentication: Ensure you have permissions for the new URL (e.g., SSH keys for SSH URLs).
  • Collaborators: If others use the repo, inform them to update their local remotes.

By following these steps, you can seamlessly update your Git repository’s remote URL!

Leave a Reply

Your email address will not be published. Required fields are marked *