Object-oriented Programming in C++

Bachelor of Science, École Polytechnique

git and gitlab

To grade your labs, we will use the gitlab hosted by the binet association. gitlab is an online developer platform used to store your code.

Creating an account

In theory, you should already have an account. To check that it is the case, try to log in on https://gitlab.binets.fr.

Creating a repository

Once you have your account, you have to create a project named cse201 by clicking on the New project button. Give the name "cse201" to your project (this name does not really matter). Let the visibility as private, and unselect "Initialize repository with README". Finally, click on "Create repository". You should now see your beautiful project. Since the project is empty, you should see some instructions explaining how to start.

What you have created on gitlab is named a "repository". A repository is like a directory where you store your source files. It's not so far from dropbox, but tailored for development. gitlab manages the repository with the git tool. git is a "distributed version control system". It means that git keeps an history of the files stored in the repository. It's very useful if you make mistakes when you modify your code, and want to retrieve an older version of a file.

Adding your professors as a friend

Now, you have to add your professors in your project. For that, click on "Manage", then "Members", and finally "Invite member". Type "gael.thomas" and selects the user. Modify the role to Developper, otherwise, we will not see your code. Finally, click on invite. Do the same with the user "julien.tierny".

Adding a ssh key to gitlab

For the moment, your source files are stored in the servers of gitlab. To work with them, you have to copy them locally on your machine. For that, you have first to create what we name a ssh keypair, which is used as an authentication token to load and store the files in your repository. Technically, a ssh keypair contains a public and a private key. You give the public key to someone (in our case gitlab), and you use the private key to generate an authentication token. Since only the owner of the private key can create a token that matches the public key, anyone can verify that you are actually the owner of a public key, which authenticates you.

To create this keypair, you have to use the ssh-keygen tool. Then you have to display the public key and to record it in gitlab. For that, you have to launch a terminal (by launching the terminal application for the Mac users, or by starting WSL for the Windows user). In the terminal, type the following commands:

ssh-keygen -t rsa cat ~/.ssh/id_rsa.pub

At this step, you should see your public key in the terminal. On gitlab, click on your profile, then preferences, then SSH Keys and then add new key. In the Key field, copy paste copy-paste the public key that you see in the terminal, and then click on "Add key".

Cloning your project locally

Now, you can clone your repository in your machine. Cloning a repository means that you create a local copy. For that, you first have to find the URL of your gitlab repository. Click on your user icon on the upper right of the screen, then "Your repositories", and then on the cse201 project. Click on the "Code" button in green and you should see an URL the URL of your project, which starts with "ssh@".

Then, you have to go to the cse201 directory you created in the first lab, where you have the helloworld and sudoku directories. In this directory, type the following command, where you have to replace %url% by the URL of your repository:

git init git remote add origin %url% git fetch git checkout -t origin/main

Importing your first lab

We will now import your first lab in your repository. For that, you have to add the source files located in the lab1 by typing git add lab1/hello.c and git add lab1/sudoku.c.

The add command of git only adds a file locally to what is named the "index". The "index" is sort of waiting room in your local copy of the repository where you accumulate the files that will have to be sent to the remote repository. You can verify that the files are actually in the "index" by typing "git status". You should see them prefixed by "new file".

Then, to send them to gitlab, you have to proceed in two steps. First you have to create what we name a local commit. A commit is a snapshot of your code. In detail, a commit includes changes to all files that were previously added to git with git add. To commit your code, just type git commit -a -m "Initial import".

  • The "-a" flag means that all the files that were added in a previous commit have to be committed. In this case, it's useless since it's your first import. However, for the next commits, this is will be very useful to use this flag.
  • The "-m" flag means that the next parameter is the commit message. A commit message is very useful to know the changes you made in each commit.

Now that you have your local commit, you can sent it to gitlab with the command git push. git push takes the local commit and add them in the remote repository. To check that everything went well, reload the gitlab webpage, and you should now see your marvelous source codes. If it's the case, congratulation!

gitignore

While you compile your code, gcc generates files. You will not add this files to your repository in order to save space on the server. For that, at the root of the repository, create a file named .gitignore. Copy-paste the content below into your .gitignore file.

Then, add this file to your repository with git add .gitignore, commit your change with git commit -a -m "add .gitignore", and push the commit to the gitlab server with git push.

Congratulation, you are now a git user! You can find the useful git command studied in this exercise here here.