Git Remote
The git remote command allows to view which remotes are currently connected, but also add new connections or remove existing ones. In another way, we can say that the git remote command is designed for viewing, creating, and deleting connections to other repositories. In general, it helps to manage remote repositories. Remote connections are similar to bookmarks rather than direct link connections, which will provide a reference instead of real-time access to another repository.
Git Remote Usage Overview
The git remote command is just an interface controlling a panel of remote records stored in the repository's ./.git/config file.
Viewing Git Remote Configurations
To list the remote connections that your local repository have to other repositories, run the following command :
$ git remote
origin
If you want to display remote connections with their URL, type the following command:
$ git remote -v
origin https://github.com/userna_name/remote_repo_demo.git (fetch)
origin https://github.com/username/remote_repo_demo.git (push)
Creating and Editing Git Remote Configurations
The git remote command is also used for modifying a ./.git/config file of the repository. The following commands will modify the repo's ./.git/config file that can also be done by directly editing the ./.git/config file using a text editor.
To add a new connection to a remote repository, run the following command:
$ git remote add <name> <url>
The above works like a shortcut to reference the remote URL. So we can use the shortcut in other git commands to reference
After running the command above, we can now use the shortcut in other git commands like the following fetch command.
$ git fetch <name>
To remove the connection to the remote repository, run the following command:
$ git remote rm <name>
To rename the shortcut of the remote connection from to , type the following command:
$ git remote rename <old-name> <new-name>
The Origin Remote
The git clone command is used to clone a remote repository, but it will also automatically creates a remote connection called origin that points back to the remote cloned repository. Creating a local copy of the central directory makes it easy to pull upstream changes or push local commits. This is why the central repository of multiple Git-based projects is called origin.
Repository URLs
There are different ways to reference a remote repository. Two common and easy ways to access remote repository are:
HTTPS: It is an easy way to allow anonymous read-only access to a repository. However, it's not possible to push commits to an HTTPS addresshttps://host/path/to/repository.gitSSH: It allows read-write access. To use the SSH protocol, you will need a valid SSH account on the host machine. Git supports authenticated access via SSH out of the box.ssh://user@host/path/to/repository.git
Git Remote Commands
The git remote command accepts additional subcommands. We are going to see them below.
ADD <NAME> <URL>: It adds a record to./.git/configfor remote named<NAME>at the repository url<URL>. It accepts the following options:-foption is used to git fetch instantly after the remote record is created.--tagsoption is used to git fetch instantly and import all the tags from the remote repository
RENAME <OLD> <NEW>: It will update./.git/configfile by renaming the record<OLD>to<NEW>. All the configuration settings and all the remote-tracking branches for the remote are updatedREMOVE or RM <NAME>: It will delete the remote named<NAME>. All configuration settings and remote-tracking branches for the remote are removed.GET-URL <NAME>: It will output the URLs for a remote record named<NAME>. It accepts the following options:--pushoption is used to query push URLs instead of fetch URLs.--alloption used to list all remote URLs.
SHOW <NAME>: It will display high-level information about the remote<NAME>.PRUNE <NAME>: It will delete all the local branches for<NAME>that are not present on the remote repository. It accepts the following options:--dry-runoption is used to show which branches are set to be pruned, but it will not prune them in reality.
Git Remote Examples
Besides origin, it's a common way to be connected to other developer's repositories, which allows for collaboration outside the central repository. For example, if a developer James, create a publicly accessible repository on dev.demo.com/james.git, you can add a connection like the following :
$ git remote add james https://dev.demo.com/james.git
The command above will create access to the James individual repository. So collaboration can happen without passing by the central repository.
Showing your Remotes
By default, the git remote command will show all stored remote connections to other repositories. This will display a single line output showing the names of bookmarked remote repositories.
$ git remote
origin
james
other_repo
The git remote command comes with the -v option that will show bookmarked repository names with the URL of the remote repository. In the example below is the output of the verbose git remote command.
$ git remote -v
origin https://github.com/userna_name/remote_repo_demo.git (fetch)
origin https://github.com/username/remote_repo_demo.git (push)
james https://dev.demo.com/james.git (fetch)
james https://dev.demo.com/james.git (push)
other_repo https://github.com/userna_name/other_repo.git (fetch)
other_repo https://github.com/username/other_repo.git (push)
Adding Remote Repositories
Executing the git remote add <name> <url> command will create a new connection record to a remote repository. The <name> will be available as a shortcut for <url> to other Git commands. This command will create a new record in the repository's ./.git/config file.
git remote add demo_remote_test https://dev.demo.com/upstream_user/reponame.git; [remote "demo_remote_test"] url = https://dev.demo.com/upstream_user/reponame.git fetch = +refs/heads/*:refs/remotes/demo_remote_test/*
Inspecting a Remote
The git remote command has a subcommand show that output more detailed on the configuration of a remote. The output will include a list of branches associated with the remote and the endpoints for fetching and pushing.
$ git remote show origin
* remote origin
Fetch URL: https://dev.demo.com/upstream_user/reponame.git
Push URL: https://dev.demo.com/upstream_user/reponame.git
HEAD branch: main
Remote branch:
main tracked
Local branch configured for 'git pull':
main merges with remote main
Local ref configured for 'git push':
main pushes to main (up to date)
Fetching and Pulling from Git remotes
After adding a remote record using the git remote <name> <url> command, the remote <name> can be passed to other Git commands to interact with the remote repository. You can pass the remote <name> to git featch and git pull commands for reading from a remote repository.
Pushing to Git remotes
The git push command is used to send local commits to a remote repository.
$ git push <remote_name> <branch_name>
Renaming and Removing remotes
The git remote rename <old_name> <new_name> will rename a remote connection from to . In addition it will modifies the contents of ./.git/config to rename the record for the remote as well.
$ git remote rename <old_name> <new_name>
The git remote rm <name> command will remove the connection to the remote repository defined by the shortcut.
$ git remote rm <name>
