How to Update Remote URL in Local Git Repository?

The git remote URL is specified in the .git/config file in a repository's folder. To update a git remote URL, you can do either of the following:

Please be aware of the potential implications of changing the remote URL. For example, if you are collaborating with others on the repository, changing the remote URL could affect their ability to push, fetch, or work with the repository.

#Running git remote set-url Command

To change the URL of a remote repository your local repository points to, you can run the following command in terminal:

git remote set-url <remote-name> <repo-url>

For example, if the remote name is "origin" and the remote URL is "git@github.com:designcise/web.git", the command would be:

git remote set-url origin git@github.com:designcise/web.git

After running this command, you can verify the updated remote URL using the following command:

git remote -v

It will show output similar to the following:

# origin  git@github.com:designcise/web.git (fetch)
# origin  git@github.com:designcise/web.git (push)

#Manually Editing URL in .git/config File

To manually edit the .git/config file, you can follow these steps:

  1. Open the .git/config file in a text editor;
  2. Search for the section labeled "[remote "<remote-name>"]", where <remote-name> is the current remote the repository points to;
  3. Edit the corresponding remote URL and save the file.

For example, if the current remote name is "origin" and the current remote URL is "git@github.com:designcise/repo.git", the relevant section in the .git/config file would look like:

[remote "origin"]
    url = git@github.com:designcise/repo.git

Here, you can edit the URL to the new repository for the corresponding remote.

After making the changes, you can check if the remote URL has been updated using the following command:

git remote -v

The output will display the updated URLs for fetch and push operations:

# origin  git@github.com:designcise/web.git (fetch)
# origin  git@github.com:designcise/web.git (push)

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.