DevOps Cours Partie2
DevOps Cours Partie2
TEK-UP
GIT & GITHub : Version Control System
➤ Why GIT?
➤ Understanding the Core Concept of GIT
➤ Features of GIT
TEK-UP
VCS
• VCS helps team to rollback to previous version in case of any issue with specific
Version.
TEK-UP
VCS
Types of VCS
❖ Centralized Version Control System (CVCS)
• uses a central server to store all files and enables team collaboration.
• works on a single repository to which users can directly access a central server.
TEK-UP
➤ Here Each Work Station is connect with Central Code
Repository.
TEK-UP
Distributed Version Control System
▪ In Distributed VCS, every contributor has a local copy or “clone” of the main
repository.
▪ User can change and commit local Repo without any interference.
▪ User can update their local Repo from the Central Server.
TEK-UP
➤ Operations in DVCS are fast.
➤ New changes can be done locally without manipulating the central data.
➤ If the central server gets crashed at any point of time, the lost data can
be easily recovered from any one of the contributor’s local repositories.
TEK-UP
GIT Key Terminology
➤ Git Repository
TEK-UP
GIT Key Terminology
• Working Directory - Area of Live Files, also known as Untracked area of GIT.
• Staging Area - Staging Area is draft space when git starts tracking and saving changes that
occur in files.
• Git Directory - Also called ‘local Repo’, is your .git repo. It’s area where GIT save everything.
TEK-UP
GIT Key Terminology
• Branch in GIT - Branch in Git is a way to keep developing and coding a new feature or
modification on the software and still not affecting the main part of the project.
TEK-UP
LAB
• Virtualization tool: Vmware Workstation Pro
• Objective: preparation of two virtual machines (CentOs) for next Labs
• Virtual machine 1: Git, Docker
• Virtual machine 2: Jenkins
• You have to create a virtual network for the interconnection of the two machines:
• Private network 192.168.10.0/24
• Automatic NAT network for internet access
• Use Mobaxterm to access the 2 VMs from the host machine
TEK-UP
GIT Install
• The .git folder is a bit like a magic hat into which you put your current
magic show.
• Everything you organise into a show format is put inside this magic hat
and can be 'pulled out' whenever, wherever you want.
Git: First Commit
#echo "hello second file" >> file2.txt //create and edit file2
#git status
#echo "add new line " >> file1.txt // modify in file1
#git status
#git add . //add all files in the current directory
#git commit –m “my second commit
Git: Tracking and Logs
• To identify who has modified the source code and who is responsible of
each commit
• the commit data determines which account name to attach to the push
GitHub
Github is a website and cloud-based service that helps developers store and manage
their code, as well as track and control changes to their code.
GitHub is a Remote repository for your code base
GitHub
#git clone httpd://github.xxx/xxx.git
#git pull
GitHub
#echo "create file from local repo to send" >> local_file.txt
#git add .
#git commit –m “local file to send “
#git push
>> remote: Support for password authentication was removed on August 13,
2021. Please use a personal access token instead.
ull
Push/p
Dev3(feature y)
Dev1 (feature1)
Dev2 (feature n)
Dev4 (feature x)
Dev1 (feature2)
Roll Back is all about to Undo the changes, you did in repo.
➤ In GIT this can be done via RESET and REVERT.
➤ RESET - Practically, user can think of it as a “rollback”.
➤ Reset command points local environment back to a previous commit.
Git: Undoing Changes
REVERT - Net effect of the git revert command is similar to reset, but its approach is
different.
Revert adds a new commit at the end of the chain to "cancel" changes.
Revert or Reset ?
If user have already pushed commits to the remote repo, a revert is a nicer way to cancel
out changes.
Git workflow works well for picking up additional commits at the end of a branch, but it
can be challenging if a set of commits is no longer seen in the chain when someone resets
the branch pointer back.
If Commit in local then Reset is good, If commit is pushed then revert is good option.
Git: Undoing Changes
Git: Undoing Changes
Git: Undoing Changes
To Hard reset your changes follow the steps:
•#echo 'wrong code' > file_to_reset.txt
•#git add .
•#git commit -m "commit to reset"
•#git push
•# git log --pretty=oneline --decorate
•#git reset –hard id_of_commit // or your can use #git reset --hard HEAD~N (N represent
the number of commit your want to delete)
•#git log –oneline //to verify that your have successfully reseted your commit
Git: Undoing Changes
Note:
If you have done a mistake by reseting more commits than you were supposed to
do
•#git reflog //to detect the id of the commit you have deleted (suppose it is XXX)
•#git reset –hard XXX
•#git log –oneline // to verify that you have recovered your commits
Git: Undoing Changes
To Soft reset your changes follow the steps:
•# git reset –soft id_of_commit
•#git status //notice there are files ready to commit and waiting in the staging area
•# git reset file_name //to cancel the git add and let files become untracked by git
•#git status //to verify that your files are in the untracked area now.
•Correct your mistake by modifying your files and then add,commit and push them
Git: Undoing Changes
To revert your changes follow the steps:
•# git revert HEAD //it will ignore your last commit and placing a new commit upon it
•# git revert --no-commit HEAD // to check what files are going to be changed/deleted before
confirming commit
•# git revert --no-commit HEAD~3.. //to ignore the last three commit at once
•# git revert --no-commit xxyyyzz..HEAD //to ignore all commit for the xxxyyyzzz commit
until HEAD
•# git revert –abort // to cancel the revert
•#git revert –continue // to validate revert and writing commit message
Git merge
The merge keeps the commits history as it is. So the branch in which we had executed the merge
will be badly organized
• #git merge hotfix --squash
•#git status
•#git commit –m “commit encapsulating all hotfix commits branch”
Git: merge conflict
Let’s invoke a merge conflict to resolve it:
• #git checkout -b feature_db
• #echo "this is featuredb source code" > data_base.db
• #git add .
• #git commit -m "feature modif for db connection"
• #git checkout main
• #echo "this is main source code" > data_base.db
• #git status
• #git add .
• #git commit -m "main modif for db connection"
• #git merge feature_db
Auto-merging data_base.db
CONFLICT (add/add): Merge conflict in data_base.db
Automatic merge failed; fix conflicts and then commit the result.
Git: Resolving merge conflict
When you Open the data_base.db file you will find:
<<<<<<< HEAD
this is main source code
=======
this is featuredb source code
>>>>>>> feature_db
Keep the piece of code that you want to have in you main branch by erasing also
=======
<<<<<<< HEAD
>>>>>>> feature_db
• save your file
• #git add
• #git commit -m "resolving merge conflict“
• #git log --oneline --graph --decorate
Git: Resolving merge conflict
Merge Workflow to avoid resolving conflict in master branch
You have to continiously merge the master branch to resolve conflicts if it’s happen
in the feature branch not the main branch
Git: Rebase & Merge
• Git rebase vs merge, both actions can be used to combine file changes from one
branch to another.
• What is Git rebase? Rebase is an action in Git that allows you to rewrite commits from
one branch into another branch
• instead of creating a merge commit, rebase consists of rewriting the history of the
project by creating new commits for each commit of the original branch
Git: Rebase
#git rebase branchA branch
• Git will check and detect the last commit have both branches in commun
• Git has a look at the current branch (branchA) and see what changed actually in this
branch
• Git saves these changes (commits) internally
• Git go back to the other branch (branchB) and see what changes are applied after the
last common commit
• Git take these changes of the other branch (branch) and go back to the current branch
(branchA) to apply it on top of the commun commit
• Finally Git apply the changes that have been saved internally on the top
Git: Stashing