What is git and how does it work?

What is Git?

Linus Torvald, the creator of Linux, founded GIT in 2005. As GIT is a distributed version, they could be used as a server for managing repositories. The main goal is that they can handle large projects and support non-linear developments quickly.

Git is the most widely known version control system. Git tracks the changes you make to files so you have a record of what has been done and can revert to specific versions if necessary. Git also facilitates collaboration by allowing multiple people’s changes to be merged into a single source.

Git is a piece of software that runs on your computer. Your files, as well as their history, are saved on your computer. You can also store a copy of the files and their revision history on online hosts such as GitHub or Bitbucket.

Having a central location where you can upload your changes and download changes from others allows you to collaborate with other developers more easily. Git can automatically merge changes, allowing two people to work on different parts of the same file and then merge their changes without losing each other’s work!

Ways to Use Git

You can use git through a command line (terminal) or a desktop app with a graphical user interface (GUI), such as Sourcetree , GitKraken, GitHub Desktop.

UI of GitKraken

Main Components of Git

Git workflow states :

Committed : Takes the file from the staging area and permanently stores it in the git as a snapshot.
Modified : It takes files from the working directory (modified or unmodified).
Staged : Snapshots are added to the staging area .

Components of git projects :

Git directory: It stores the object database that is cloned to another remote computer in the repository.
Working tree: Files are extracted from the database and saved to disk for modification.
Staging area: This serves as an index, directing you to the next commit.

Git Repositories

A Git repository contains all of the project files as well as the entire revision history. You’ll take an ordinary folder of files (such as the root folder of a website) and tell Git to turn it into a repository. This generates a .git subfolder containing all of the Git metadata for tracking changes.

Online Git providers

To store our repository code online we can use Git providers

  • GitHub
  • Bitbucket
  • GitLab
  • Azure DevOps

Stage & Commit

A commit represents a new version of your project. It represents a standalone version of your project and contains a reference to all of its files and folders.

Before we commit anything, we must tell Git which files we want to commit. This is known as staging .We can use the git add command in order to add new or updated files to the staging area.

To make a commit we use the git commit command with -m option and pass in a commit message,

git commit -m "this is the first commit"

Branches

Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, regardless of how big or small, you create a new branch to contain your changes. This makes it more difficult for unstable code to be merged into the main code base, and it allows you to clean up your future branch’s history before merging it into the main branch.

To create a branch

git branch new-branch-name

To delete a branch

git branch -d new-branch-name

To switch between branches we can use checkout

git checkout <branchname>

To switch to a new branch

git checkout -b <new-branch> <existing-branch>

Merging

When we are finished working with a branch we can merge that branch to main/other branch following code shows how it can be done

# Start a new feature
git checkout -b new-feature main
# Edit some files
git add <file>
git commit -m "Start a feature"
# Edit some files
git add <file>
git commit -m "Finish a feature"
# Merge in the new-feature branch
git checkout main
git merge new-feature
git branch -d new-feature

Pull Requests

Pull request is a way of asking another developer to pull a branch from your repository into their repository. To file a pull request, you must provide four pieces of information: the source repository, the source branch, the destination repository, and the destination branch.

Pros of using Git

  • Good distributed model because each developer gets a local repository with a complete history of commits, making git faster than other VCs.
  • Branching and merging are simple and inexpensive, and data integrity is maintained.
  • Free and open-source, we can easily download the source code and modify it. Capable of handling larger projects efficiently.
  • Push/pull operations are faster . Save developers time because of the ability to fetch and create pull requests without switching.
  • Redundancy and replication of data Add-ons can be written in a variety of languages.
  • Have better and faster network performance and better disk utilization, and identify data as a series of snapshots.
  • The object model is straightforward, minimizing push/pull data transfers.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Thank you for your feedback, Brian, I'm glad that the article proved to be useful!

What’s at stake for software development ?

Install Flutter on macOS & M1 chip — 2

How I solved Dynamic Task Scheduling using AWS DynamoDB TTL, Stream and Lambda

The Never-Ending Search for Purple Cows

Building A Production App With React Native and Firebase — Lessons I Learnt.

Why did I create a blog from scratch?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevinwijesooriya

Kevinwijesooriya

More from Medium

Learned Java script for free!!!

Scrimba.com Screen shot

Top Core Java Concepts

GitHub: A developers dive into the importance of version control and collaboration.

RESTful API — What You Need To Know