02-032 - Additional Reading - Git Basics

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

Git Basics

Additional Reading
Introduction
Knowing how to use version control is a crucial skill for any developer working on a
project, especially when working in a team of developers. The source code of a project is
an extremely precious asset and must be protected. Version control software tracks all
changes to the code in a special kind of database. Therefore, if a developer makes a
mistake, they can compare earlier versions of the code to the current version to help fix
the mistake while minimising disruption to the rest of the team. Here, we will introduce
you to the basics of version control. It focuses on the Git version control system and the
collaboration platform, GitHub.

Extra resource

A version control system is one of the most important tools for any software developer!
Check out this HyperionDev blog post to see the other four tools essential for all
developers.

What is a version control system?


Version control systems record modifications to a file or set of files so that you can recall
specific versions of it later on. A version control system can be thought of as a kind of
database. You are able to save a snapshot of your complete project at any time. Then,
when you take a look at an older snapshot (or version) later on, your version control
system shows you exactly how it differs from the current one.

Version control is independent of the kind of project, technology, or framework you are
working with. For instance, it works just as well for an Android app as it does for an
HTML website. It is also indifferent to the tools you work with. You can use it with any
kind of text editor, graphics program, file manager, etc.

Copyright © 2024 HyperionDev. All rights reserved.


Why do you need a version control
system?
Below are some of the benefits of using a version control system for your projects:

● Collaboration: when working on a large (or even medium-sized) project, more


often than not you will find yourself working as part of a team of developers.
Therefore, you will have multiple people who need to work on the same file.
Without a version control system in place, you will probably have to work
together in a shared folder on the same set of files. It is therefore extremely
difficult to know when someone is currently working on a file and, sooner or
later, someone will probably overwrite someone else's changes.

By using a version control system, everybody on the team is able to work on any
file at any time. The version control system then allows you to merge your
changes into a common version, so the latest version of the project is stored in a
common, central place.

● Storing versions: it is especially important to save a version of your project after


making any modifications or changes. This can become quite confusing and
tedious if you do not have a version control system in place. A version control
system acknowledges that there is only one project being worked on, therefore,
there is only one version on the disk you are currently working on. All previous
versions are neatly stored inside the version control system. When you need to
look at a previous version, you can request it at any time.

● Restoring previous versions: being able to restore older versions of a file enables
you to easily fix any mistakes you might have made. Should you wish to undo any
changes, you can simply restore your project to a previous version.

● Understanding what happened: your version control system requires you to


provide a short description of the changes you have made every time you decide
to save a new version of the project. It also allows you to see exactly what was
changed in a file’s content. This helps you understand the modifications that
were made in each version of the project, even if you weren’t the one who made
them.

● Backup: a version control system can also act as a backup. Every member of the
team has a complete version of the project on their disk. This includes the
project’s complete history. If your central server breaks down and your backup
drive fails, you can recover your project by simply using a team member’s local
repositories.

Copyright © 2024 HyperionDev. All rights reserved.


The Git version control system
In this course, we will be using the Git version control system. Git is the most widely
used modern version control system. It is free and open-source, and is designed to
handle everything from small to very large projects.

Git has a distributed architecture and is an example of a distributed version control


system (DVCS). This means that with Git, every developer’s working copy of the code is
also a repository that contains the full history of all changes, instead of having only one
single place for the full version history of the project.

As well as being distributed, Git has been designed with performance, security, and
flexibility in mind.

Installing Git
Before you start learning how to use Git, you must install it. Even if you already have it
installed, you should ensure that you update it to the latest version. Below are the
instructions on how to install Git on Windows, Mac, and Ubuntu:

Installing Git on Windows


1. Go to Git SCM to download the official build from the Git website. The download
should start automatically.

2. After starting the installer, you should see the Git Setup wizard screen. Click on
the “Next” and “Finish” prompts to complete the installation.

3. Open a Command Prompt.

4. Configure your Git username and email using the following commands:

git config --global user.name "Your Name"

git config --global user.email "[email protected]"

Copyright © 2024 HyperionDev. All rights reserved.


Installing Git on macOS
1. Download the latest Git for macOS installer.

2. Follow the prompts to install Git.

3. Open a terminal.

4. Verify that the installation was successful by typing the following command into
the terminal:

git --version

5. Configure your Git username and email using the following commands:

git config --global user.name "Your Name"

git config --global user.email "[email protected]"

Installing Git on Ubuntu


1. Install Git by typing the following commands into your terminal:

sudo apt-get update

sudo apt-get install git

2. Verify that the installation was successful by typing the following into the
terminal:

git --version

3. Configure your Git username and email using the following commands:

git config --global user.name "Your Name"

git config --global user.email "[email protected]"

Extra resource

If you’re using a different Linux/Unix distribution, you can use Git-SCM to download Git
using the native package manager on numerous platforms.

Copyright © 2024 HyperionDev. All rights reserved.


Git concepts
Before you dive into actually using Git, you need to understand a few important
concepts.

Repositories
A repository can be thought of as a kind of database where your version control system
stores all the files for a particular project. A repository in Git is a hidden folder called
‘.git’, which is located in the root directory of your project. Fortunately, you do not have
to modify anything in this folder. Simply knowing that it exists is good enough for now.

There are two types of repositories, namely, local repositories and remote repositories. A
local repository is located on your local computer as the ‘.git‘ folder inside the project's
root folder. You are the only person that can work with this repository. A remote
repository, however, is located on a remote server on the internet or in your local
network. Teams of developers use remote repositories to share and exchange data.
These serve as a common base where everybody can publish and receive changes.

You can get a repository on your local machine in one of two ways:

● You can initialise a new repository that is not yet under version control for a
project on your local computer.

● You can get a copy of an existing repository. For example, if you join a company
with a project that is already running, you can clone this repository to your local
machine.

Commit
A commit is a wrapper for a set of changes. Whenever someone makes a commit, they
are required to explain the changes that they made with a short commit message so
that, later on, people looking at the project can understand what changes were made.

Every set of changes creates a new, different version of your project. Therefore, every
commit marks a specific version. The commit can be used to restore your project to a
certain state as it’s a snapshot of your complete project at a certain point in time.

Copyright © 2024 HyperionDev. All rights reserved.


The working directory, staging area, and Git
repository
Your files can have three main states in Git: committed, modified, and staged. If your
file is committed, its data is safely stored in your local database. If it is modified, the file
has changed but has yet to be committed to your database. If it is staged, it means that
you have marked a modified file to go into your next commit in its current version.

This leads to the main sections of a Git project: the working directory, staging area,
and Git repository. A working directory consists of files that you are currently working
on. The staging area is a file that is contained in the Git repository. It stores information
about what will go into your next commit. The staging area acts as the interface
between the repository and the working directory. All changes added to the staging
area will be the ones that actually get committed into the Git repository, which is where
Git stores the metadata and object database for your project.

A version of a file is considered committed if it is in the Git repository. If it has been


changed and has been added to the staging area, it is considered staged. If it has
changed but has not been staged, it is modified.

Basic Git workflow


Below is the basic Git workflow:

1. Modify a file from the working directory.

2. Add these modified files to the staging area.

3. Perform a commit operation to move the files from the staging area and store
them permanently in the Git repository.

GitHub
GitHub is an online Git repository hosting service. It is free to use for open-source
projects and offers paid plans for private projects.

GitHub offers all of the functionality of Git while also adding its own features. While Git
is a command-line tool, GitHub provides a web-based graphical interface. It provides
access control and many features that assist with collaboration, such as wikis and basic
task management tools for all projects.

Each project hosted on GitHub will have its own repository. Anyone can sign up for an
account on GitHub and create their own repositories. They can then invite other GitHub
users to collaborate on their project.

Copyright © 2024 HyperionDev. All rights reserved.


GitHub is not just a project-hosting service, it is also a large social networking site for
developers and programmers. Each user on GitHub has their own profile, showing their
past work and contributions they have made to other projects. GitHub allows users to
follow each other, subscribe to updates from projects, or like them by giving them a
star rating.

Extra resource

Check out this HyperionDev blog post about Git and GitHub. It provides a simple
guide to GitHub for beginners.

Set up Git and GitHub


1. Install Git by following the instructions given previously for your operating
system.

2. Once you have successfully downloaded and installed Git, enter git --version
into your terminal or command prompt to display your current version of Git.

3. Create a free GitHub account by visiting Join GitHub.

Copyright © 2024 HyperionDev. All rights reserved.

You might also like