Version Control [ Git ]
AGENDA
What is Version Control?
Types of Version Control Systems
Introduction to Git
Git Lifecycle
How Does Git Work?
Common Git Commands
Merging Branches
WHAT IS VERSION
CONTROL?
© Copyright. All Rights Reserved.
WHAT IS VERSION CONTROL?
Version control is a system that records/manages changes to documents,
computer programs etc over time. It helps us tracking changes when multiple
people work on the same project
© Copyright. All Rights Reserved.
PROBLEMS BEFORE VERSION CONTROL
Versioning was Manual
Team Collaboration was a time
consuming and hectic task
No easy access to previous versions
Multiple Version took a lot of space
© Copyright. All Rights Reserved.
ADVANTAGES OF VERSION CONTROL
Versioning is Automatic
Team Collaboration is simple
Easy Access to previous Versions
Only modified code is stored across
different versions, hence saves storage
© Copyright. All Rights Reserved.
TYPES OF VERSION
CONTROL SYSTEM
© Copyright. All Rights Reserved.
TYPES OF VERSION CONTROL SYSTEM
Version Control System
Central VCS Distributed VCS
Remote Repo Remote Repo
Local Repo Local Repo Local Repo
© Copyright. All Rights Reserved.
CENTRALIZED VERSION CONTROL SYSTEM
Centralized Version Control System has one
single copy of code in the central server
Remote Repo
Developers will have to “commit” their
changes in the code to this central server
“Committing” a change simply means
recording the change in the central system
Centralized VCS
© Copyright. All Rights Reserved.
DISTRIBUTED VERSION CONTROL SYSTEM
In Distributed VCS, one does not necessarily rely on a
Remote Repo central server to store all the versions of a project’s
file
Every developer “clones” a copy of the main
repository on their local system
Local Repo Local Repo Local Repo
This also copies, all the past versions of the code on
the local system too
Therefore, the developer need not be connected to
the internet to work on the code
Distributed VCS
© Copyright. All Rights Reserved.
EXAMPLES OF CVCS
EXAMPLES OF DVCS
INTRODUCTION TO GIT
© Copyright. All Rights Reserved.
WHY GIT?
Git is the most popular tool among all the DVCS tools.
© Copyright. All Rights Reserved.
What is Git?
Git is a version-control system for tracking changes in computer files and coordinating work on those
files among multiple people. It is primarily used for source-code management in software
development, but it can be used to keep track of changes in any set of files.
© Copyright. All Rights Reserved.
GIT
LIFECYCLE
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Following are the lifecycle stages of files in Git
Working Staging
Commit
Directory Area
© Copyright. All Rights Reserved.
GIT LIFECYCLE
The place where your project resides in your local disk
Working Directory
This project may or may not be tracked by git
Staging Area In either case, the directory is called the working directory
The project can be tracked by git, by using the command git init
Commit
By doing git init, it automatically creates a hidden .git folder
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Once we are in the working directory, we have to specify which files
Working Directory are to be tracked by git
We do not specify all files to be tracked in git, because some files
Staging Area could be temporary data which is being generated while execution
To add files in the staging area, we use the command git add
Commit
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Once the files are selected and are ready in the staging area, they can
now be saved in repository
Working Directory
Saving a file in the repository of git is known as doing a commit
Staging Area
When we commit a repository in git, the commit is identified by a
commit id
Commit The command for initializing this process is git commit –m “message”
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Program Workspace Git initialized
Working Directory
Staged files in the
working directory for
commit
Files committed to
git repository
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Program Workspace Git initialized
Working Directory
Staged files in the
working directory for
commit
Files committed to
git repository
How do we collaborate with the team?
© Copyright. All Rights Reserved.
GIT LIFECYCLE
Program Workspace Git initialized
Working Directory
Staged files in the
working directory for
commit
GitHub Files committed to
Team
git repository
Collaboration
Once the files are committed, they can be pushed to a remote
repository such as GitHub
© Copyright. All Rights Reserved.
HOW DOES
GIT WORK?
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
Any project which is saved on git, is saved using a commit. The commit is
identified using a commit ID.
Commit ID: 00001
Project Folder
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
When we edit the project or add any new functionality, the new code is again
committed to git, a new commit ID is assigned to this modified project. The older
code is stored by git, and will be accessible by it’s assigned Commit ID
Commit ID: 00002
Commit ID: 00001
Project Folder
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
All these commits are bound to a branch. Any new commits made will be added
to this branch. A branch always points to the latest commit. The pointer to the
latest commit is known as HEAD
HEAD
Commit ID: 00001 Commit ID: 00002
Project Folder
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
The default branch in a git repository is called the Master Branch
Master Branch
Commit ID: 00001 Commit ID: 00002
Project Folder
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
The default branch in a git repository is called the Master Branch
Master Branch
Commit ID: 00001 Commit ID: 00002
Project Folder But, why do we need a branch?
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
Say, a developer has been assigned enhance this code by adding Feature A. The code is assigned to this
developer in a separate branch “Feature A”. This is done, so that master contains only the code which is
finished, finalized and is on production
Master Branch
Commi t ID: Commi t ID:
00001 00002
Feature A Branch
Commi t ID:
00002
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
Therefore, no matter how many commits are made by this developer on Feature A branch, it will not
affect the Master Branch.
Master Branch
Commi t ID: Commi t ID:
00001 00002
Feature A Branch
Commi t ID: Commi t ID:
00002 00004
© Copyright. All Rights Reserved.
HOW DOES GIT WORK?
Once the code is finished, tested and ready we can merge the Feature A branch, with the master branch
and now the code is available on the production servers as well
Master Branch
Commi t ID: Commi t ID: Commi t ID:
00001 00002 00004
Feature A Branch
Commi t ID: Commi t ID:
00002 00004
© Copyright. All Rights Reserved.
COMMON GIT
COMMANDS
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS
You can do the following tasks, when working with git. Let us explore the commands related
to each of these tasks
Creating Repository Making Changes
Parallel Development Syncing Repositories
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT INIT
You can create a repository using the command git init. Navigate to your
project folder and enter the command git init to initialize a git repository
for your project on the local system
Creating Repository
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT STATUS
Once the directory has been initialized you can check the status of the
files, whether they are being tracked by git or not, using the command
git status
Creating Repository
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT ADD
Since no files are being tracked right now, let us now stage these files.
For that, enter the command git add. If we want to track all the files in
the project folder, we can type the command,
Creating Repository
git add .
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT COMMIT
Once the files or changes have been staged, we are ready to commit
them in our repository. We can commit the files using the command
git commit –m “custom message”
Creating Repository
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT REMOTE
Once everything is ready on our local, we can start pushing our changes
to the remote repository. Copy your repository link and paste it in the
command
Creating Repository
git remote add origin “<URL to repository>”
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT PUSH
To push the changes to your repository, enter the command
git push origin <branch-name> and hit enter. In our case the branch is master, hence
git push origin master
Creating Repository This command will then prompt for username and password, enter the values and
hit enter.
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT PUSH
Your local repository is now synced with the remote repository on
github
Creating Repository
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT CLONE
Similarly, if we want to download the remote repository to our local system, we can
use the command:
git clone <URL>
Creating Repository This command will create a folder with the repository name, and download all the
contents of the repository inside this folder. In our example, repository contents
were downloaded into the ”devops” folder.
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT PULL
The git pull command is also used for pulling the latest changes from the repository,
unlike git clone, this command can only work inside an initialized git repository. This
command is used when you are already working in the cloned repository, and want
Creating Repository to pull the latest changes, that others might have pushed to the remote repository
git pull <URL of link>
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT BRANCH
Until now, we saw how you can work on git. But now imagine, multiple developers
working on the same project or repository. To handle the workspace of multiple
developers, we use branches. To create a branch from an existing branch, we type
Creating Repository
git branch <name-of-new-branch>
Similarly, to delete a branch use the command
Making Changes
git branch –D <branch name>
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT CHECKOUT
To switch to the new branch, we type the command
git checkout <branch-name>
Creating Repository
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT LOG
Want to check the log for every commit detail in your repository?
You can accomplish that using the command
git log
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT STASH
Want to save your work without committing the code? Git has got you covered.
This can be helpful when you want to switch branches, but do not want to save
your work to your git repository. To stash your staged files without committing just
Creating Repository type in git stash. If you want to stash your untracked files as well, type git stash –u.
Once you are back and want to retrieve working, type in git stash pop
Making Changes
Syncing Repositories
Parallel Development
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT REVERT
This command helps you in reverting a commit, to a previous version
git revert <commit-id>
<commit-id> can be obtained from the output of git log
© Copyright. All Rights Reserved.
COMMON GIT COMMANDS – GIT DIFF
This command helps us in checking the differences between two versions of a file
git diff <commit-id of version x> <commit-id of version y>
<commit-id> can be obtained from the output of git log
© Copyright. All Rights Reserved.
MERGING
BRANCHES
© Copyright. All Rights Reserved.
MERGING BRANCHES
Once the developer has finished his code/feature on his branch, the code will have to
be combined with the master branch. This can be done using two ways:
Git Merge
Git Rebase
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT MERGE
If you want to apply changes from one branch
to another branch, one can use merge
command
Should be used on remote branches, since
Git Merge history does not change
Creates a new commit, which is a merger of
the two branches
Git Rebase
Syntax: git merge <source-branch>
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT MERGE
Imagine, you have a Master branch and a Feature A branch.
The developer has finished his/her work in the feature A
branch and wants to merge his work in the master.
Git Merge Master
Feature A
Git Rebase
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT MERGE
If he is using git merge, a new commit will be created, which
will have the changes of Feature A and Master branch
combined.
Any new commits to the Feature branch will be isolated from
the master branch
Git Merge
Master
Git Rebase
Feature A
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT MERGE
This command can be executed using the syntax
git merge <source-branch-name>
Git Merge
Git Rebase
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT MERGE
The history of the branch will look something like this, if we are using
git merge
Git Merge
Git Rebase
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT REBASE
This is an alternative to git merge command
Should be used on local branches, since
history does change and will be confusing for
other team members
Git Merge
Does not create any new commit, and results
in a cleaner history
The history is based on common commit of
Git Rebase the two branches (base)
The destination’s branch commit is pulled
from it’s “base” and “rebased” on to the latest
commit on the source branch
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT REBASE
Imagine, you have a Master branch and a test branch(local
branch)
The developer has finished his/her work in the test branch
Git Merge But the master moved forward, while the code was being
developed
Code being developed is related to the new commit added in
master
Git Rebase
Master
Test
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT REBASE
Therefore you want all the changes from master in feature.
Since, it is a local branch, you would want a cleaner or linear
history, you decide to use git rebase
Git Merge
Syntax: git rebase <source branch>
Git Rebase
Master
Test
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT REBASE
This is how the output looks like:
Git Merge
Git Rebase
© Copyright. All Rights Reserved.
MERGING BRANCHES – GIT REBASE
This is how the commits look like, after a rebase. The commit
was “rebased” from the first commit to the next commit
Test
Git Merge
Test
And looking at the history we can clearly see, it’s a clean
Git Rebase linear history, without any branches
© Copyright. All Rights Reserved.
© Copyright. All Rights Reserved.