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
orvi
,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.
- Go to https://gitlab.cs.ut.ee/
- Login using University
username
andpassword
1.2: Installing Git on Windows
- Go to https://git-scm.com/download/win and download as per your windows version.
- Now install the download package
- Open the Git Bash terminal
- check git verison with
git --version
command - Enter
git
to see the list of available options/commands - For any help enter
git help <command name>
- e.g.
git help init
- e.g.
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:
- Ubuntu:
apt-get install git
- Centos:
yum install git
1.3: Configure Git
- To review the configuration setting at any time, issue the following command.
git config --list
- For global settings you may use
--global
option, e.g.git config --global --list
- 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"
- you can find the configuration file at
~/.gitconfig
- To see the configuration file enter
cat ~/.gitconfig
- To see the configuration file enter
1.4: Create first repo locally
- Open Git Bash Terminal
- Goto your home directory
- Create a directory with the name you want to use as your repository name.
mkdir firstrepo
cd firstrepo
- 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
- 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
- What is stagging? Stagging is an intermediate phase prior to committing a file to the repository with the
git commit
command. - Now you are inside
firstrepo
directory. - Lets first create two new empty files.
touch LICENSE
touch readme.md
- Now if you enter
git status
command, you should be able see that two files are untracked. - Add the above files to git tracking system. This is also referred to as staging the files.
git add LICENSE
git add readme.md
- 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
- Now you are inside
firstrepo
directory. - 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"
- Now again check the status with
git status
command. - Modifying the Existing
readme.md
file- Open the
readme.md
file withvim 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 onlyreadme.md
file. - Commit the changes with
git commit
command. e.g.git commit -m "readme file updated."
- Open the
- Similarly, update the
LICENSE
file with the content available at https://www.apache.org/licenses/LICENSE-2.0.txt and do a commit. - 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.
- Enter
git log
command. - Chose any two commit hashes, say
<Commit-hash1>
and<Commit-hash2>
- 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.
- 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
- Screenshot 1.9-1: Take the screenshot of the output similar to below:
- 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 replacedehury
with your Gitlab username. - Screenshot 1.9-2: Take the screenshot of the web UI similar to below:
- Now in the Git Bash terminal come outside the
firstrepo
directory withcd ..
command
1.10: Clone a repo from your remote gitlab account
- login to your gitlab account https://gitlab.cs.ut.ee/
- Create a new repo
- Click on New Project button
- Select "
Create blank project
" - Enter the project name as
secondrepo
inProject slug
field - Leave everything else to its default and click on
Create project
.
- 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 replacedehury
with your Gitlab username. - Screenshot 1.10: Take the screenshot of your terminal output similar to below:
- e.g.
- 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 tosecondrepo
directory, usingcd secondrepo
command. - Here you can see the only default
readme.md
file.
- enter clone command:
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
git checkout <branch name>
is used to switch to another branch. Lets switch to the newly created branch:git checkout branch-ex2
- 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
- Now lets append new line to
readme.md
file.echo "Now I am in branch-ex2" >> readme.md
- Stage the
readme.md
changes :git add readme.md
- Commit the changes on this branch:
git commit -m "readme file updated in branch-ex2"
- Check the repository status using
git status
command. Here, you should see that the commits are on branchbranch-ex2
. - Now check the content of
readme.md
file. Here you should see the line sayingNow I am in branch-ex2
. - 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:
- 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
- Here we will merge the content of
readme.md
file frombranch-ex2
to master branch using thegit merge <source branch-name>
command. - Lets first checkout the master branch:
git checkout master
- We are now in
master
branch. Lets first verify if the lineNow I am in branch-ex2
is present usingcat readme.md
command. As expected, that line should not present in this branch. - Now issue
git merge
command:git merge branch-ex2 -m "merging readme file to master"
- 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:
- 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:
- Now check the content of
readme.md
file usingcat readme.md
. The changes from thebranch-ex2
branch should be avalable in the currentmaster
branch.
2.6. Deleting a branch
- Make sure that
branch-ex2
andmaster
is available usinggit branch
command. - Checkout the
master
branch:git checkout master
- Delete the
branch-ex2
branch using following command:git branch -d branch-ex2
- Push all the changes to your remote gitlab account.
- Question: what will happen if you are checked out at
brnach-ex2
while deletingbranch-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'
- Question: what will happen, if the
branch-ex2
is not fully merged withmaster
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.
- 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
- Stage the file:
git add .
- Commit the staged content:
git commit -m "added some extra lines to readme file"
- 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
- Lets first create a new branch called
branch-ex3
usinggit branch branch-ex3
command. - Switch to or checkout the new branch:
git checkout branch-ex3
- 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. - Stage and commit the changes:
-
git add .
-
git commit -m "readme file modified in branch-ex3"
-
- 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:
- First checkout
master
branch:git checkout master
- 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.
- 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. - Stage and commit the changes
-
git add .
-
git commit -m "readme file re-modified in master"
-
- 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.
- Checkout
master
branch:git checkout master
- Merge the
branch-ex3
branch usinggit merge branch-ex3
command. - 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.
- 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.
- The
readme.md
file now contains information to help you find the conflict. The line between<<<<<<< HEAD
and=======
represent the line from themaster
branch and the line between=======
and>>>>>>> branch-ex3
represents the line frombranch-ex3
branch. - Lets remove the line from the
master
branch and keep the line frombranch-ex3
branch.
For this remove following lines from thereadme.md
file:
<<<<<<< HEAD This line is RE-MODIFIED in MASTER branch. =======
and
>>>>>>> branch-ex3
- 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.
- Now Stage and commit the changes
-
git add .
-
git commit -m "manually conflict handled"
-
- 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:
- 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
usinggit branch ex4-dehury
. You should replacedehury
with your name. - Checkout the newly created
ex4-dehury
branch usinggit checkout ex4-dehury
. Replacedehury
with your name. - Create a directory:
mkdir dehury && cd dehury
. Replacedehury
with your name. - Create a file
hello.txt
insidedehury
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 commandgit 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 repositorygit merge ex4-dehury
. Replacedehury
with 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