git remote set-url to change remote repo URL [With Examples]

Using git remote set-url to change remote repository URL

It is inevitable to push changes that you make in your local project upstream while using git. In the process, you may require to use the git remote set-url function if you want to push the changes to a different URL. This could be because you changed your user name or you want to move the project to a new repository. Git has provided reliable means to run such operations without affecting the project progress or the work of other collaborators.

In this tutorial, we will practice how to change a remote URL without running an error supported by examples.

Advertisement

git remote set-url syntax

Following is a basic syntax which we will use through out this article to change remote URL. For complete list of supported options you can check official git documentation.

git remote set-url [--push] <name> <newurl> [<oldurl>]
git remote set-url --add [--push] <name> <newurl>
git remote set-url --delete [--push] <name> <url>

 

Git workflow to change remote URL

Following are the brief steps required to change a remote URL properly in git

git remote -v
# View the existing remotes
# origin  https://github.com/user/repo-1.git (fetch)
# origin  https://github.com/user/repo-1.git (push)

git remote set-url origin https://github.com/user/repo-2.git
# Change the 'origin' remote's URL

git remote -v
# Verify the new remote URL
# origin  https://github.com/user/repo-2.git (fetch)
# origin  https://github.com/user/repo-2.git (push)

 

Setting up the lab environment

Before we can practice using the git remote set-url command, we shall first prepare our lab environment. We will clone a remote project git-url to our local work station windows 10 pro. We will also be using git version 2.32.0.windows.2 to run this experiment.

Below is the sample output for the git clone command.

$ git clone https://github.com/Maureen-M/git-url.git
Cloning into 'git-url'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

 

How to change remote URL to repo with new account user name

Let’s assume we had issues with the current git account and had to change the user name from Maureen-M to Josephine-M-Tech after cloning. Such a change will affect the process of pushing your commits upstream. Your local changes will still be using your old username and will require you to update the changes to the new user name.

It is such a scenario that will prompt you to use the git remote set url command. Let put that into practice as follows:

Advertisement

 

Using the active project git-url we shall run a few commits and try to push the changes upstream to see what happens.

To view the current remote URLs before changing the git account username we shall run the git remote –v command as shown below:

$ git remote -v
origin  https://github.com/Maureen-M/git-url.git (fetch)
origin  https://github.com/Maureen-M/git-url.git (push)

Now let’s add a few commits to the local project  git-url before pushing upstream

$ git checkout -b newbranch
Switched to a new branch 'newbranch'
 
$ touch newfile.css

$ git add .

$ git commit -m "newfile.css"
[newbranch 0d78950] newfile.css
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 newfile.css
$ git push --set-upstream origin newbranch
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 272 bytes | 272.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote: This repository moved. Please use the new location:
remote:   https://github.com/Josephine-Techie/git-trial.git
remote:
remote: Create a pull request for 'newbranch' on GitHub by visiting:
remote:      https://github.com/Josephine-Techie/git-trial/pull/new/newbranch
remote:
To https://github.com/Maureen-M/git-trial.git
 * [new branch]      newbranch -> newbranch
Branch 'newbranch' set up to track remote branch 'newbranch' from 'origin'.

Note the message where git informs you that the repository moved and you will have to use the new location.

 

We will now apply the $ git remote set-url <remote_name> <remote_url> command to update the changes to the new location as follows:

$ git remote set-url origin https://github.com/Josephine-Techie/git-trial.git

To view the set remote URL we shall run git remote –v command again:

$ git remote -v
origin  github.com:Josephine-M-Tech/git-url.git (fetch)
origin  github.com:Josephine-M-Tech/git-url.git (push)

It’s a success! We now have a new remote URL set using the git remote set-url command.

Understand that git can automatically update the new changes by linking the two URLs which you can still work with. An issue may however arise if the old username URL is taken by someone else and the link becomes broken.

Advertisement

To view the set URL in action, let’s make changes to the committed file and push upstream using the active branch newbranch as follows:

$ git commit -m "edited newfile.css"
[newbranch 14fc920] edited newfile.css
 1 file changed, 1 insertion(+)

$ git push origin newbranch
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 283 bytes | 283.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Josephine-Techie/git-trial.git
   0d78950..14fc920  newbranch -> newbranch

You notice the new push action no longer has a warning message about the change in URL as it has been updated.

 

How to add a remote URL from the local repository

To set a remote URL from a local repository use the git remote add <remote-name> <remote-url> command as demonstrated below:

We will use the master branch in the git-url project for this example.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'origin/master'.

Next, we shall run git remote –v command to view all the present remote as shown below:

$ git remote -v
origin  https://github.com/Josephine-Techie/git-trial.git (fetch)
origin  https://github.com/Josephine-Techie/git-trial.git (push)

To set another remote from the local repository we shall run the following:

$ git remote add new-user-name  https://github.com/Josephine-M-Tech/git-url.git

We have now set a new remote url new-username in our remote repository git-url.

 

We shall run the git remote –v command to view all the remotes URLs available as shown below:

new-username    https://github.com/Josephine-Techie/git-trial.git (fetch)
new-username    https://github.com/Josephine-Techie/git-trial.git (push)
origin  https://github.com/Josephine-Techie/git-trial.git (fetch)
origin  https://github.com/Josephine-Techie/git-trial.git (push)

The remote new-username url has been added.

 

How to push new commits upstream using git remote set URL

To understand how to push commits into the set remote URL new-user we shall commit some changes to the active branch master as follows;

$ touch new-2.txt

$ git add .

$ git commit -m "new-2.txt"
[master f74febb] new-2.txt
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 new-2.txt

Now, we shall push the changes upstream using the git remote set-url --add --push origin command as illustrated below:

$ git remote set-url --add --push origin new-user/git-url.git

Next, we will run the git status function to confirm if the changes were successfully pushed upstream.

$ git status
On branch master
nothing to commit, working tree clean

The status confirms a clean working tree and therefore the push procedure was a success.

Note that the git remote set-url --add --push origin function, when used, it will override any push made from the default url

 

How to delete a remote URL in git

To remove a remote URL you will use the git remote remove <name> command. In this example, we are going to remove the new-user URL as follows;

Let's view the current URLs as shown:

new-user        https://github.com/Josephine-M-Tech/git-url.git (fetch)
new-user        https://github.com/Josephine-M-Tech/git-url.git (push)
new-username    https://github.com/Josephine-Techie/git-trial.git (fetch)
new-username    https://github.com/Josephine-Techie/git-trial.git (push)
$ git remote remove new-user

We will then run the git remote –v command to confirm the removal of new-user as demonstrated below:

$ git remote -v
new-username    https://github.com/Josephine-Techie/git-trial.git (fetch)
new-username    https://github.com/Josephine-Techie/git-trial.git (push)

From the output, we no longer have the new-user URL among the list of remote URLs.

 

How to set a remote SSH URL in git

To set a remote URL if you have SSH configured GitHub a count follows the same process as changing the remote URL. You will insert the new SSH-remote-url in place of the new-remote-url as shown in the syntax below.

$ git remote set-url <remote_name> <ssh_remote_url>

Here is an example of how the SSH URL looks like:

git@github.com:Josephine-Techie/git-url.git

Now we shall use in the $ git remote set-url <remote_name> <ssh_remote_url> command to set a new URL as shown below:

$ git remote set-url origin git@github.com:Josephine-Techie/git-url.git

$ git remote -v
new-user        https://github.com/Josephine-M-Tech/git-url.git (fetch)
new-user        https://github.com/Josephine-M-Tech/git-url.git (push)
new-username    https://github.com/Josephine-Techie/git-trial.git (fetch)
new-username    https://github.com/Josephine-Techie/git-trial.git (push)
origin  git@github.com:Josephine-Techie/git-url.git (fetch)
origin  git@github.com:Josephine-Techie/git-url.git (push)

We have set a new URL origin using git SSH URL

 

Summary

We have covered the following topics relating to git remote set URL:

  • Understanding git remote set URL
  • How to set remote URL in git scenarios
  • How to change remote repository URL
  • How to push new commits upstream using git remote set URL function
  • How to delete a remote URL
  • How to set a remote SSH in git

 

Further Reading

Git-remote-set-url

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Leave a Comment