Version control using Git: Sharing repositories online

Overview

Teaching: 10 min
Exercises: 10 min
Questions
  • How can I set up a public repository online?
  • How can I clone a public repository to my computer?
  • How does version control scale from 1 to N users per repository?
Objectives
  • We get a feeling for remote repositories (more later).
  • We are able to publish a repository on the web.
  • We are able to fetch and track a repository from the web.

From our laptops to the web

We have seen that creating Git repositories and moving them around is simple and that is great.

So far everything was local and all snapshots, branches, and tags are saved under .git.

If we remove .git, we remove all Git history of a project.

  • What if the hard disk fails?
  • What if somebody steals my laptop?
  • How can we collaborate with others across the web?

Remotes

We will learn how to work with remote repositories in detail in the next section of the lesson. In this section we only want to get a taste to prepare us for the comming sessions.

Our goal is to publish our guacamole recipe on the web. Don’t worry, you will be able to remove it afterwards.

To store your git data on another server, you use remotes. A remote is treated the same as a branch - most of the same concept apply, but you can also push changes to the remote and pull from the remote.

You might use remotes to:

  • Back up your own work.
  • To collaborate with other people.

There are different types of remotes:

  • If you have a server you can ssh to, you can use that as a remote.
  • GitHub is a popular, closed-source commercial site.
  • GitLab is a popular, open-core commercial site. Many universities have their own private GitLab servers set up as in GFZ we have GitLab instance
  • Bitbucket is yet another popular commercial site.
  • Another option is NotABug

Requirement

We will be using the public GitLab instance of gitlab.com. The concept we teach remains the same for GitLab instances at your institute or university. It will also match for other platforms such as GitHub.


Type-along: Create a new repository on GitLab

For the rest of this page, we will make a new repository for our guacamole recipe on GitLab at GitLab.com, send our code to it, and then see how others can get the code from it.

  1. Login
  2. Click on green button lables as “New project” ( you will find it on the right top side of the screen. See an example figure below)
  3. Click on the green button

On this page choose a project name (screenshot).

For the sake of this exercise do not select “Initialize this repository with a README” (what will happen if you do?).

Once you click the green “Create project” button, you will see a page similar to:

What this means is that we have now an empty project with either an HTTPS or an SSH address: click on the blue drop down option “Clone”, you will see options to clone either by HTTPS or SSH.

To push changes to the project you will either need SSH keys for the SSH address (preferred) or you will have to use your GitLab username and password when using the HTTPS address. If you don’t know what to do, use HTTPS.


Pushing our guacamole recipe repository to GitLab

We now want to try the last option that GitLab suggests:

Push an existing Git repository

  1. Now go to your guacamole repository on your computer.
  2. Check that you are in the right place with git status.
  3. Copy paste the last four lines (see below) to the terminal and execute those, in my case (you need to replace the “user” part and possibly also the repository name):
$ git remote add origin https://gitlab.com/user/recipe.git
$ git push -u origin --all
$ git push -u origin --tags

You should now see:

Counting objects: 100% (30/30), done.
Delta compression using up to 4 threads
Compressing objects: 100% (26/26), done.
Writing objects: 100% (30/30), 2.97 KiB | 2.97 MiB/s, done.
Total 30 (delta 5), reused 0 (delta 0)
To https://gitext.gfz-potsdam.de/user/recipe.git
 * [new branch]      master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

Reload your GitLab project website and - taa-daa - your commits should now be online!

What just happened? Think of publishing a repository as uploading the .git part online.


Cloning a repository

Now other people can clone this repository and contribute changes. In the next sessions we will learn in detail how collaborative distributed version control works.

At this point only a brief demo - if you copy the SSH or HTTPS address, you can clone repositories like this (again adapt the “namespace/repository.git” part):

$ git clone https://gitlab.com/user/recipe.git

This creates a directory called “recipe” unless it already exists. You can also specify the target directory on your computer:

$ git clone https://gitext.gfz-potsdam.de/user/recipe.git myrecipe

What just happened? Think of cloning as downloading the .git part to your computer. After downloading the .git part the branch pointed to by HEAD is automatically checked out.

Key Points