How To Change Git Remote Origin / URL

Share:
how-to-change-git-remote-origin-url

As a developer, you are probably pushing your code to your remote origin every day. Git is a distributed version-control system for tracking changes in any set of files, originally designed for coordinating work among programmers cooperating on source code during software development.

Git remote is a pointer that refers to another copy of the repository that is usually hosted on a remote server. However, in some cases, you might choose to migrate your Git repository or to merge existing ones. As a consequence, you may need to change the Git origin remote to a new URL.

In this tutorial, you are going to learn how you can change the URL of a Git remote easily.

Change Git Remote URL

Each Git repository can have zero or more Git remotes linked to it. When you clone a repository, the name of the remote is set automatically to origin and points to the repository that you cloned from. If you created the repository locally, you can add a new remote.

The remote can point to a repository hosted on a Git hosting service such as GitHub, GitLab, and BitBucket or your private Git server.

Follow the steps below to change the URL of a remote:

Go to repository's directory

Change working directory to the directory where the repository is located. Issue the cd command:

$ cd /path/to/your/repository

Run git remote

This command will list the existing remotes and see their names and URLs:

$ git remote -v

The output should be similar like below:

origin  https://gitserver.com/user/repo_name.git (fetch)
origin  https://gitserver.com/user/repo_name.git (push)

Set git remote

Use the git remote set-url command followed by the remote name, and the remote’s URL:

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

The URL can be found on the repository page of your Git hosting service. It should be something like:

https://gitserver.org/user/repo_name.git

Verification

In order to verify that the changes were made, you can use the git remote command with the “-v” option (for verbose). Verify that the remote’s URL was successfully changed by listing the remote connections:

$ git remote -v

The output should be similar like below:

origin	https://gitserver.org/user/repo_name.git (fetch)
origin https://gitserver.org/user/repo_name.git (push)

Congratulations, you successfully changed the URL of your Git remote!

Changing Git Remote to SSH

In some cases, you may have configured your Git repository to use SSH key-based authentication.

If you want to change your Git origin remote using SSH authentication, you can use the same git remote set-url command but you will have to use the SSH URL in order to connect.

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

The SSH URL usually takes the following form :

SSH URL : [email protected]<repo_url>:<url>/<git_repository>.git

For example, if your repository was configured on Github, you would use the following command to change your remote.

$ git remote set-url origin [email protected]:user/repository.git

Verification

Again, to verify that the remote’s URL was successfully changed by listing the remote connections:

$ git remote -v

The output should be similar like below:

origin  ssh://[email protected]:user/repo_name.git (fetch)
origin  ssh://[email protected]:user/repo_name.git (push)

Congratulations, you successfully changed the URL of your Git remote!

 

 

Tags Linux
Share:

0 comment

Leave a reply

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