Remote

Remote

A remote is a repository in a different place (usually online)

Git provides the tools to keep
local and remote repositories synchronized with each other

Remote

Manage a shared project

Usually we rely on hosting platforms to be a common repository between local ones

gitlab logo Even if possible, the direct synchronization between
two personal computers is not generally used
Remote

Example of a topology with hosting platforms

topology with hosting
Remote

How to tell git of the existence of remotes

Before being able to synchronize a repository, we need to tell git were is the remote one

This can be done with the remote command

git remote add <ref_name> <url_to_repo>

The subcommand add allows to create a new reference to a remote repository

git supports https, ssh or path
Remote
git remote add origin https://gitlab.org/path/repository.git

What has changed?

git remote -v
origin  https://gitlab.org/path/repository.git (fetch)
origin  https://gitlab.org/path/repository.git (push)

Two references to a remote repository (which I called "origin") were created:

Remote
Remote

refs on the remote

Every ref on the remote is different from the local one

The remote refs will be called <remote_name>/ref_name

If the remote is called origin, the main branch will be:

  • main for the local branch
  • origin/main for the remote one
refs in official guide
Remote
git pull <remote_ref_name> <ref>

The pull command tries to retrive a ref (and associated commits) from a remote and merge them to the local history

Remote
git pull origin master

git tries to download the commits of branch master
from the remote repository called origin,
and then tries to merge them with the local history.

If there is any merge conflict it needs to be solved as usual

Remote

How to "sync" to a remote repository?

git push <remote_ref_name> <ref>

The push command takes a reference to a remote and a reference to a commit, then it sends the referenced commit to the remote, with all the commit it depends on

Remote
git push origin master

git sends the commits which the branch master depends on
to the remote repository called origin,
which then tries to merge them to its history.

If that merge would result in a conflict, the remote will refuse the commits

You will have to to first pull and solve the conflict locally

Remote

How to download an existing repo for the first time

We can do it with the previous commands

mkdir <repo_name>
cd <repo_name>
git init
git remote add origin <repo_uri>
git pull

...or, we can also use the clone command

git clone <repo_uri>

Where to find reference and help