Overview
Teaching: 15 min
Exercises: 0 minQuestionsObjectives
- How can we share repositories with others?
- How can we keep repositories in sync?
- Understand different workflows within the distributed version control.
We already discussed some basics of working with remotes in this lesson.
The git clone
command makes a copy of a repository. For example,
$ git clone https://host.com/user/project.git project
Git implements a distributed version control.
This means that any type of repository links that you can think of can be implemented - not just “everything connects to one central server”.
So every developer can do both:
Two topologies are very frequent: centralized and forking layout.
In Git, all repositories are equivalent but in the typical centralized style, we consider one repository as the main development line and this is marked as “central”. The “central” is a role, not a technical difference.
Features:
Advantages:
Disadvantages:
Real life examples:
In the forking layout, again we call one repository the “central” repository but people push to forks (their own copies of the repository on Github).
Features:
Advantages:
Disadvantages:
Real life examples:
$ git remote add origin <git-repo-url>
$ git remote set-url origin <new-git-url>
origin
. The origin
is just an alias.$ git remote add upstream https://github.com/project/project.git
$ git remote rm upstream
$ git remote add group-repo https://example.com/exciting-project.git
$ git remote rm group-repo
$ git remote rename <old> <new>
We synchronize remotes via the local clone.
To see all remotes:
$ git remote -v
Luke Skywalker: You know, I did feel something. I could almost see the remote.
Ben Kenobi: That’s good. You’ve taken your first step into a larger world.
(from Star Wars Episode IV - A New Hope)
We already know Git commands that can help to inspect changes. And we can use them to compare the branches in local and remote repositories. For example:
git diff
that allows to compare your current HEAD
with master
branch on remote origin
:
$ git diff HEAD origin/master
is helpful to inspect what you are about to push. We know we can add various flags to git diff
(e.g. --stat
, --numstat
, etc.)
git log
allows to list commits on a local master
branch that are about to be pushed to origin/master
:
$ git log --oneline origin/master..master
Many Git commands also have --dry-run
flag. For instance git push --dry-run
will “simulate git push” but not execute it.
Key Points
git clone
copies everything and sets some pointers to remember where the clone came from.
origin
is just an alias.We can add and remove remotes.
We can have multiple remotes.
We can call these aliases as we like.
We synchronize remotes via the local clone.
To see all remotes use
git remote -v
.