Skip to article frontmatterSkip to article content

PS1: Discover Git and GitLab

IUT d'Orsay, Université Paris-Saclay

Before starting to code, it is important to familiarize yourself with the tools that help organize your programming work.

Objectives

The goal of this session is to understand the following points:

These points are essential for the rest of the course, so you must master them!

Activating your GitLab account

As a student at the IUT of Orsay, you already have an account on the IUT Orsay GitLab.

git.iut-orsay.fr

To log in, use your department username and password.

Personal Access Token

To establish a secure connection between your machine and the GitLab server at the IUT, we will use a Personal Access Token (PAT), which replaces the old password system.

To create a PAT:

  1. Go to your profile by clicking on your avatar (top left) and then Edit profile.
Edit profile
  1. In the left menu, click on Access Tokens.
Access Tokens
  1. Add a new token by clicking on Add new token.
Add new token
  1. Give the token a name that indicates the workstation being used (for example, IUTOrsay if you’re working on a machine at the IUT).
  1. Set an expiration date of one year (the maximum allowed) from today’s date.
  1. Select all the scopes.
  1. Click on Create personal access token.

Creating a remote repository

  1. Create your first repository by clicking on the + button located at the top left of the screen.
Create new project
  1. Create a blank project with the name qualite-dev-s2-<first name>-<last name> (for example qualite-dev-s2-hoang-la).
  1. For the project URL, choose your short login in Users and the default URL provided.

  2. Select the “Private” visibility option for your project and check the option to initialize the project with a README.

  1. Click on Create Project.

  2. If you see the following messages, you can ignore them by clicking Don’t show again for the SSH key and X for Auto DevOps.

SSH Key and Auto DevOps pipeline
  1. Browse through the default README proposed by GitLab.

Configuring your workstation

Your project has been created on the IUT server. Now, to work on this project from your local workstation, you need to configure it first.

  1. Copy the following lines of code into a terminal.
git config --global user.name "<Firstname> <Lastname>"
git config --global user.email "<University email>"

For example :

git config --global user.name "Hoang La"

Then :

git config --global user.email "hoang.la@universite-paris-saclay.fr"

Or :

git config --global user.email "hoang.la-tmp@universite-paris-saclay.fr"

You can now clone your remote repository to download it to your local workstation: 2. Click on the Code button on the right.

Code
  1. Copy the URL under the Clone with HTTPS option.
Clone with HTTPS
  1. Navigate to your preferred working directory (and open a terminal).

  2. Enter the following command, pasting the copied address and adding your login and PAT in the appropriate places.

git clone https://<your short login>:<Personal Access Token>@git.iut-orsay.fr/<login of the creator of the project>/<project name>.git

For example:

git clone https://hla:glpat-1234thisIsYourPAT5678@git.iut-orsay.fr/hla/monprojet.git

Local repository

A directory with the same name (local repository) has now been downloaded to your machine.

  1. Run the following commands.
cd monprojet/
ls
git status

The ls command allows you to list all the files in the directory, while git status shows the status of the repository. For now, the latter command should indicate that there is nothing to commit and your working directory is clean

  1. What do you observe when running the following commands?
touch my-first-file.txt
git status
  1. Add a line to my-first-file.txt (for example, Hello World!).

  2. Ask Git to track the change to my-first-file.txt by running the following command.

git add my-first-file.txt
  1. Run the git status command again. What does it tell you?
  1. Now, to create a history of the staged changes (in this case, the file my-first-file.txt with the line Hello World!), you need to commit these changes and add a descriptive message by running the command git commit -m "<descriptive commit message>".

For example:

git commit -m "Initial commit of my-first-file.txt saying Hello World!"
  1. Re-run git status to check the status of your repository. What does it indicate?
  1. Run git push.

  2. Go to GitLab and check that the remote repository matches the local repository.

Remote repository

  1. Click on my-first-file.txt.
  2. Click on Edit.
  3. Select the option Open in Web IDE.
  4. Add a second line to this file (for example, Hi!).

You will see a 1 on the left bar, indicating that a change has been made.
If you click on it, you will see a change regarding the my-first-file.txt file and the option to Commit and push to ‘main’.

  1. Add a commit message.
  2. Click on the Commit and push to ‘main’ button.
  3. Click on Continue when asked if you want to save it to the default branch (main).

Synchronizing the local repository with the remote repository

Go back to your local repository and let’s assume that a collaborator made changes and then pushed them to the remote repository without you being notified.

  1. Run git status. Do you notice anything unusual?
  1. Run git remote update, then git status. What do you see now?
  1. Run git diff origin/main to see the differences between the two repositories.
  1. To synchronize your local repository with the remote repository (which, this time, is ahead), run git pull.
  1. To view the commit history, run git log. To exit the log, press q (for ‘quit’).

Ignoring files

  1. Create a file hello-world.cpp (for example, with touch hello-world.cpp).

  2. Open hello-world.cpp in your favorite code editor and copy the following code.

hello-world.cpp
#include <iostream>
using namespace std;
int main() {
    cout << "Hello World!" << endl;
    return 0;
}
  1. Compile the code with g++ -o <filename> <filename>.cpp.

For example:

g++ -o hello-world hello-world.cpp
  1. Run the executable hello-world with the command ./hello-world.

Executables are among the files that we want to ignore.

  1. Create the .gitignore file.
  1. Write the name of the executable hello-world in .gitignore.
  1. Run git add, git commit -m "<message>", and git push to send the .gitignore file to the remote repository.

  2. Run git status. What changes compared to usual?

To finish...

Let’s finish this lab with a little reorganization.

  1. Create a directory PS1/ in your project and move everything except for .git, .gitignore, and README.md to this directory.

You will maintain this project throughout the course by creating separate directories for each practical session (PS).

  1. Go back to the root of the project and run git add ..
  1. Commit the staged changes with git commit.

Since we will be ignoring many executables in the future, to avoid modifying .gitignore every time, we will use regular expressions to ignore files without extensions (executables).

  1. Replace the content of the .gitignore file with the following code.
.gitignore
*
!*.*
!*/
  1. Run add, commit, and push to synchronize your repositories.

  2. Add your instructor to your project by following the instructions below.

Manage members
  1. Go back to the objectives and check the points you have mastered. Practice the commands and points you have not fully understood yet. Call your instructor over if necessary.