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
Usually we rely on hosting platforms to be a common repository between local ones
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 pathgit 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:
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:
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
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
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
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
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>
git <command> -h
Will display a short help on the command line
git <command> --help
Will open the manual page for that command (almost like calling man
)
Those pages are available also at the official website
Online there is also the git book, which is open and free
The official documentation page also contains other resources