Worksheet 1: Git & GitLab

This worksheet covers the tasks you need to carry out to set up Git & GitLab for the exercises and coursework in COMP1721.

Configuring Git

  1. Make sure Git is installed on your own PC. Use your Linux distribution’s package manager to install it if necessary. If you have a Mac, you can download an official Mac build from the Git project, or install it as part of the Xcode Command Line Tools, or install it using Homebrew. If you are wedded to Microsoft Windows, there is a Git for Windows distribution available that should be suitable.

  2. If you wish to use the School of Computing’s remote Linux desktop for the practical work in this module, you’ll find that Git is already available there – however, the version is rather old. You can make a newer version available using the following command:

    module add git/2.9.5
    

    You can make this permanent by adding this command to the end of your ~/.bashrc file; otherwise, you’ll need it to enter it at the start of every terminal window session.

  3. IMPORTANT: If you haven’t already configured Git with your identity, do so now using commands like the following in a terminal window (substituting your actual name and university email address, obviously):

    git config --global user.name "Charles Xavier"
    git config --global user.email sc17cx@leeds.ac.uk
    

    Do this in every environment where you expect to use Git, so that authorship of your future Git commits is recorded consistently.

Accessing GitLab

  1. If you already have an account on gitlab.com, you can skip to Step 2 below.

    If you haven’t yet created an account, do so now by visiting https://gitlab.com/users/sign_up in a web browser.

    Screenshot of the account registration interface on gitlab.com

    GitLab account registration

  2. Log in to gitlab.com. If you created an account last semester, you should see a list of your GitLab projects, including one for COMP1721.

    Once you can see a link to your COMP1721 project area, click on it. This will take you to a screen that looks similar to this:

    Screenshot of GitLab project screen

    GitLab project screen

  3. Spend some time familiarising yourself with your project area. Click on the files and directories listed in your project area to examine their contents. Note that there are separate directories for each piece of coursework and for each exercise.

    Find the Repository menu in the list of options on the left of the screen, then use the Commits link in this menu to examine the commits to your project repository. Currently there should be only one, made by us to set up the directory structure of the repository.

Setting Up SSH

Setting up SSH access to gitlab.com is necessary so that you don’t get repeatedly prompted to authenticate yourself every time that you interact with a remote repository. If you’ve already set up SSH access for your GitLab account, you can skip the steps below and move on to the next task, cloning your repository.

  1. If you already have an SSH key pair, go to Step 2 below. Otherwise, enter the following to generate a key pair:

    ssh-keygen -t ed25519 -C COMMENT
    

    Be sure to use -C (uppercase) and not -c (lowercase) for the comment option. Replace COMMENT in the command above with suitable comment text (e.g., your email address). Enclose your comment in quote marks if it contains any spaces.

  2. Log in to gitlab.com and click on the user avatar on the right of the GitLab toolbar, then choose Settings from the drop-down menu. On the Settings page, click on the SSH Keys link that appears in sidebar (expand the sidebar if you can’t identify this link).

  3. Locate the file containing your public key. This should be a file named id_ed25519.pub (or possibly id_rsa.pub), in a subdirectory of your home directory named .ssh. Open this file in a text editor and copy the entire contents of the file. Paste the file contents into the Key area on the SSH Keys page, specify a title to identify your public key, then click the green Add key button.

Cloning Your Repository

  1. Open a terminal window, cd to a directory where you want to store your work, then create a local clone of your GitLab repository by entering a command like this:

    git clone git@gitlab.com:comp1721/22/USERNAME.git comp1721
    

    Substitute your actual University of Leeds username1 for USERNAME in the above command.

    The comp1721 appearing at the end of this command is the name of the directory that Git will create to hold your cloned repository. Use a different name for this directory if you like (but make sure it doesn’t include spaces).

    If you’ve set up an SSH key correctly, you will be prompted to enter the SSH passphrase that you specified when creating the key pair 2. Subsequent uses of Git commands that interact with the remote repository will not require you to reenter your passphrase, at least until your next login.

  2. Move into the new directory and examine it. You should see files and directories that match what is in your repository at gitlab.com.

Pushing Commits to GitLab

  1. Do the first exercise. If you wish to use the IntelliJ IDEA development environment for the exercises, do Worksheet 2, which will guide you through the process of using this tool. Otherwise, use your favourite text editor, making sure that the .java file containing your solution is in the exercises/ex1 directory. Use Git to add and commit your changes, then send them to gitlab.com with3

    git push
    
  2. Wait a few seconds, then view your GitLab project again in a web browser. Use the Commits and Files links from the Repository menu to verify that the commit was pushed successfully and that your exercise solution is now visible in the remote repository.

Need Help With Git?

You’ll be expected to use Git a lot in COMP1721. If you are not confident with Git, use these resources to help you improve your skills:


  1. Do not use your full email address here! Your username is the part before the @ symbol. ↩︎

  2. If this doesn’t work, run the ssh-add command, enter your passphrase when prompted, then try again. ↩︎

  3. Note: if you are using a very old version of Git, you might need to use the more explicit command git push origin main here. ↩︎