Git and remote repositiories (fetching new code templates)

On the first round, when you created a local repository and the working copy from your central repository in version control (or when you cloned a repository), the repository included material only for rounds 1-3. After course staff has prepared more material, you should retrieve these new materials.

Course staff has stored the new material in central repository, called student_template_project. You can find the central repository from:

https://course-gitlab.tuni.fi/compcs110-spring2024/student_template_project

Since Git is a distributed version control system, it is possible to retrieve updates from several central repositories to a local repository. Now you will retrieve the material of the new round from aforementioned student_template_project to your own local repository, from which you will move them to your own central repository.

Commands ``push`` and ``pull`` between student's and staff's repositories

On this course, course staff prepares material in student_template_project. You will prepare your solutions in the repository of your own. In addition, every now and then (every week) you will retrieve updates from student_template_project.

To be able to retrieve material from a repository, different from your own central repository, you must set that other repository as remote in your local repository. You can set student_template_project as remote by following the guidance below.

You can do such a operation either in Qt Creator or in command line (Terminal). Therefore, choose either of the following choices.

Warning

Before pulling new materials, make sure that you have no local changes that you have not pushed into your central repository. You can check this with the command git status. If the command prints:

On branch main
Your branch is up-to-date with 'origin/main'.
nothing to commit, working tree clean

you have no unfinished changes. If the command git status prints something else, you should remove all files and directories generated by the compiler. Such files typically end with .pro.user, and such directories typically contain the word build. Other necessary changes you can push into your central repository.

Choice 1: command line

Setting a remote repository in command line

Open command line and move to your local repository. Execute the following command:

git remote add <shortname> <url>

where <shortname> is a short, descriptive name for the new remote repository and <url> is the address of if. For example:

git remote add course https://course-gitlab.tuni.fi/compcs110-spring2024/student_template_project.git

To test how the above command succeeded, write:

git remote –v

You can see a list of all remote repositories, for example:

course https://course-gitlab.tuni.fi/compcs110-spring2024/student_template_project.git (fetch)
course https://course-gitlab.tuni.fi/compcs110-spring2024/student_template_project.git (push)
origin https://course-gitlab.tuni.fi/compcs110-spring2024/user_id.git (fetch)
origin https://course-gitlab.tuni.fi/compcs110-spring2024/user_id.git (push)

If everything goes well, you need to do the above kind of remote addition only once during the course.

Retrieving new materials from the remote repository in command line

After setting a remote repository, you can retrieve updates from it to your local repository by writing:

git pull <remote> <branch>

where <remote> is the name of the remote repository that you gave above, and <branch> is the branch of the remote repository, from which you want to retrieve the updates.

Note that the above command starts a text editor set for Git (vi by default). In the text editor, you will write a merge message describing why you have merged. This happens in the same way as writing commit messages, which we have learned earlier. (If you happen to end up in vi editor, you can exit from there by writing ”:wq”.)

For example, the command described above can be executed as:

git pull course main

We have not considered Git branches in this course, they are mainly material of the next programming course. The only branch so far is main.

The above action is needed whenever course staff has delivered new material in student_template_project, typically at the beginning of each round.

Alternatively (if you do not want to write a merge message in an editor) you can, instead of pull, do the same action in two phases as:

git fetch <remote> <branch>
git merge <remote>/<branch> -m "Write a message here"

From above, you can conclude that git pull is actually the same as git fetch + git merge.

Choice 2: Qt Creator

Setting a remote repository in Qt Creator

In Qt Creator, open a project stored in version control to make the necessary menu available. In the menu of Qt Creator, choose the action Tools > Git > Remote Repository > Manage Remotes...:

Screenshot on setting a remote repository in Qt Creator

As URL, you should have https://course-gitlab.tuni.fi/compcs110-spring2024/xyz.git or git@course-gitlab.tuni.fi:compcs110-spring2024/xyz.git, where in the place of xyz you have your user id.

Click ”Add…”, and in the opening dialog at ”Name”, write a short, descriptive name to the new remote (e.g. course or in Finnish kurssi). At ”URL”, write the address mentioned at the very beginning of this material section. After that you can see the new remote repository in the list:

Screenshot on added remote repository in Qt Creator

If everything goes well, you need to do the above kind of remote addition only once during the course.

Retrieving new materials from the remote repository in Qt Creator

After adding the remote repository in the list, choose it by clicking the mouse. After that, when clicking ”Fetch”, the updates will be retrieved from the remote repository to your local one. In the case of student_template_project, these updates mean the material of a new round. However, the updates cannot yet be seen the structure of the directory. Click ”Close”.

In Qt Creator menu, choose next action Tools > Git > Local Repository > Branches...:

Screenshot on selecting a branch of a remote repository in Qt Creator

From the list, choose branch main, just below the newly added remote repository (the line with main (master in figure), but do not confuse it with another main line). Click ”Merge” (if this takes you to a menu, choose ”Fast Forward”). If your repository is in a state, in which it should be, you should now see the updates in the structure of the directory. In this case, you should see new rounds in the directory templates.

If you have, for example, changed the content of the directory templates, it is possible that the above operation does not succeed. In such a case, contact course personnel.

The above action is needed whenever course staff has delivered new material in student_template_project, typically at the beginning of each round.

Possible errors

If the aforementioned pull command does not work, but Git prints an error message including the following lines:

hint:   git config pull.rebase false  # merge
hint:   git config pull.rebase true   # rebase
hint:   git config pull.ff only       # fast-forward only

you can try to select the first one of these (merge). So, write in command line:

git config pull.rebase false

and after that try pull again.

Note also the warning about local changes in the beginning of this section. Therefore, it is a good idea to check git status.