Skip to content

Git and Gitlab - Beginners, working alone

Discover Gitlab

  1. Log in on https://gitlab-df.imt-atlantique.fr
  2. Create a project, name it as you wish
  3. Choose to add a README.md file
  4. Get the clone HTTPS URL

All the necessary information is available here: Gitlab.

Discover Git

The challenge here will be to familiarize yourself with the concepts of Git by using the previously created project, and to address the possibilities of documenting a project.

  1. Clone the Gitlab project repository Clone
  2. Modify the README.md file by deleting/modifying/adding lines with VScode
  3. See what happens (withgit status)
  4. Add these modifications to the index Add
  5. See what happens
  6. Create a new commented version including these modifications Commit

    The commit command adds a commit to the history of the local repository:

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true,'mainBranchName': 'GitlabRepo'}} }%%
    gitGraph:
        commit id: "Initial"
        branch Alice
        commit id: "Modif Readme"
  7. See what happens

  8. Propagate these modifications to the remote repository Push

    The push command propagate the commit to the gitlab remote repository:

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true,'mainBranchName': 'GitlabRepo'}} }%%
    gitGraph:
        commit id: "Initial"
        branch Alice
        commit id: "Modif Readme"
        checkout GitlabRepo
        merge Alice
    9. See what happens

All the necessary information is available here: Git.

Branches on Git

The challenge here is to understand how to develop software using git. You will also discover the concept of branches in Git. To that end, you will work on a simple python project: a smart Todo Command Line Interface (CLI).

Concept

Up until that point, you have worked with a single succession of commits, or branch. This is an ordered line of commits which builds on each other to form the current version of your repository. See Branching for more details.

By convention, the first and main branch created on a repository is called main or master. You will see in this succession, you can create multiple branches on a repository, separating from the main one (to work separately), and potentially merging them to combine their line of commits into a single output version.

Project

For the remainder of this formation, you will work on a project to build a simple smart todo list application with a command line interface (CLI).

Here is the starting point for this project, a single file called main.py:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
print("Welcome to SmartTodo!")

todos = ["Buy milk", "Do homework", "Call mom"]

while True:
    print("\nCommands: add, list, quit")
    command = input("Enter command: ")

    if command == "list":
        print("Your tasks:")
        for i in range(len(todos)):
            print(str(i + 1) + ". " + todos[i])
    elif command == "quit":
        break
    else:
        print("Unknown command")

  1. You can start by creating this starting file main.py inside your project and run it to check how it works:

    1
    python main.py
    
  2. Create a new branch called dev (see Create a Branch)

    As you may have noticed, the new branch is created but you are still on the main branch. You can use the command git branch to check on which branch you are (it will be marked with a star and coloured).

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true}} }%%
    gitGraph:
    commit id: "Initial Commit"
    commit id: "Modif1" type: HIGHLIGHT tag: "HEAD"
    branch dev
    1. Go on the newly created dev branch (See Change branch)

    Reminder: You can check you are on the right branch with the command git branch

  3. Add the starting file (named main.py) to your local repository (reminder: add - commit)

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true}} }%%
    gitGraph:
    commit id: "Initial Commit"
    commit id: "Modif1"
    branch dev
    commit id: "Add file" type: HIGHLIGHT tag: "HEAD"
  4. Go back to the previous branch main

    Where is your main.py file? When you change branch, git automatically set the state of your tracked files (all files added with git) so they match the state of the branch. As main.py was only added to the branch dev branch for now, it is only visible on that branch (at the moment).

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true}} }%%
    gitGraph:
    commit id: "Initial Commit"
    commit id: "Modif1" type: HIGHLIGHT tag: "HEAD"
    branch dev
    commit id: "Add file"

    To convince yourself, you can quickly go to the dev branch to see your main.py file still exists.

  5. From the main branch, make a small change on the README.md file and commit it.

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true}} }%%
    gitGraph:
    commit id: "Initial Commit"
    commit id: "Modif1"
    branch dev
    commit id: "Add file"
    checkout main
    commit id: "Modify Readme" type: HIGHLIGHT tag: "HEAD"
  6. From the main branch, merge the commits of the branch dev so its changes are added (see Merge branches).

    %%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true}} }%%
    gitGraph:
    commit id: "Initial Commit"
    commit id: "Modif1"
    branch dev
    commit id: "Add file"
    checkout main
    commit id: "Modify Readme"
    merge dev id: "Merge dev branch" type: HIGHLIGHT tag: "HEAD"

    You should now see the main.py file.

  7. Push your changes on the remote Gitlab repository

You have essentially developed a working application. Such software projects are often split into a comprehensive list of development tasks (adding a new feature, fix a bug, etc). This allows for a gradual and organized development process, and an easier collaboration with tasks that can be distributed amongst your colleagues.

  1. Complete the following task on the project

    Task – Add a New Todo

    Description: Add functionality to append a new todo to the list and display it.

    Tip

    Code tips:

    • Use input() to get the new todo
    • Use list.append() to add the todo
    • Print the updated list using the existing while loop

    Workflow tips: use the complete workflow you have seen up until then (branch - develop - add - commit - merge)