What is Git ?



Table of Content


  1. Introduction to Git
  2. Why do we need GIT / benefits of git?
  3. How to use GIT?
  4. Use of Git for developers
  5. Use of Git for DevOps engineers ?
  6. Git best practices
  7. Popular git GUI software
  8. Conclusion

Introduction to GIT:


Here we are getting started with GIT. We will begin by explaining some background on version control, then move on to how to work on Git on your system and finally how to set up to start working with GIT. By the end you should understand why Git is important for DevOps engineers, and why you should use it.

What is the Version control system? Why should you learn?


Version control system is a software application or tool that tracks the changes in a file or set of files. It can easily track the new changes and can revert to the previous version if required. Using a version control system generally means that if you screw things up or lose files, you can easily recover.

Local Version Control Systems:


The traditional approach that developers take for version control is by manually copying files into different directories. This method is error-prone and lacks the systematic tracking and management that modern version control systems (VCS) provide.

In response to the issues with manual copying, developers started using Local Version Control Systems (Local VCS). A local VCS is a version control system that operates on a single machine and typically involves a simple database that keeps track of changes to files under revision control. One of the earliest local VCS examples is the Revision Control System (RCS). RCS maintains a set of revisions for each file, allowing developers to track changes, revert to previous versions, and annotate changes with comments. However, they still have limitations, especially when it comes to collaboration among multiple developers. As software projects grew more complex and distributed, for that purpose distributed version control systems (DVCS) like Git emerged, addressing the limitations of local VCS and allowing teams to collaborate seamlessly.

Types of version control systems:


1. Centralized version control system:


The next major issue that people encounter is that they need to collaborate with developers on other systems. To deal with this problem, Centralized Version Control Systems (CVCSs) were developed. This system has only one single server or one central copy of the entire project. But it offers many advantages, like everyone knows to a certain degree what everyone else on the project is doing. Administrators have fine-grained control over who can do what, and it’s far easier to administer a CVCS than it is to deal with local databases on every client.

On the other side, there are also downside disadvantages, like If that server goes down for an hour, then during that hour nobody can collaborate at all or save versioned changes to anything they’re working on. If the hard disk of the central database is corrupted, and proper backups haven’t been kept, you lose absolutely everything. and can access only required files like those we want to change, but we never have the entire project locally. Here the entire team relies on the central server.


2. Distributed version control system:


This is where Distributed Version Control Systems (DVCSs) come into the picture. In a DVCS (such as Git, Mercurial, Bazaar or Darcs), clients can fully check the repository, including its full history. Thus, if any server dies, and these systems are collaborating via that server, any of the client repositories can be copied back-up to the server to restore it. Every clone is a full backup of all the data. and also This allows you to set up several types of workflows that aren’t possible in centralized systems.


History of GIT:


GIT was created by Linus Torvalds, the same person who created the Linux operating system kernel. Linus Torvalds started working on Git in 2005. At the time, the Linux kernel community was using a proprietary version control system called BitKeeper. However, when the free use of BitKeeper was revoked, Linus decided to create his version control system.

Git quickly gained popularity not only in the Linux kernel community but across the entire software development industry. Its efficiency, speed, and versatility made it a preferred choice for version control.

What is GIT?


GIT is a de-centralized version control system. Everything in Git is checksummed before it is stored and is then referred to by that checksum. This means it’s impossible to change the contents of any file or directory without Git knowing about it. This functionality is built into Git at the lowest levels and is integral to its philosophy. You can’t lose information in transit or get file corruption without Git being able to detect it.

3 States of GIT:


Git has three main states that your files can reside in: modified, staged, and committed:

  • Modified means that you have changed the file but have not committed it to your database.

  • Staged means that you have marked a modified file in its current version to go into your next commit.

  • Committed means that the data is safely stored in your local database.

  1. Working directory

  2. Staging area

  3. Repository

1. Working directory:

The working directory is a single version of the project. These files are pulled out of a compressed database. Where files that you can edit and modify

2. Staging area:
The staging area plays a crucial role in the Git workflow and allows you to selectively choose which changes to include in your next commit.

3. Repository:
This is where Git permanently stores the history of your project, including all committed changes. After staging the changes, you commit them to the repository using the git commit command.


Why do we need GIT / benefits of git?



Use of GIT for developers:


  1. You can revert specific files or the entire codebase to a previous state. This is useful if you discover a bug or issue in the current code and need to go back to a known working version.

  2. You can roll back the entire project to a specific commit or version. This is beneficial when you want to undo all the changes made at a particular point in time.

  3. keeps a detailed history of all changes, allowing you to compare code between different commits. This is useful for identifying when specific changes were made, understanding the evolution of the code, and pinpointing the introduction of bugs or issues.

  4. GIT Logs each commit with information about the author, date, and a commit message. This helps you identify who made a particular change, making it easier to collaborate and track down the source of issues.

Why GIT is so important for DevOps engineers:


  1. Using GIT developers can easily collaborate with each other

  2. Review each other’s code changes and track issues in real-time

  3. Operations teams can use GIT to manage their infrastructure as Code(IaC) and configuration files, making it easier to automate their infrastructure.

  4. GIT is essential for automation and CI/CD pipelines. GIT automatically triggers builds and tests when the code is committed to the repository, ensuring changes are thoroughly tested before going to production.

  5. Whenever there is an issue can revert to commits and clear the issue


How does GIT work?


GIT was developed as a command-line tool, it provides a simple command-line interface by which we can control the system. To work with this we need to understand its workflow and its commands.

Installing GIT


Before we start working with GIT, we need to verify GIT was installed or not. If it is already installed then do update otherwise, we must install it by following commands

For Debian/Ubuntu:

  1. sudo apt-get update

  2. sudo apt-get upgrade

  3. sudo apt-get install git

For Redhat Based:

  1. sudo yum upgrade

  2. sudo yum install git

For Mac OS:

brew install git

For Mac Installer:

https://git-scm.com/download/mac

Getting a Git Repository


We typically obtain a Git repository in one of two ways:

  1. We can take a local directory that is currently not under version control, and turn it into a Git repository, or

  2. We can clone an existing Git repository from elsewhere.

Either way, we end up with a Git repository on your local machine, ready for work.

Initializing a Repository in an Existing Directory:


For Linux:

cd /home/user/my_application

For Mac OS:

$ cd /Users/user/my_application

For Windows:

$ cd C:/Users/user/my_application

and then initialize your repository:

Type $ git init This creates a new sub-directory

Let’s start working with GIT: (working directory to local repository)

1. Start with Creating the directory

$ mkdir “directory name”

Replace “directory name” with the name of your directory

2. Move to home directory to the directory you want to work on(working directory)

$ cd “directory name”

3. Start creating or initialize an empty GIT repository

$ git init

4. Next, we need to track the untracked files

$ git add “file name”

Replace “file name” with files that are held in the directory. We can also add the entire directory which we want to track. Tracking the files means files are saved in the staging area

5. To check the status of files

$ git status

6. To save files from the staging area or to the local git repository

$ git commit -m “initial commit message”

“-m” flag in the git commit command is used to include a commit message directly from the command line.


Local repository to remote repository:


To Push the file from the local Repository to the Remote Repository we have a tool like GitHub, GitLab, or Bitbucket, create a Repository and copy URL of the Repository in any tool
Command:-

$ git remote add origin <repository-url>

Command to push the file from local to remote repository

$ git push origin master


Secure shell cryptography (SSH) key:


To create an authorization for pushing from the local to the remote repository

1. To generate a new SSH key

Command: ssh-keygen -t rsa -b 4096 -C "[email protected]"

2.Add SSH key to SSH agent

eval "$(ssh-agent -s)"

$ ssh-add ~/.ssh/id_rsa

3.Copy SSH key to clipboard

$ cat ~/.ssh/id_rsa.pub

4. Add SSH Key to Your Git Hosting Provider

5. Test SSH key connection

ssh -T [email protected]

Git local commands


1. It shows the difference in content or commits held in files

The command is: “git diff”

2. To see a single file change

The command is: $ git diff “filename”

3. To add multiple files at a time

Command is: $ git add *. txt

4. To see the changes in the working directory after staging

Command: $ git diff -- staged

5. To see all the commit history (the last commit will be shown first).

Command: $ git log

6. To see the difference between particular commit

Command: $ git show “commit id”

Git Remote commands:


1. To clone the entire project in the local repository and to initialize the local git repo

Command: git clone: $ git clone (remote URL)

2. To pull all changes from the remote repo to the local repo

Command: $ git Pull

3. To see changes in commits in a remote repo without merging

Command: $ git fetch origin

4. To merge new changes to the local file

Command: $ git checkout

Git undoing commands:


1. Git reset: To unstage changes made in the staging area, can also be used to undo a commit

Git reset HEAD~1: To move one commit back

Git Reset willn’t remove data, just it will move to the previous commit. All changes are held in the working directory.

2. To reset a particular ID

$ git reset "commit id"

3. Git revert

Instead of deleting commits in the commit history, a revert will create a new commit that will inverse changes Specified

Command: “git revert head”

Git Remove and Ignore:


1. git rm - To Remove file from the local git repository

2. git rm - - cached “file name” - to remove a file from the staging area

3. git rm - -f “file name” - to remove file forcefully

4. To ignore file: .git ignore


Several graphical user interface (GUI) tools are available for Git, making it easier for users who prefer a visual interface to interact with Git repositories. Here are some popular Git GUI tools:

GitHub Desktop:

  • GitHub Desktop is an official GUI client provided by GitHub. It’s user-friendly and integrates well with GitHub repositories. It is available for both Windows and macOS.

  • GitHub Desktop

GitKraken:

  • GitKraken is a cross-platform Git GUI with a sleek and intuitive interface. It supports both Git and GitHub and provides features like visual branching and merging, collaboration, and more.

SourceTree:

  • SourceTree is a free Git and Mercurial client for Windows and macOS. It’s developed by Atlassian, the company behind Bitbucket and Jira. SourceTree provides a powerful and feature-rich GUI for managing Git repositories.

TortoiseGit:

  • TortoiseGit is a Windows Explorer extension that integrates with the file system. It provides context menu options and overlays to perform Git operations directly from the Windows File Explorer.

Git Extensions:

  • Git Extensions is a standalone Git GUI for Windows that provides a range of features, including a visual repository viewer, Git command-line integration, and support for GitFlow.

SmartGit:

  • SmartGit is a Git client with a focus on simplicity and efficiency. It supports Git, Mercurial, and SVN. SmartGit is available for Windows, macOS, and Linux.

Tower:

  • Tower is a Git client for macOS and Windows with a powerful interface. It supports Git and Mercurial, providing features like visual history, branching, and easy conflict resolution.

Conclusion:


GIT is a vital tool in DevOps that leads us to work efficiently while improving the quality of software releases.

Understanding the foundational principles of Git is essential for unlocking its full potential in modern software development workflows. These principles serve as guiding principles, empowering developers and teams to collaborate effectively, track changes efficiently, and maintain code integrity in today’s dynamic development landscape. Follow RazorOps Linkedin Page Razorops, Inc.


Enjoyed this article? Share it.



Sravani Bikkina

DevOps Engineer

Sravani is a DevOps engineer at Razorops and she has hands-on experience in Devops, Cloud, Git, Docker Kubernetes and CI/CD.





Upcoming Webinar

How to secure Kubernetes and secrets management- Part 2.

29th October,2022

11:30 AM IST

Register Now


Want to setup CI/CD in a more complex app?



Request a Demo





LATEST POSTS

Ready to get started?

30 Days Free Trial

Signup Here