DevOps Chapter 2

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 31

Sinhgad Technical Education Society’s

Sinhgad Institute of Management & Computer Application

DevOps
IT-41
DevOps

Chapter-2

Version Control Git

Dr. Poonam Sawant


Topic Content

2.1. Introduction to GIT


2.2. What is Git
2.3. About Version Control System and Types
2.4. Difference between CVCS and DVCS
2.5. A short history of GIT
2.6. GIT Basics
2.7. GIT Command Line
2.8. Installing Git
2.9. Installing on Linux
2.10. Installing on Windows
2.11. Initial setup
2.12. Git Essentials
2.13. Creating repository
2.14. Cloning, check-in and committing
2.15. Fetch pull and remote
2.16. Branching
2.17. Creating the Branches, switching the branches, merging
2.18. The branches.
What is Git ?

• Git is a DevOps tool used for source code management.


• It is a free and open-source version control system used to handle small to very large
projects efficiently.
• Git is used to tracking changes in the source code, enabling multiple developers to work
together on non-linear development.

Version Control System: The code which is stored in Git keeps changing as more code is added.
Also, many developers can add code in parallel. So Version Control System is a software helps in
handling this by maintaining a history of what changes have happened.

• Allows developers to work simultaneously.


• Does not allow overwriting each other’s changes, trace each change with msg.
• Maintains a history of every version.
Types of Version Control

There are two types of version control: centralized and distributed.

Centralized version control System:

• Centralized version control systems, have a single “central” copy of your project on a server .
• It commit changes to this central copy.
• You can pull the files that you need, but you never have a full copy of your project locally. 
• Some of the most common version control systems are centralized, including Subversion
(SVN) and Perforce.
Types of Version Control

Distributed version control System:

• In distributed version control systems (DVCS), we don‘t rely on a central server to store all the
versions of a project’s files.
• Instead, we clone a copy of a repository locally so that you have the full history of the project. 
• Two common distributed version control systems are Git and Mercurial.
Difference between CVCS and DVCS

CVCS DVCS
CVCS is dependent on the access to the server DVCS provides the benefits to work offline
CVCS is easy to administrate and has more CVCS is difficult to administrate
control over users and access
CVCS is comparatively slow as you have to DVCS is comparatively fast as you don’t have to
contact the central server for every command. contact the central server for every command.
CVCS allows you to checkout only few lines of Downloading the entire history can take an
code if you just need to work on few modules. unreasonable amount of time and disk space in
DVCS

SVN and CVS are the popular tools of CVCS GIT and Mercurial are the popular tools of
DVCS
History of Gits

• During the early years of the Linux kernel maintenance (1991–2002), changes to the software
were passed around as patches and archived files.

• In 2002, the Linux kernel project began using a proprietary DVCS called BitKeeper.

• In 2005, the relationship between the community that developed the Linux kernel and the
commercial company that developed BitKeeper broke down, and the tool’s free-of-charge
status was revoked.

• This prompted the Linux development community to develop their own tool based on some of
the lessons they learned while using BitKeeper.

• Some of the goals of the new system were as follows:


• Speed
• Simple design
• Strong support for non-linear development (thousands of parallel branches)
• Fully distributed
• Able to handle large projects like the Linux kernel efficiently (speed and data size)

• Since its birth in 2005, Git has evolved and matured to be easy to use and yet retain these
initial qualities.
Characteristics of Git

• Strong support for non-linear development


• Distributed development
• Compatibility with existing systems/protocol
• Efficient handling of large projects
• Data Assurance
• Automatic Garbage Collection
• Periodic explicit object packing
Basics of Git
Repository: Users can perform many operations with this repository such as add file, remove file,
rename file, move file, commit changes, and many more

Working Directory and staging area: The working directory is the place where files are checked
out And Staging area stores modified files

Suppose you modified two files, namely “sort.c” and “search.c” and you want two different
commits for each operation. You can add one file in the staging area and do commit. After the
first commit, repeat the same procedure for another file.

Basic workflow of Git

Step 1 − You modify a file from the working directory.

Step 2 − You add these files to the staging area.

Step 3 − You perform commit operation that moves


the files from the staging area. After push operation,
it stores the changes permanently to the Git
repository.
Basics of Git
Blobs

• Blob stands for Binary Large Object.


• Each version of a file is represented by blob.
• A blob holds the file data but doesn’t contain any metadata about the file.
• In Git, files are not addressed by names. Everything is content-addressed.

Trees
• Tree is an object, which represents a directory.
• It holds blobs as well as other sub-directories.
• A tree is a binary file that stores references to blobs.

Commits
• Commit holds the current state of the repository.
• You can consider a commit object as a node of the linked list.
• Every commit object has a pointer to the parent commit object.
• From a given commit, you can traverse back by looking at the parent pointer to view the
history of the commit.
• If a commit has multiple parent commits, then that particular commit has been created by
merging two branches.

Blobs, commit and trees are named by SHA1 hash.


Basics of Git
Branches
• Branches are used to create another line of development.
• By default, Git has a master branch, which is same as trunk in Subversion.
• Usually, a branch is created to work on a new feature.
• Once the feature is completed, it is merged back with the master branch and we delete the
branch.
• Every branch is referenced by HEAD, which points to the latest commit in the branch.
• Whenever you make a commit, HEAD is updated with the latest commit.

Tags
• Tag assigns a meaningful name with a specific version in the repository.
• Tags are very similar to branches, but the difference is that tags are immutable.
• It means, tag is a branch, which nobody intends to modify.
• Once a tag is created for a particular commit, even if you create a new commit, it will not be
updated.
• Usually, developers create tags for product releases.

Clone
• Clone operation creates the instance of the repository.
• Clone operation not only checks out the working copy, but it also mirrors the complete
repository.
• Users can perform many operations with this local repository.
• The only time networking gets involved is when the repository instances are being
synchronized.
Basics of Git
Pull
• Pull operation copies the changes from a remote repository instance to a local one.
• The pull operation is used for synchronization between two repository instances.
• This is same as the update operation in Subversion.

Push
• Push operation copies changes from a local repository instance to a remote one.
• This is used to store the changes permanently into the Git repository.
• This is same as the commit operation in Subversion.

HEAD
• HEAD is a pointer, which always points to the latest commit in the branch.
• Whenever you make a commit, HEAD is updated with the latest commit.
• The heads of the branches are stored in .git/refs/heads/ directory.

Revision
• Revision represents the version of the source code.
• Revisions in Git are represented by commits.
• These commits are identified by SHA1 secure hashes.

URL
• URL represents the location of the Git repository.
• Git URL is stored in config file.
Git Commandline
There are a lot of different ways to use Git. E.g. original command-line tools and graphical user
interfaces of varying capabilities.

The command line is the only place you can run all Git commands.

Most of the GUIs implement only a partial subset of Git functionality for simplicity.

Git Commands

Following are the  Git commands which are being covered:

git config, git init, git clone, git add, git commit, git diff, git reset, git status, git rm
git log, git show, git tag, git branch, git checkout, git merge, git remote, git push, git pull
git stash
Git Commands
git config: This command sets the author name and email address respectively to be used with your commits.

Usage: git config –global user.name “[name]”


Usage: git config –global user.email “[email address]”

git init: This command is used to start a new repository.


Usage: git init [repository name]

git clone: This command is used to obtain a repository from an existing URL.
Usage: git clone [url]
Git Commands
git add: This command adds a file to the staging area.

Usage: git add [file]

Usage: git add *


This command adds one or more to the staging area.

git commit
Usage: git commit -m “[ Type in the commit message]”
This command records or snapshots the file permanently in the version history.
Git Commands

Usage: git commit -a


This command commits any files you’ve added with the git add command and also commits any files you’ve changed since then.

git diff
Usage: git diff
This command shows the file differences which are not yet staged.
Git Commands

Usage: git diff –staged


This command shows the differences between the files in the staging area and the latest version present.

Usage: git diff [first branch] [second branch]


This command shows the differences between the two branches mentioned.
Git Commands

git reset
Usage: git reset [file]
This command unstages the file, but it preserves the file contents.

Usage: git reset [commit]


This command undoes all the commits after the specified commit and preserves the changes locally.

Usage: git reset –hard [commit]


This command discards all history and goes back to the specified commit.
Git Commands

git status
Usage: git status
This command lists all the files that have to be committed.

git rm
Usage: git rm [file]
This command deletes the file from your working directory and stages the deletion.
Git Commands

git log
Usage: git log
This command is used to list the version history for the current branch.
Git Commands

Usage: git log –follow[file]


This command lists version history for a file, including the renaming of files also.
Git Commands

git show
Usage: git show [commit]
This command shows the metadata and content changes of the specified commit.
Git Commands

git tag
Usage: git tag [commitID]
This command is used to give tags to the specified commit.

git branch
Usage: git branch
This command lists all the local branches in the current repository.

Usage: git branch [branch name]


This command creates a new branch.
Git Commands

Usage: git branch -d [branch name]


This command deletes the feature branch.

git checkout
Usage: git checkout [branch name]
This command is used to switch from one branch to another.

Usage: git checkout -b [branch name]


This command creates a new branch and also switches to it.
Git Commands

git merge
Usage: git merge [branch name]
This command merges the specified branch’s history into the current branch.

git remote
Usage: git remote add [variable name] [Remote Server Link]
This command is used to connect your local repository to the remote server.

git push
Usage: git push [variable name] master
This command sends the committed changes of master branch to your remote repository.
Git Commands

Usage: git push [variable name] [branch]


This command sends the branch commits to your remote repository.

Usage: git push –all [variable name]


This command pushes all branches to your remote repository.

Usage: git push [variable name] :[branch name]


This command deletes a branch on your remote repository.
Git Commands
git pull
Usage:  git pull [Repository Link]
This command fetches and merges changes on the remote server to your working directory.

git stash
Usage: git stash save
This command temporarily stores all the modified tracked files.

Usage: git stash pop


This command restores the most recently stashed files.
Git Commands

Usage: git stash list


This command lists all stashed changesets.

Usage: git stash drop


This command discards the most recently stashed changeset.
References

• https://git-scm.com/book/en/v2/Getting-Started-Installing-Git
• https://git-scm.com/book/en/v2/Getting-Started-A-Short-History-of-Git
• https://www.geeksforgeeks.org/history-of-git/
• https://devopsbuzz.com/centralized-vs-distributed-version-control-systems/#:~:tex
t=CVCS%20is%20dependent%20on%20the,is%20server%20from%20one%20place
.
• https://support.atlassian.com/bitbucket-cloud/docs/types-of-version-control/
• https://www.tutorialspoint.com/git/git_basic_concepts.htm
Thank You

You might also like