Git remote

  1. Definition
  2. Creating and editing git remote configurations
  3. The origin remote
  4. Repository URLs
  5. Git remote subcommands
  6. Git remote examples
  7. Showing your remotes
  8. Adding remote repositories
  9. Inspecting a remote
  10. Fetching and pulling from Git remotes
  11. Pushing to Git remotes
  12. Renaming and removing remotes

Definition

The git remote command is designed for creating, viewing and removing connections to other repositories. Remote connections are considered to be bookmarks in other repositories, which are convenient names used for referencing URL that are not convenient enough.

The diagram below displays two remote connections from your repository into the central repository and into the repository of another developer. You can pass the origin to another developer and they will shortcut to other Git commands. As a result, you won’t have to reference the connections by their full URLs.

Creating and editing git remote configurations

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

  • 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
  • Be executing the following command, remove the connection to the remote repository, which is called <name> .

    git remote rm
  • Run the command below for renaming a remote connection:

    git remote rename

You can get the same result, as in case of executing the commands above, by modifying ./.git/config file with a text director, too.

The origin remote

Cloning a repository by executing git clone command, will automatically create a remote connection, that is called origin, and that points back to the cloned repository. Creating a local copy of the central directory is an easy method of pulling upstream modifications or publishing local commits. That’s why, the central repository of many Git-based projects is called “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. In the case of accessing a remote repository via HTTPS, you can’t push commits to an HTTPS address.

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

Use SSH for a read and write access. You need a valid SSH account. But take into account, that only authenticated access is supported by Git. Modern secure third party hosting solutions, for example, Bitbucket.com, can provide such URLs:

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

Git remote subcommands

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

The subcommand below will add a record to ./.git/config file for remote named <NAME> at the repository url <URL> . It accepts a -f option, that after the creation of the remote record will instantly git fetch <name>. Another option accepted by this subcommand is a --tags option, which will instantly git fetch <name> and import all the tags from the remote repository.

ADD <NAME> <URL>

The following subcommand will update ./.git/config for renaming the record <OLD> to <NEW>. As a result, all the configuration settings and branches of the remote will be updated.

RENAME <OLD> <NEW>

Next subcommand edits ./.git/config removing the remote named <NAME>. In the end, all the configuration settings and branches for the remote will be removed.

REMOVE or RM

Executing the subcommand below will output the URLs for a remote record. This subcommand accepts --push and --alloptions.

GET-URL <NAME>

The next subcommand will output information about the remote <NAME>.

SHOW <NAME>

And the last command will delete all the local branches for <NAME> that don’t exist on the remote repository. This subcommand accepts --dry-run option which lists the branches that have been set to be pruned, but in fact doesn’t prune them.

PRUNE <NAME>

Git remote examples

While using Git, it’s desirable to be connected to other developers’ repositories too, because this gives the opportunity to collaborate outside of the central repository and have effective work. For example, your co-worker has kept a repository dev.example.com/tom.git that is publicly accessible, you can add a connection like the following:

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

Showing your remotes

By default, the git remote command lists all the remote connections that have previously been stored to other repos. As a result, you will have a single line output listing the names of bookmarked remote repositories.

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
origin  git@bitbucket.com:origin_user/reponame.git (fetch)
origin  git@bitbucket.com: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 (fetch)
other_repo    https://example.com/other_repo/r

Adding remote repositories

Invoking the git remote add command, a new connection record will be created to a remote repository. Then the <name> name will be available as an acceptable shortcut for <url> in other Git commands. This command creates a new record in the ./.git/config of the repository.

git remote add fake_test https://example.com/upstream_user/reponame.git; [remote "remote_test"] 
url = https://example.com/upstream_user/reponame.git 
fetch = +refs/heads/*:refs/remotes/remote_test/*

Inspecting a remote

You can also attach the show subcommand to the git remote command to have more detailed output on the configuration of a remote, included a list of those branches that are connected with the remote and the endpoints appended for fetching and pushing.

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

Immediately after the configuration of a remote record done with the git remote command, the remote name can serve 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>

Renaming and removing remotes

The git remote command renames a remote connection from <that-name> to <this-name>. Besides, it modifies the contents of ./.git/config to rename the record for the remote as well.

git remote rename <that-name> <this-name>

The git remote rm command removes the connection to the remote repository defined by the <name> parameter.

git remote rm <name>



Do you find this helpful?

Related articles