Lab 3 - Git & C++ Project Template

Responsible: Ing. Jakub Minařík

Git (1h 30min)

First, read the Git Tutorial chapter to get familiar with the workflow and commands

Exercise

Sign On

Select any of the following free Git Services and register for it

This server will serve as your "origin" (remote repository) for the rest of the BPC-PRP course.

The instructors will have access to all your repositories, including their history, and can monitor your progress, including who, when, and how frequently commits were made.

Create a repository on the server to maintain your code throughout the course.


Cloning repository in labs

HTTPS - GitHub Token

When cloning a repository via HTTPS, you cannot push changes using your username and password. Instead, you must use a generated GitHub token.

To generate a token, go to Profile picture (top-right corner) > Settings > Developer Settings > Personal Access Tokens > Tokens (classic) or click here. Your generated token will be shown only once, after which you can use it as a password when pushing changes via HTTPS until the token expires.

SSH - Setting a Different Key

You can generate an SSH key using the ssh-keygen command. It will prompt you in a single input for both the location and name of your key, followed by a passphrase. For use in a laboratory set a passphrase. The default location where the system recognizes the keys is ~/.ssh.

When cloning a repository via SSH in labs, you may encounter a problem with Git using the wrong SSH key.
You'll need to configure Git to use your generated key:

git config core.sshCommand "ssh -i ~/.ssh/<your_key>"

In this command, <your_key> refers to the private part of your generated key.

On GitHub, you can add the public part of your key to either a specific repository or your entire account.

  • To add a key to a project (repository level):
    Go to Project > Settings > Deploy keys > Add deploy key, then check Allow write access if needed.

  • To add a key to your GitHub account (global access):
    Go to Profile picture (top-right corner) > Settings > SSH and GPG keys > New SSH key.

Team Exercise

As a team, complete the following steps:

  1. One team member creates a repository on the server.
  2. All team members clone the repository to their local machines.
  3. One team member creates a "Hello, World!" program locally, commits it, and pushes it to the origin.
  4. The rest of the team pulls the changes to their local repositories.
  5. Two team members intentionally create a conflict by modifying the same line of code simultaneously and attempting to push their changes to the server. The second member to push will receive an error from Git indicating a conflict.
  6. The team member who encounters the conflict resolves it and pushes the corrected version to the origin.
  7. All team members pull the updated version of the repository. Each member then creates their own .h file containing a function that prints their name. Everyone pushes their changes to the server.
  8. One team member pulls the newly created .h files and modifies the "Hello, World!" program to use all the newly created code. The changes are then pushed to the origin.
  9. All team members pull the latest state of the repository.

C++ Project Template (30 min)

Now it is time to create your main project for this course.

  1. Create a project on the web page of your Git service.
  2. Clone project to the local
  3. Create the following project structure
 |--docs
 | \--placeholder
 |--README.md
 |--CMakeLists.txt
 |--.gitignore
 |--include
 | \--<project_name>
 |   \--lib.hpp
 \--src
   |--lib.cpp
   \--main.cpp
  1. Fill all required files
  • README.md is a brief description and how-to-use your project.
  • folder docs will be used later. Now just create file placeholder.
  • Write some basic code into the cpp adn hpp files.
  • Fill the .gitignore file. It is used to inform git to ignore some files, folders or extensions, not to commit it into the repository.
# Ignore build directories
/build/
/cmake-build-debug/
/cmake-build-release/

# Ignore CMake-generated files
CMakeFiles/
CMakeCache.txt
cmake_install.cmake
Makefile

# Ignore IDE-specific files (CLion and JetBrains)
.idea/
*.iml
  1. Commit and push your project to the server and share it with other members of the team.