W3docs

git remote

Learn how to create and edit git remote configurations, add remote repositories, inspect, fetch, pull, push, rename and remove them, see examples.

Definition

The git remote command is designed for creating, viewing and removing connections to other repositories. Remote connections act as bookmarks to other repositories, providing convenient names to reference URLs that might otherwise be cumbersome.

The diagram below shows two remote connections: one to a central repository and one to another developer's repository. You can share the remote name with another developer, allowing them to use it as a shortcut in other Git commands. As a result, you won’t have to reference the connections by their full URLs.

git remote

Creating and editing git remote configurations

The git remote command is also used for editing the ./.git/config file of the repository. You can do it by executing the commands below:

  1. Use the command below for adding a new remote connection to a remote repository. Then you can use <name> as an appropriate shortcut for <url> in other Git commands.

git remote add

git remote add <name> <url>
  1. Execute the following command to remove the connection to the remote repository, which is called <name>.

git remote rm

git remote rm <name>
  1. Run the command below for renaming a remote connection:

git remote rename

git remote rename <old> <new>
  1. Use the command below to change the URL of an existing remote:

git remote set-url

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

You can achieve the same result by editing the ./.git/config file directly with a text editor.

The origin remote

Executing the git clone command automatically creates a remote connection named origin, which points to the cloned repository. A local copy makes it easy to pull upstream changes or publish local commits. This is why the central repository in many projects is named origin.

Repository URLs

There are many ways of referencing a remote repository, but the two simplest ones are considered to be via HTTPS and SSH protocols. HTTPS URLs are commonly used for read-only access or when paired with credential helpers or personal access tokens for authentication.

HTTPS URL format

http://host/path/to/repo.git

Use SSH for read and write access. You need a valid SSH account. Note that access requirements depend on the repository's permissions; public repositories allow unauthenticated access, while private ones require authentication. Modern secure third party hosting solutions, for example, Bitbucket.com, can provide such URLs:

SSH URL format

ssh://user@host/path/to/repo.git

You can also use the widely supported SCP-like syntax:

user@host:path/to/repo.git

Git remote subcommands

The git remote command has its own subcommands. We are going to examine them below.

**git remote add <name> <url> **

Adds a record to the ./.git/config file for a remote named <name> pointing to <url>. Accepts a -f option, which immediately runs git fetch <name> after creating the remote record. Another option accepted by this subcommand is a --tags option, which will immediately run git fetch <name> and import all the tags from the remote repository.

git remote add <name> <url>

**git remote rename <old> <new> **

Updates ./.git/config to rename <old> to <new>. All configuration settings and branches for the remote are updated accordingly.

git remote rename <old> <new>

git remote remove or rm

Removes the remote named <name> from ./.git/config. All associated configuration settings and branches are removed.

git remote rm <name>

**git remote set-url <name> <new-url> **

Changes the URL of an existing remote record in ./.git/config.

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

**git remote get-url <name> **

Outputs the URLs for a remote record. This subcommand accepts --push and --all options.

git remote get-url <name>

**git remote show <name> **

Outputs detailed information about the remote <name>.

git remote show <name>

**git remote prune <name> **

Deletes remote-tracking branches for <name> that no longer exist on the remote. This subcommand accepts --dry-run option which lists the branches that have been set to be pruned, but in fact doesn’t prune them.

git remote prune <name>

Git remote examples

Connecting to other developers’ repositories enables collaboration outside the central repository. For example, if a colleague maintains a publicly accessible repository at dev.example.com/tom.git, you can add it as follows:

**git remote add <name> <url> **

git remote add tom http://dev.example.com/tom.git

Showing your remotes

By default, git remote lists all configured remote connections. The output shows a single line for each bookmarked repository.

the git remote command

git remote
origin
upstream
other_repo

You can invoke the git remote command with the -v option that will list bookmarked repository names with the URL of the corresponding repository.

git remote -v

git remote -v
origin      [email protected]:origin_user/reponame.git (fetch)
origin      [email protected]:origin_user/reponame.git (push)
upstream    https://example.com/upstream_user/reponame.git (fetch)
upstream    https://example.com/upstream_user/reponame.git (push)
other_repo  https://example.com/other_repo/reponame.git (fetch)
other_repo  https://example.com/other_repo/reponame.git (push)

Adding remote repositories

Running git remote add creates a new connection record in ./.git/config. The <name> then serves as a shortcut for <url> in other Git commands.

git remote add example

git remote add fake_test https://example.com/upstream_user/reponame.git

Inspecting a remote

Appending the show subcommand provides detailed configuration output, including connected branches and fetch/push endpoints.

git remote show

git remote show upstream
* remote upstream
Fetch URL: https://example.com/upstream_user/reponame.git
Push URL: https://example.com/upstream_user/reponame.git
HEAD branch: master
Remote branches:
master tracked
 simd-deprecated tracked
 tutorial tracked
Local ref configured for 'git push':
 master pushes to master (fast-forwardable)

Fetching and pulling from Git remotes

Once a remote is configured, its name can be used as an argument for other Git commands to create a communication with the remote repository. You can use git fetch and git pull commands for reading from a remote repository.

Pushing to Git remotes

The git push command writes to a remote repository.

**git push <remote-name> <branch-name> **

git push <remote-name> <branch-name>

Practice

Practice

What are the correct statements about the " `git remote` command as described in the W3Docs Git Tutorial?