Remote repositories help developers collaborate with one other and share projects with the rest of the world. Local repositories push and pull changes from the remote repositories.
However, sometimes the URI or the URL for a remote repo may change. We may migrate our remote repository to a new host, or we may merge multiple repositories. In any case, the URI or URL of the repository will change.
The push and pull operations will not work if the remote repo is no longer present at the same URL. We need to make appropriate changes in our local repo to reset the connection to the remote repository. Let's learn how to do so.
We use the Git Remote command to work with remote repositories and manage remote connections. Let's first view the existing remote connections. Use the --verbose option(or -v flag) with the Git Remote command to see the current URLs from where we are fetching and pushing.
git remote --verbose
git remote -v
We will use the set-url option with the Git Remote command to change the URL for a remote repository. Pass the remote name and the new remote repository URL with the command. In most cases, the remote name is the origin.
git remote set-url <remote-name> <new-remote-URL>
Instead of using Git Remote set-url command, we can alter the .git/config file. A typical config file is shown below.
[core]
repositoryformatversion = 0
filemode = false
bare = false
logallrefupdates = true
symlinks = false
ignorecase = true
[remote "origin"]
url = github-repo-URL
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
To change the remote repository URL, alter the url= line under the [remote "origin"].
...
[remote "origin"]
url = <new URL here>
fetch = +refs/heads/*:refs/remotes/origin/*
...
Verify the result by using the Git Remote --verbose command.
Let's take a simple example to demonstrate the process. Consider we have a remote repository hosted on GitHub. Run the Git Remote --verbose command to verify the connection between the local and remote repo.
git remote --verbose
origin github-repo-URL (fetch)
origin github-repo-URL (push)
As we can see in the output, the command prints two URLs. One is for fetching and the other one is for pushing. Origin, in the output, represents the remote name.
Suppose we migrate our remote repository from GitHub to BitBucket. Now, we need to alter the remote connections for our local repo. Use the set-url option with the Git Remote command and pass the new URL.
git remote set-url origin bitbucket-repo-URL
Alternatively, we can modify the .git/config file and add the new URL.
...
[remote "origin"]
url = bitbucket-repo-URL
fetch = +refs/heads/*:refs/remotes/origin/*
...
We can confirm the change in URL by using the Git Remote --verbose command. As we can see in the output, both fetch and push URLs have been updated.
git remote --verbose
origin bitbucket-repo-URL (fetch)
origin bitbucket-repo-URL (push)
Remote repositories act as a central hub where all collaborators can publish their changes. Remote repositories also help individual developers to share their work with the rest of the world.
Our local repository uses the remote repository's URL for pushing and fetching changes. But when this URL changes, we need to make the appropriate changes in our local repository. The Git Remote command helps us to manage remote connections. To change a remote repository connection URL, use the set-url option with the Git Remote command. The --verbose option can be used to verify the change. Also, we can use the .git/config file and alter the remote repository URL.