Git maintains the state of a project in two kinds of repositories: local and remote. The local repository is stored on the local machine, whereas the remote repository is mostly stored on a remote machine via a platform like GitHub or GitLab.
Initially, on the local machine, we store the URL of the remote repository by a specific name which allows us to perform git operations easily and keep both the repositories in sync. Storing the remote URL by a name makes it easier to perform git operations without writing the entire URL. The names origin
and upstream
are common choices for a remote URL name.
If we wish to change the remote repository’s URL, we use the git remote set-url
command.
Following is the most basic syntax for using the git remote set-url
command.
git remote set-url remote_name remote_URL
In the above snippet of code, remote_name
will be replaced by an existing remote name, and remote_URL
will be replaced by the new URL of the remote repository.
To enlist the existing remote URLs, use the
git remote -v
command.
If the remote_name
entered by the user does not already exist, the CLI will generate an error.
We can use the --push
, --add
or --delete
options to alter the behavior of the get remote set-url
command. The following table briefly describes the usage of these options and shows their syntaxes.
Option | Description | Syntax |
--push | With this option, only push URLs are updated whereas the fetch URLs remain the same. | git remote set-url [--push] <repo_name> <new_remote_url> [<old_remote_url>] |
--add | This option adds a new URL without making any changes to the existing remote URL. | git remote set-url --add [--push] <repo_name> <new_remote_url> |
--delete | This option is used to delete a remote repository's URL. | git remote set-url --delete [--push] <repo_name> <old_remote_url> |
In the above table,
old_url
can be a regular expression as well.
The following snippet of codes updates the remote URL to a repository of the user named educative who has a project named test.
git remote origin https://github.com/educative/test.git
The above snippet of code updates the URL for the origin
remote using the HTTPS protocol.
git remote origin git@github.com:educative/test.git
The above snippet of code updates the URL for the origin
remote using the SSH protocol.
Note that there are several other platforms like GitHub which support Git. These include GitLab, BitBucket, etc. GitHub uses just for the sake of an example in above snippets.
Free Resources