Arvutiteaduse instituut
  1. Kursused
  2. 2021/22 sügis
  3. DevOps: tarkvara tarnimise ja käituse automatiseerimine (LTAT.06.015)
EN
Logi sisse

DevOps: tarkvara tarnimise ja käituse automatiseerimine 2021/22 sügis

Please contact chinmaya.dehury@ut.ee for more information.

  • Homepage
  • Lectures
  • Practicals
  • Exam & Grading
    • Final Exam Sample Questions
    • Final Exam Guidelines
  • Submit Homework
  • Grades
  • Plagiarism
  • Communication

Practice Session 5: Git: Version Control System

In this practice session, you will get familiar with git version control system. You will use the Institute's GitLab environment. After this practice session, you should be able to

  • create and manage the repository
  • create and merge branches
  • resolving merge conflicts
  • Getting involved with other project through forking the repo. creating merge request, etc
  • you should be able perform most of the above tasks through command line interface

Pre-Requisites:

  • basic bash commands; ls, vim or vi, cat, mkdir, cd etc.

Exercise 1: Creating first repository

Intro: In this exercise you will get familiar with the basic Git commands, including installation and configuration of git, creating repositories, cloning repositories etc. You will not use Github rather the GitLab environment provided by the institute.

1.1: Create Institute GitLab environment

In this practice session, we will not use GitHub. We will use Institute provided GitLab environment.

  1. Go to https://gitlab.cs.ut.ee/
  2. Login using University username and password

1.2: Installing Git on Windows

  1. Go to https://git-scm.com/download/win and download as per your windows version.
  2. Now install the download package
  3. Open the Git Bash terminal
  4. check git verison with git --version command
  5. Enter git to see the list of available options/commands
  6. For any help enter git help <command name>
    • e.g. git help init

1.2.1: Installing Git on macOS:

  • brew install git
  • ref: https://git-scm.com/download/mac

1.2.2: Installing Git on Linux and Unix:

  1. Ubuntu:
    • apt-get install git
  2. Centos:
    • yum install git
  • Ref: https://git-scm.com/download/linux

1.3: Configure Git

  1. To review the configuration setting at any time, issue the following command.
    git config --list
  2. For global settings you may use --global option, e.g. git config --global --list
  3. Configure user information to be used for all the local repositories. Make sure, you put your information in quotes " ".
    • git config --global user.name "Your Name"
    • git config --global user.email "your@email.id"
  4. you can find the configuration file at ~/.gitconfig
    • To see the configuration file enter cat ~/.gitconfig

1.4: Create first repo locally

  1. Open Git Bash Terminal
  2. Goto your home directory
  3. Create a directory with the name you want to use as your repository name.
    • mkdir firstrepo
    • cd firstrepo
  4. Initialize the git repo with
    • git init command
    • This will add a .git directory with some necessary information
    • You can go inside and check the directory

1.5: Status of the repository

  1. This refers to the state of the working directory and the staging area. Enter the command git status inside firstrepo directory.
    On branch master
    No commits yet
    nothing to commit (create/copy files and use "git add" to track)

DYI: learn by yourself the meaning of above lines. You should be able to recall the concepts of branching, commit, tracking/staging.

1.6: Staging the file

  1. What is stagging? Stagging is an intermediate phase prior to committing a file to the repository with the git commit command.
  2. Now you are inside firstrepo directory.
  3. Lets first create two new empty files.
    • touch LICENSE
    • touch readme.md
  4. Now if you enter git status command, you should be able see that two files are untracked.
  5. Add the above files to git tracking system. This is also referred to as staging the files.
    • git add LICENSE
    • git add readme.md
  6. Now check the status of your repo using git status command and find the meaning of the output by yourself.
    • git status

1.7: Committing the files

  1. Now you are inside firstrepo directory.
  2. Lets make an initial commit and check the status. The -m option lets you give a short summary of this commit.
    • git commit -m "Initial repo commit"
  1. Now again check the status with git status command.
  2. Modifying the Existing readme.md file
    • Open the readme.md file with vim readme.md command and add "Git is cool.." line.
    • Enter git add command: git add . . Here . (dot) at the end of the command represents everything in the current directory. In our case, the command will add only readme.md file.
    • Commit the changes with git commit command. e.g. git commit -m "readme file updated."
  3. Similarly, update the LICENSE file with the content available at https://www.apache.org/licenses/LICENSE-2.0.txt and do a commit.
  4. To see the history of commits, issue git log command.

DYI: Find the ans: What information can be obtained from the output of git log command?

1.8: Find the difference between two changes

git diff command takes two inputs (e.g. hash of two commits) and reflects the differences between them. Make multiple changes and commits to readme file.

  1. Enter git log command.
  2. Chose any two commit hashes, say <Commit-hash1> and <Commit-hash2>
  3. Enter the git diff command with both hashes: git diff <Commit-hash1> <Commit-hash2>
    DYI: Find the ans: how to interpret git diff command?

1.9: Pushing the changes to remote repo

So far all the changes are made locally. Now its time to push the code changes to your remote gitlab repo using git push command.

  1. push the local repo to your remote gitlab account. You should replace dehury with your gitlab user account name.
    • git remote add master https://gitlab.cs.ut.ee/dehury/firstrepo
    • git push master
  2. Screenshot 1.9-1: Take the screenshot of the output similar to below:
  3. To verify, go to your remote gitlab account at https://gitlab.cs.ut.ee/ and see if it is present. The repository should be available at https://gitlab.cs.ut.ee/dehury/firstrepo. You should replace dehury with your Gitlab username.
  4. Screenshot 1.9-2: Take the screenshot of the web UI similar to below:
  5. Now in the Git Bash terminal come outside the firstrepo directory with cd .. command

1.10: Clone a repo from your remote gitlab account

  1. login to your gitlab account https://gitlab.cs.ut.ee/
  2. Create a new repo
    • Click on New Project button
    • Select "Create blank project"
    • Enter the project name as secondrepo in Project slug field
    • Leave everything else to its default and click on Create project.
  3. Now go to your git terminal and make sure you are **not** inside firstrepo directory.
    • enter clone command: git clone <url of the second repo>
      • e.g. git clone https://gitlab.cs.ut.ee/dehury/secondrepo. You should replace dehury with your Gitlab username.
      • Screenshot 1.10: Take the screenshot of your terminal output similar to below:
    • At this point, this may ask you for the university's username and password.
    • Now you should see the secondrepo directory available in currect directory. Change the current directory to secondrepo directory, using cd secondrepo command.
    • Here you can see the only default readme.md file.

Exercise 2: Branches and Merging

Intro: Branching means you diverge from the main line of development and continue to do work without messing with that main line. The default branch name in Git is master. As you start making commits, you’re given a master branch that points to the last commit you made. git init command creates master branch by default and most people don’t bother to change it.

We will use the firstrepo repository in this exercise.
Go to the Git Terminal. Clone the firstrepo and do cd firstrepo. To recap: in the current directory, you have two files: readme.md and LICENSE.

2.1. Creating a New Branch

Create a new branch called branch-ex2 using git branch branch-ex2 command. This creates a new pointer to the same commit you’re currently on. It is a good practice to use branches rather than the master branch. This allows you to not mess with the main code.

2.2. Listing branches

git branch <options> command allows you to list, create, or delete branches. List all the branches using git branch command. Here you should see following two branches
$ git branch

     branch-ex2
    * master

The * indicates the current branch.

2.3. Change the current branch

  1. git checkout <branch name> is used to switch to another branch. Lets switch to the newly created branch:
    • git checkout branch-ex2
  2. Now enter git branch command to see the current active branch. The output changes to following:
    $ git branch
   * branch-ex2
     master

2.4. Modify new branch content

  1. Now lets append new line to readme.md file.
    • echo "Now I am in branch-ex2" >> readme.md
  2. Stage the readme.md changes : git add readme.md
  3. Commit the changes on this branch: git commit -m "readme file updated in branch-ex2"
  4. Check the repository status using git status command. Here, you should see that the commits are on branch branch-ex2.
  5. Now check the content of readme.md file. Here you should see the line saying Now I am in branch-ex2.
  6. Screenshot 2.4-1: Using git push command push all the changes of this branch to your remote gitlab account and take the screenshot terminal output. Sample output given below:
  7. Screenshot 2.4-2: Using your browser, go to your remote Gitlab repo and take the screenshot showing the update status. Sample output given below:

2.5. Merge the content of multiple branches

  1. Here we will merge the content of readme.md file from branch-ex2 to master branch using the git merge <source branch-name> command.
  2. Lets first checkout the master branch: git checkout master
  3. We are now in master branch. Lets first verify if the line Now I am in branch-ex2 is present using cat readme.md command. As expected, that line should not present in this branch.
  4. Now issue git merge command: git merge branch-ex2 -m "merging readme file to master"
  5. Screenshot 2.5-1: From the terminal push all the changes of this branch to your remote gitlab account and take the screenshot. Sample output given below:
  6. Screenshot 2.5-2: Using your browser, go to your remote Gitlab repo and take the screenshot showing the update status. Sample output given below:
  7. Now check the content of readme.md file using cat readme.md. The changes from the branch-ex2 branch should be avalable in the current master branch.

2.6. Deleting a branch

  1. Make sure that branch-ex2 and master is available using git branch command.
  2. Checkout the master branch: git checkout master
  3. Delete the branch-ex2 branch using following command:
    • git branch -d branch-ex2
  4. Push all the changes to your remote gitlab account.
  5. Question: what will happen if you are checked out at brnach-ex2 while deleting branch-ex2?
    You will get the error similar to below:
    $ git branch -d branch-ex2
    error: Cannot delete branch 'branch-ex2' checked out at 'D:/firstrepo'
  1. Question: what will happen, if the branch-ex2 is not fully merged with master branch?
    You will get following error:
    $ git branch -d master
     error: The branch 'master' is not fully merged.
     If you are sure you want to delete it, run 'git branch -D master'.

Exercise 3: Handling Merge Conflicts

Conflict arises when you may have made overlapping changes to a file, and Git cannot automatically merge the changes. In this exercise we will see how to handle the conflicts.

3.1. Update the readme file with some extra lines.

Lets append some new lines to the readme.md file. Make sure that you are inside firstrepo in your git terminal.

  1. Append two new lines using following commands:
    • echo "This line is added in master branch." >> readme.md
    • echo "This is just an extra line inserted while in master branch." >> readme.md
  2. Stage the file: git add .
  3. Commit the staged content: git commit -m "added some extra lines to readme file"
  4. At this point the content of readme.md file should be:
    Git is cool..
    Now I am in branch-ex2
    This line is added in master branch.
    This is just an extra line inserted while in master branch.

3.2. Create and checkout new branch

  1. Lets first create a new branch called branch-ex3 using git branch branch-ex3 command.
  2. Switch to or checkout the new branch: git checkout branch-ex3
  3. Open readme.md file and update the third line:
    From: This line is added in master branch.
    To: This line is MODIFIED in BRANCH-EX3 branch.
  4. Stage and commit the changes:
    • git add .
    • git commit -m "readme file modified in branch-ex3"
  5. Here, the content of the readme.md file should be:
    Git is cool..
    Now I am in branch-ex2
    This line is MODIFIED in BRANCH-EX3 branch.
    This is just an extra line inserted while in master branch.

3.3. Checkout master branch and modify the readme.md file

Now lets again modify the same file in master branch:

  1. First checkout master branch: git checkout master
  2. Verify the content of readme.md file. The content should be as below:
    Git is cool..
    Now I am in branch-ex2
    This line is added in master branch.
    This is just an extra line inserted while in master branch.
  1. Lets update again the third line of readme.md file.
    From: This line is added in master branch.
    To: This line is RE-MODIFIED in MASTER branch.
  2. Stage and commit the changes
    • git add .
    • git commit -m "readme file re-modified in master"
  3. Now the content of readme.md file should look like:
    Git is cool..
    Now I am in branch-ex2
    This line is RE-MODIFIED in MASTER branch.
    This is just an extra line inserted while in master branch.

3.4. Merge to master branch

Now at this point we will merge the branch-ex3 branch to master branch.

  1. Checkout master branch: git checkout master
  2. Merge the branch-ex3 branch using git merge branch-ex3 command.
  3. Here you will get following similar error:
    Git is cool..
    Auto-merging readme.md
    CONFLICT (content): Merge conflict in readme.md
    Automatic merge failed; fix conflicts and then commit the result.
  1. If you now see the content of readme.md file, this should look like:
    $ cat readme.md
    Git is cool..
    Now I am in branch-ex2
    <<<<<<< HEAD
    This line is RE-MODIFIED in MASTER branch.
    =======
    This line is MODIFIED in BRANCH-EX3 branch.
    >>>>>>> branch-ex3
    This is just an extra line inserted while in master branch.
  1. The readme.md file now contains information to help you find the conflict. The line between <<<<<<< HEAD and ======= represent the line from the master branch and the line between ======= and >>>>>>> branch-ex3 represents the line from branch-ex3 branch.
  2. Lets remove the line from the master branch and keep the line from branch-ex3 branch.
    For this remove following lines from the readme.md file:
    <<<<<<< HEAD
    This line is RE-MODIFIED in MASTER branch.
    =======

and

    >>>>>>> branch-ex3
  1. Now the readme.md file should look like:
    Git is cool..
    Now I am in branch-ex2
    This line is MODIFIED in BRANCH-EX3 branch.
    This is just an extra line inserted while in master branch.
  1. Now Stage and commit the changes
    • git add .
    • git commit -m "manually conflict handled"
  2. Screenshot 3.4-1: From the terminal push all the changes of this branch to your remote gitlab account and take the screenshot. Sample output given below:
  3. Screenshot 3.4-2: Using your browser, go to your remote Gitlab repo and take the screenshot showing the update status. Sample output given below:

Exercise 4: Forking and merging a branch

Intro: A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.
In this excerice your going to complete following tasks:

  • Forking the main repository (https://gitlab.cs.ut.ee/dehury/DevOps21FallPub) to your gitlab account.
  • You will clone the forked repository and create a branch, modify it by adding your files and merge it your forked respository.
  • Finally, you will send merge request to owner of the main repository (https://gitlab.cs.ut.ee/dehury/DevOps21FallPub)

NOTE: Replace dehury with your name in all the following places

Forking a project

  • Login to your remote GitLab account: https://gitlab.cs.ut.ee
  • Go to https://gitlab.cs.ut.ee/dehury/DevOps21FallPub. Do not replace dehury with your name here.
  • Fork this repository (GUI)

Clone your forked project, create and add your files and merging

  • Clone that repository to your local environment using git clone <url>command. The url reposiory for clonning can be found here
  • Make a new branch ex4-dehury using git branch ex4-dehury. You should replace dehury with your name.
  • Checkout the newly created ex4-dehury branch using git checkout ex4-dehury. Replace dehurywith your name.
  • Create a directory: mkdir dehury && cd dehury. Replace dehury with your name.
  • Create a file hello.txt inside dehury directory using following command.
    echo "Helloooo, Its <your_name> speaking from DevOps course." > hello.txt
    • Copy all the screenshots so far you have taken (in the previous exercises) to this directory. This can be done manually using windows explorer or similar environments in other OSs.
  • Change to parent directory cd ..
  • Stage parent directory git add .
  • Commit the changes locally using git commit -m "change from <your_name>"
  • See the list of existing remotes git remote
  • Add a remote with the name main using the command git remote add main https://gitlab.cs.ut.ee/<your_gitlab_username>/DevOps21FallPub.git
  • Push the changes git push main
  • Check out git checkout main and Merge the changes to your repository git merge ex4-dehury. Replace dehurywith your name.
  • Push the final changes git push main
  • After this you should see the following changes in your repo

Sending merge request to owner of the main repository

Here, you can send a merge request in two ways: 1)Gitlab GUI 2) git cmd line interface

  • Create a merge request using GUI, for this go to your gitlab project and create as shown below:
  • Make sure, you added correct projects as shown below
  • If everything goes well, PI and TAs will get a merge request notification in their private slack channel.

Deliverables

  • Upload the screenshot taken wherever mentioned
  • Pack the screenshots into a single zip file and upload them through the following submission form.

Deadline: 22nd Oct 2021

5. Lab 5
Sellele ülesandele ei saa enam lahendusi esitada.
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused