What is Git?

Chamal Weerasinghe
6 min readMay 14, 2021

--

Image from Double Momentum

“Always have a backup plan.” — Mila Kunis

What is Version Control

During the time software become widely adopted in every industry and dot-com buddle also became a reason for complex and enterprise-grade software, with the time the requirements getting changed, bugs have to fix and more than one team consist of many developers had to work on the same system. and the need of a version control/source control system raised.

The version control system is a special type of software that can keep track of every modification done to the codebase, this allows the team to manage the source code without collisions and identify what are the changes done through the time to the system. SVN, CVS, Git are popular version control systems.

Why Git So Special?

Git was created by Linus Torvalds around 2005 as a solution for maintaining the Linux kernel. When it compares to other Git has unique features.

Git is distributed - Git does not need a central server to work even in the local machine software can maintain using Git.

It supports parallel development - Using the concepts of branching and merging git provides the ability to maintain more than one active development cycle parallelly, without affecting each other.

Other than that it’s simplicity and speed compared to others.

Before getting started with Git follow the link to download and install git.

Concepts of Git

Initializing a Git repo

Before starting to work on Git a repository should be created after installing the Git, with the appropriate CLI, for that these commands can be used.

1. Navigate to the folder 
cd myRepository
2. initialize as a git repository
git init

Branches of Git

Git supports parallel development of the same application by maintaining different branches, these branches are completely isolated from one another a change made in one branch does not affect other branches. This allows developers to work on different features at the same time. However the “main” / “master” branch use to store the current stable version of the codebase, usually, other branches are derived from the main branch at first.

How Git Branching Works

To view available branches

git branch

To create branch

//Create a branch
git branch -b <branchname>
//Create a new branch and Navigate to it
git checkout -b <branch_name>

It is important here that when creating the branch it uses the current branch you are in to derive from. it is better to view the current branch or first navigate to it using.

git checkout <branch_name>

Here are the other commands related to branches

// Safe delete a branch
git branch -d <branch_name>
// Force delete a branch
git branch -D <branch_name>

Commits of Git

Git is very alerted when it comes to the changes, within a git repository every change done to a single file or a collection of files is recorded within Git including the previous state and the current state. Git uses checksumming mechanism with SHA-1 hash and checks whether the difference has happened or not.

And when the developer made a change to the codebase, he/she has to decide whether this change is working and keep it for delete back to the previous state. If the dev decides to keep it, it should be “committed” to the repository. and making it in the current state.

How Git Keep Tracks of its commits (Image from Git. SCM)

to commit a change to the repository, first, it should be added to the repo

git add --all (adding all the changes)
git add changedFileOnly.c (add only one file)

commit changes

git commit -m "meaningful commit message"

and the commit will now be recorded.

Merging

We talked about how to create a branch, how to commit changes to the repository, Are we missing something? Yes, how do we concatenate the works of multiple branches into one? That is merging in Git.

Multiple developers working on a different set of features but at the end of the day, those are all relevant to the same application, in that the changes are merged into the main/master branch.

How Merging works
//Navigae to the main branch
git checkout main
git merge branch01 //Merge branch 01 to main
git merge branch02 //Merge branch 02 to main

If there are modifications for the same file, same lines of code, git cannot identify which one to keep or remove at that time merge conflicts should be removed by the developer.

Other features of Git

git stash - If the developer had to suddenly change the files currently working on and had to take another set of files, or had to work in a different branch, the developer can temporarily shelve the uncommitted changes.

git stash //put to the shelves
git pop // get back from the shelves
git stash apply //apply the working changes to the working copy

git diff - If the developer not sure where are what are the changes made in two arbitrary commits by diff command it will display the changes been made.

git diff //Overall changes done in the directory

.gitignore -o this is actually a file in the git directory, where we can ignore the files and folders when doing the commits, a file or folder defined in this file will not add to the commits.

Git != Github && Git != GitLab

There are some misconceptions about the use of Git, GitHub, and GitLab, Both GitHub and GitLab are cloud-based solutions build on top of Git as the underlying technology. but there are inbuilt with complete CI/CD cycles and GUI interface for the Git to increase user-friendliness.

The power of Git we talked about “Git is distributed”, Unlike decade before the development teams are now spread in every continent at that time managing a local copy and keep updating is time-consuming services like GitHub provided solution for this by using cloud technologies and providing global accessible. and automating the processes like testing and deployment.

There are a few commands and methods used in these services.

git clone - Clone is used to get the copy of the existing repository in a service like GitHub or GitLab to our local machine.

git clone <repo> <directory>git clone ssh://john@example.com/path/to/my-project.gitgit clone https://github.com/openjdk/jdk.gitgit clone -branch new_feature git://remoterepository.git

git push - Git push is used to push our changes into the remote repository.

git push <remote> <branch>//Force push
git push <remote> --force
//push to the main branch
git push origin main

git fork - In git fork rather than the original server repository, the user can have its own complete repository, The repositories are independent of one another but can merge to the original repo or from the original repository to forked repository.

For more detailed information please use the references below.

--

--

No responses yet