Remotes management in Git

In Git, you can have more than one remote repository. git remote command is used to manage them. Operations illustrated include how to add a remote, remove a remote, view a remote’s information, change a remote’s URL, etc.

Add a remote

# Add a remote
$ git remote add <name> <url>

# Examples:
# Add a remote named test
$ git remote add test https://github.com/username/repository.git

Note: When you clone a remote repository, Git sets the remote as origin for you automatically.

List remotes

# Show remotes with names
$ git remote
origin

# Show remotes with names, urls
$ git remote -v
origin    git@github.com:mojombo/grit.git (fetch)
origin    git@github.com:mojombo/grit.git (push)

Change URL of a remote

# Chang url of origin
$ git remote set-url origin https://github.com/USERNAME/REPOSITORY.git

Verify its URL has changed:

$ git remote get-url origin
origin https://github.com/USERNAME/REPOSITORY.git

Get URL of a remote

# Get the url of origin
$ git remote get-url origin
origin https://github.com/USERNAME/REPOSITORY.git

Fetch updates from a remote

# Fetch updates from the remote named origin
$ git remote update origin

Rename a remote

# Rename a remote
$ git remote rename <old> <new>

# Examples:
# Rename origin to main
$ git remote rename origin main

Remove a remote

# Remove a remote
# Note: all the remote-tracking branches and settings
# for the remote are removed.
$ git remote remove <name>

# Examples:
# Remove the remote nameed server2
$ git remote remove server2