Version Control System
Version Control System
TRADE: SOFTWARE
DEVELOPMENT
MODULE CODE:SWDVC301
TEACHER’S GUIDE
Module name: CONDUCT VERSION CONTROL
1
Indicative contents
1.1 Introduction to version control
1.2 Description of git
1.3 Use of GitHub repository
1
Introduction
Let's imagine a company operating in many countries with many employees, in order to work
they need to come to work in different departments. What happened during the COVID 19
pandemic? Are employees still working as usual? We all know it's not possible, because
many companies were forced to close their doors because of the stay-at-home order. How
would the company's employees continue to work and be aware of the changes in their
company? How would they know about the changes in their institution? How would they
keep up with the past?
There are different problems that may be raised, among them we can cite the followings:
Lack of collaboration
Storing versions
Restoring previous version once needed
Figuring out what happened in different branches
Problem of backup
In summary Is the reason why we need a version control system as developers in order to
overcome those cited issues.
INTRODUCTION TO VERSION CONTROL
With centralized version control systems, you have a single “central” copy of your project on
a server and commit your changes to this central copy.
A centralized version control system offers software development teams a way to collaborate
using a central server. In a centralized version control system (CVCS), a server acts as the
main repository which stores every version of code.
You 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.
2
Figure 1 Centralised Version Control
With distributed version control systems (DVCS), you don't rely on a central server to store
all the versions of a project’s files. Instead, you 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.
While you don't have to have a central repository for your files, you may want one "central"
place to keep your code so that you can share and collaborate on your project with others.
That's where Bitbucket comes in. Keep a copy of your code in a repository on Bitbucket so
that you and your teammates can use Git or Mercurial locally and to push and pull code.
A distributed version control system (DVCS) is a type of version control where the complete
codebase — including its full version history — is mirrored on every developer's computer.
It's abbreviated DVCS. Changes to files are tracked between computers.
3
Figure 2 Distrubuted Vesrion Control
4
5
Repository
A Git repository tracks and saves the history of all changes made to the files in a Git project.
It saves this data in a directory called.git, also known as the repository folder.
Git uses a version control system to track all changes made to the project and save them in
the repository. Users can then delete or copy existing repositories or create new ones for
ongoing projects.
Bare Repositories
Software development teams use bare repositories to share changes made by team members.
Individual users aren't allowed to modify or create new versions of the repository.
Non-Bare Repositories
With non-bare repositories, users can modify the existing repository and create new
versions. By default, the cloning process creates a non-bare repository.
Version control
Version control is a system that records changes to a file or set of files over time so that you
can recall specific versions later.
For the examples in this book, you will use software source code as the files being version
controlled, though in reality you can do this with nearly any type of file on a computer.
If you are a graphic or web designer and want to keep every version of an image or layout
(which you would most certainly want to), a Version Control System (VCS) is a very wise
thing to use. It allows you to revert selected files back to a previous state, revert the entire
project back to a previous state, compare changes over time, see who last modified something
that might be causing a problem, who introduced an issue and when, and more. Using a VCS
also generally means that if you screw things up or lose files, you can easily recover. In
addition, you get all this for very little overhead.
Git
Git tracks the changes you make to files, so you have a record of what has been done, and
you can revert to specific versions should you ever need to.
Git also makes collaboration easier, allowing changes by multiple people to all be merged
into one source.
1
So regardless of whether you write code that only you will see, or work as part of a team, Git
will be useful for you.
GitHub:
GitHub is a code-hosting platform for version control and collaboration. It lets you and others
work together on projects from anywhere.
GitHub is an online software development platform used for storing, tracking, and
collaborating on software projects.
GitHub is a Git repository hosting service that provides a web-based graphical interface.
Terminal:
The terminal is an interface that lets you access the command line(Bash and CMD).
Bash (Bourne Again Shell) is the free and enhanced version of the Bourne shell distributed
with Linux and GNU operating systems. Bash is similar to the original, but has added
features such as command-line editing.
CMD is an acronym for Command. Command prompt, or CMD, is the command-line
interpreter of Windows operating systems. It is similar to Command.com used in DOS and
Windows 9x systems called “MS-DOS Prompt”. It is analogous to Unix Shells used on Unix
like system.
The command prompt is a native application of the Windows operating system and gives the
user an option to perform operations using commands.
In group of three open and test if the command prompt of your computer is
working.
2
Points to Remember
Version control is a system that records changes to a file or set of files over time so
that you can recall specific versions later.
We have two main types of version control system local and remote.
For local version control we use git weather for remote version control we use GitHub
or GitLab.
You can run git commands by using CMD.
Branch
Clone
The git clone command is used to create a copy of a specific repository or branch within a
repository.
Pull
The git pull command is used to fetch and download content from a remote repository and
immediately update the local repository to match that content.
Push
The git push command is used to upload local repository content to a remote repository.
Pushing is how you transfer commits from your local repository to a remote repo. It's the
counterpart to git fetch , but whereas fetching imports commits to local branches, pushing
exports commits to remote branches.
Commit
It is used to record the changes in the repository. It is the next command after the git add.
Every commit contains the index data and the commit message
Initialisation
The git init command creates a new Git repository. It can be used to convert an existing,
unversioned project to a Git repository or initialize a new, empty repository. Most other Git
3
commands are not available outside of an initialized repository, so this is usually the first
command you'll run in a new project.
The git init command is the first command that you will run on Git. The git init command is
used to create a new blank repository.
Git architecture
Many VCS’s use a two-tier architecture i.e a repository and a working copy. Git uses three-
tier architecture i.e a working directory, staging area and local repository. The three stages of
git can store different(or the same) states of the same code in each stage.
Git workflow
A Git workflow is a recipe or recommendation for how to use Git to accomplish work in
a consistent and productive manner. Git workflows encourage developers and DevOps
teams to leverage Git effectively and consistently. Git offers a lot of flexibility in how users
manage changes.
INITIALISATION OF GIT
git init
This command turns a directory into an empty Git repository. This is the first step in creating
a repository. After running git init, adding and committing files/directories is possible.
git add
4
Adds files in the to the staging area for Git. Before a file is available to commit to a
repository, the file needs to be added to the Git index (staging area). There are a few different
ways to use git add, by adding entire directories, specific files, or all unstaged files.
git commit
Record the changes made to the files to a local repository. For easy reference, each commit
has a unique ID.
git status
git status will return the current working branch. If a file is in the staging area, but not
committed, it shows with git status. Or, if there are no changes it’ll return nothing to commit,
working directory clean.
git config
With Git, there are many configurations and settings possible. git config is how to assign
these settings. Two important settings are user user.name and user.email. These values set
what email address and name commits will be from on a local computer. With git config, a --
global flag is used to write the settings to all repositories on a computer. Without a --
global flag settings will only apply to the current repository that you are currently in.
git branch
To determine what branch the local repository is on, add a new branch, or delete a branch.
git checkout
git merge
Integrate branches together. git merge combines the changes from one branch to another
branch. For example, merge the changes made in a staging branch into the stable branch.
git remote
5
To connect a local repository with a remote repository. A remote repository can have a name
set to avoid having to remember the URL of the repository.
git clone
To create a local working copy of an existing remote repository, use git clone to copy and
download the repository to a computer. Cloning is the equivalent of git init when working
with a remote repository. Git will create a directory locally with all files and repository
history.
git pull
To get the latest version of a repository run git pull. This pulls the changes from the remote
repository to the local computer.
git push
Sends local commits to the remote repository. git push requires two parameters: the remote
repository and the branch that the push is for.
git log
To show the chronological commit history for a repository. This helps give context and
history for a repository. git log is available immediately on a recently cloned repository to see
history.
git rm
Remove files or directories from the working index (staging area). With git rm, there are two
options to keep in mind: force and cached. Running the command with force deletes the file.
The cached command removes the file from the working index. When removing an entire
directory, a recursive command is necessary.
To use Git, you have to install it on your computer. Even if you have already installed Git, it's
probably a good idea to upgrade it to the latest version. You can either install it as a package
or via another installer or download it from its official site.
Now the question arises that how to download the Git installer package. Below is the
stepwise installation process that helps you to download and install the Git.
6
CONFIGURE GIT
Syntax
The init command will initialize an empty repository. See the below screenshot.
This command configures the user. The Git config command is the first and necessary
command used on the Git command line. This command sets the author name and email
address to be used with your commits. Git config is also used in other scenarios.
$ git config --global user.name "ImDwivedi1" $ git config --global user.email "Himanshudu
[email protected]"
Open the command prompt "terminal" and type git version to verify Git was installed.
Description of GitHub
GitHub is a Git repository hosting service. GitHub also facilitates with many of its features,
such as access control and collaboration. It provides a Web-based graphical interface.
7
GitHub is an American company. It hosts source code of your project in the form of different
programming languages and keeps track of the various changes made by programmers.
It offers both distributed version control and source code management (SCM)
functionality of Git. It also facilitates with some collaboration features such as bug tracking,
feature requests, task management for every project.
Collaboration
Integrated issue and bug tracking
Graphical representation of branches
Git repositories hosting
Project management
Team management
Code hosting
Track and assign tasks
Conversations
Benefits of GitHub
GitHub can be separated as the Git and the Hub. GitHub service includes access controls as
well as collaboration features like task management, repository hosting, and team
management.
8
The key benefits of GitHub are as follows.
If you don't already have a GitHub account, here's how to create one.
You have two options to create a Git repo. You can create one from the code in a folder on a
computer, or clone one from an existing repo. If working with code that's just on the local
computer, create a local repo using the code in that folder. But most of the time the code is
already shared in a Git repo, so cloning the existing repo to the local computer is the
recommended way to go.
9
Once creating a remote repository, you can use GitHub interface by following those steps.
Step 1
Usage Examples
In its simplest (and most common) form, only the repository URL is specified:
cd folder/to/clone-into/
git clone https://github.com/gittower/git-crash-course.git
This will download the project to a folder named after the Git repository ("git-crash-course"
in this case). If you want a different folder name, simply specify it as the last parameter:
Git remote
In Git, the term remote is concerned with the remote repository. It is a shared repository that
all team members use to exchange their changes. A remote repository is stored on a code
hosting service like an internal server, GitHub, Subversion, and more. In the case of a local
repository, a remote typically does not provide a file tree of the project's current state; as an
alternative, it only consists of the .git versioning data.
The developers can perform many operations with the remote server. These operations can be
a clone, fetch, push, pull, and more. Consider the below image:
10
Figure 4 Git Remote
To check the configuration of the remote server, run the git remote command. The git remote
command allows accessing the connection between remote and local. If you want to see the
original existence of your cloned repository, use the git remote command. It can be used as:
Syntax:
1. $ git remote
Output:
The given command is providing the remote name as the origin. Origin is the default name
for the remote server, which is given by Git.
11
Git remote supports a specific option -v to show the URLs that Git has stored as a short name.
These short names are used during the reading and write operation. Here, -v stands for
verbose. We can use --verbose in place of -v. It is used as:
Syntax:
1. $ git remote -v
Or
Output:
The above output is providing available remote connections. If a repository contains more
than one remote connection, this command will list them all.
When we fetch a repository implicitly, git adds a remote for the repository. Also, we can
explicitly add a remote for a repository. We can add a remote as a shot nickname or short
name. To add remote as a short name, follow the below command:
Syntax:
Output:
In the above output, I have added a remote repository with an existing repository as a short
name "hd". Now, you can use "hd" on the command line in place of the whole URL. For
12
example, you want to pull the repository, consider below output:
I have pulled a repository using its short name instead of its remote URL. Now, the repository
master branch can be accessed through a short name.
You can fetch and pull data from the remote repository. The fetch and pull command goes out
to that remote server, and fetch all the data from that remote project that you don't have yet.
These commands let us fetch the references to all the branches from that remote.
To fetch the data from your remote projects, run the below command:
To clone the remote repository from your remote projects, run the below command:
1. $ git clone<remote>
When we clone a repository, the remote repository is added by a default name "origin." So,
mostly, the command is used as git fetch origin.
The git fetch origin fetches the updates that have been made to the remote server since you
cloned it. The git fetch command only downloads the data to the local repository; it doesn't
merge or modify the data until you don't operate. You have to merge it manually into your
repository when you want.
The git pull command automatically fetches and then merges the remote data into your current
13
branch. Pulling is an easier and comfortable workflow than fetching. Because the git clone
command sets up your local master branch to track the remote master branch on the server
you cloned.
If you want to share your project, you have to push it upstream. The git push command is used
to share a project or send updates to the remote server. It is used as:
To update the main branch of the project, use the below command:
It is a special command-line utility that specifies the remote branch and directory. When you
have multiple branches on a remote server, then this command assists you to specify your
main branch and repository.
Generally, the term origin stands for the remote repository, and master is considered as the
main branch. So, the entire statement "git push origin master" pushed the local content on
the master branch of the remote location.
You can remove a remote connection from a repository. To remove a connection, perform the
git remote command with remove or rm option. It can be done as:
Syntax:
Or
Suppose you are connected with a default remote server "origin." To check the remote
verbosely, perform the below command:
14
1. $ git remote -v
Output:
The above output will list the available remote server. Now, perform the remove operation as
mentioned above. Consider the below output:
In the above output, I have removed remote server "origin" from my repository.
Git allows renaming the remote server name so that you can use a short name in place of the
remote server name. Below command is used to rename the remote server:
Syntax:
Output:
In the above output, I have renamed my default server name origin to hd. Now, I can operate
using this name in place of origin. Consider the below output:
15
In the above output, I have pulled the remote repository using the server name hd. But, when I
am using the old server name, it is throwing an error with the message "'origin' does not
appear to be a git repository." It means Git is not identifying the old name, so all the
operations will be performed by a new name.
To see additional information about a particular remote, use the git remote command along
with show sub-command. It is used as:
Syntax:
It will result in information about the remote server. It contains a list of branches related to the
remote and also the endpoints attached for fetching and pushing.
Output:
16
The above output is listing the URLs for the remote repository as well as the tracking branch
information. This information will be helpful in various cases.
We can change the URL of a remote repository. The git remote set command is used to
change the URL of the repository. It changes an existing remote repository URL.
We can change the remote URL simply by using the git remote set command. Suppose we
want to make a unique name for our project to specify it. Git allows us to do so. It is a simple
process. To change the remote URL, use the below command:
The remote set-url command takes two types of arguments. The first one is <remote name >,
it is your current server name for the repository. The second argument is <newURL>, it is
your new URL name for the repository. The <new URL> should be in below format:
https://github.com/URLChanged
1. $ git remote -v
17
Theoretical learning Activity
Written assessment
18
d) In git you can view untracked files by using git version command
9. What will be the output of the folloing git command
Git init L3SWDA
10. When do we use the following git commands?
a) Git Commit
b) Git Clone
c) Git push
d) Git Pull
e) Git remote
f) Git Status
g) Git config
h) Git config
i) Git rm
Practical assessment
Resources
Git
Terminal
Web browser
Tools
Text editor (sublime text, notepad,
notepad++, vscode)
GitHub
Computer
Equipment Network devices
19
Internet
Materials/ Consumables Electricity
Assessment Observation
criteria
Assessable Marks
(Based on Indicator
outcomes allocation
performance Yes No
criteria)
.Setup Git is Indicator 1. Git setup is
repositor properly installed 5
initiated
based on Git Indicator 2. Git is
commands 5
configured
Repository is Indicator 1. GitHub
5
properly account is created
created based
on the Indicator 2. Remote
5
project. Repository is created
Remote repository is
5
cloned
Untracked files are
5
displayed
Remote URL Indicator 1. Remote 5
is properly URL is generated
20
set in Indicator 2. URL is
accordance configured
5
with Git
commands
21