0% found this document useful (0 votes)
63 views51 pages

DevOps Cours Partie2

The document provides an overview of version control systems (VCS) and introduces Git and GitHub. It discusses centralized and distributed version control systems and how Git is a distributed VCS. It covers key Git concepts like repositories, branches, commits, diffs and logs. It also demonstrates how to set up Git and GitHub, create and manage repositories, branches and commits, and compare changes between commits. The document is intended to help readers understand the core concepts of Git and get started using it.

Uploaded by

chaima sakka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views51 pages

DevOps Cours Partie2

The document provides an overview of version control systems (VCS) and introduces Git and GitHub. It discusses centralized and distributed version control systems and how Git is a distributed VCS. It covers key Git concepts like repositories, branches, commits, diffs and logs. It also demonstrates how to set up Git and GitHub, create and manage repositories, branches and commits, and compare changes between commits. The document is intended to help readers understand the core concepts of Git and get started using it.

Uploaded by

chaima sakka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 51

GIT : Version Control System

TEK-UP
GIT & GITHub : Version Control System
➤ Why GIT?
➤ Understanding the Core Concept of GIT
➤ Features of GIT

➤ Master in GIT Commands


➤ Compare GIT Commands and Working Model
➤ How GIT play vital Role in DevOps
➤ Exposure to GITHub
➤ Bonus Materials

TEK-UP
VCS

• Version Control System - VCS helps a software team manage changes


to source code over time.

• Version control system keeps track of every modification of the code in a


special kind of database.

• VCS helps team to rollback to previous version in case of any issue with specific
Version.

• The need of Version Control System is primordial !!!

TEK-UP
VCS
Types of VCS
❖ Centralized Version Control System (CVCS)

❖ Distributed Version Control System (DVCS)

Centralized VCS – 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.

➤ Drawbacks - It is not locally available.


➤ Crash of CVCS will result in losing the entire data of the project.

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.

▪ User can update the Central Server from their Repo.

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

• GIT Repository contains Files, History, Config Managed by GIT.

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

• Remote repository is stored on a code hosting service like GitHub or on an internal


server. In order to be synchronised between multiple users and to keep the work
safe and secure.

• 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

GIT is open source and can be installed on all major os


• Windows
• Mac OS
• Linux
Link : https://git-scm.com/
For CentOS:
#sudo yum update
#sudo yum upgrade
#sudo yum install –y git
#git --version
Create Git project

#sudo mkdir /home/First_Git_Directory


#git init
#ls -al /home/First_Git_Directory/
Create Git project
Git stores the metadata and object database for the project in this .git directory
like:
1.Remote information (to which remote server your project is connected)
2.History of all local commits
3.Branch information (on which branch is your current project state (HEAD)
pointing to)
4.All logs of all local commits you have ever made (including revert changes)

• 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

#sudo touch file1.txt


#git status //Show the status of the working directory in the staging area
#ls -al /home/First_Git_Directory/
#git add file1.txt //to add file to the staging area
#git commit –m “my first file to commit” //keep your code updated with the latest
changes and capture the state of the project at that point of time
Git: Tracking and Logs

#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

#git log // show by default the last 10 commits


#git log –n // show the last n commits
# git log --oneline //show log commit prettier
# git log --author="email or name" // show the specific commit of the developer
GIT Configure User Information

#git help name_of_command


#git config --global user.name "name fmaily_name“
#git config --global user.email "[email protected]"
#git config --list
#git config --global color.ui auto
GIT Configure User Information
Why ?
• Just imagine a team of 100 developers modify on the same code and
working each of them in the same time.

🡺 Account configuration is needed for more Team visibility.

• 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.

To generate our personal token from github:


Settings > Developer settings > Personal access tokens
Generate a token to use it instead of the password
SETUP Authentication in local with GitHub
SSH Connection steps:
1. Open the github settings> ssh and gpg keys
2. Open your terminal and execute the command
• # ssh-keygen -t ed25519
• Hit enter to create your ssh key
• Copy public key and paste it in github ssh key creation
3. #git clone [email protected]:tekup2021/ssh_repo.git
4. #echo 'hello ssh repo' >> file1.txt
5. #git add .
6. #git commit -m "my first file in ssh repo connection with github"
7. #git push
Git: Concept of branches

ull
Push/p
Dev3(feature y)
Dev1 (feature1)

Dev2 (feature n)
Dev4 (feature x)

Dev1 (feature2)

Production branch (main)


Git: Concept of branches
• A branch represents an independent line of development.
• It's important to understand that branches are just pointers to commits.
When you create a branch, all Git needs to do is create a new pointer, it
doesn’t change the repository in any other way
• you start with a repository that looks like this:
Git: Concept of branches
• Then, you create a branch:
The repository history remains unchanged. All you get is a new pointer to the
current commit
Git: Concept of branches
• To list all branches
• #git branch –a
• To create new branch
• #git branch branch_name
• To rename branch
• #git branch –m old_name new_name
• To delete branch
• #git branch –d branch_name
• To switch to specific branch
• #git checkout branch_name
Git: Concept of branches
Manipulating branch steps:
1. #git branch
2. #git branch feature1
3. #git branch
4. #git checkout feature1
5. #git branch
• To create branch and switch directly into it you can use
#git checkout –b feature2
6. Try to create different files into these branches and compare it with main branch
• For a new branch we have to push using #git push origin feature
• Before pulling this branch from remote #git branch --set-upstream-to=origin/feature
Git: Compare
To compare working directory with staging area after modification:
•# echo 'diff newline' >> file_name
•# git status
•#git diff file_name //to see what modification is added or deleted
To compare commits in git
• #git log –oneline
• #git diff older_commit newer_commit //to detect exactly what is recently added to
older_commit
• #git diff commitA commitB –name-only //to list just the name of files impacted
Git: Compare
To compare working directory with staging area after modification:
•# echo 'diff newline' >> file_name
•# git status
•#git diff file_name //to see what modification is added or deleted
To compare commits in git
• #git log –oneline
• #git diff older_commit newer_commit //to detect exactly what is recently added to
older_commit
• #git diff commitA commitB –name-only //to list just the name of files impacted
Git: Undoing Changes

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

• Open github ui and go to the section pull request to apply


your merge
• Or do it with git commande
• #git checkout main
• #git merge feature
Git merge
• Merging is a common practice for developers. Whether branches are created for
testing, bug fixes, or other reasons, merging is commiting changes to another
branch. It takes the contents of a source branch and integrates it with a target
branch.

• Simply it integrates changes from one branch into another.


Git merge
To merge branches follow the steps:
•#git checkout –b hotfix
•#echo ‘resolve production problem’ >> hotfix.txt
•#git add .
•#git commit –m “this is correction in the hotfix branch”
•Switch to the other main branch and do 2 commits
•In the same main branch merge the hotfix modification into main source code :
• #git merge hotfix
•#git log –oneline –graph --decorate
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

#git stash // just saves the state of staging area


#git stash –a // saves state of staging area and untracked area
#git stash list //show stashes
#git stash apply //applies last stash and saved in stash
#git stash pop //aplie but erase it from saved stashes
Git:
Cherry-pick
Cherry-pick is the copy of a useful specific commit from branch to
another

#git Cherry-pick id_of_commit_of_the_branch_we_want_to_copy_from

You might also like