Git is a distributed version control system tracking changes in source code during software development. Linus Torvalds created it in 2005 for the development of the Linux kernel. Developers use Git to keep track of their code changes, revert to previous versions, and collaborate with others. It is a command based tool (CLI).
In Git, each project is stored in a repository, a directory containing all the files, folders, and a history of changes. Each change made to the code is stored as a “commit,” with a unique identifier, message, and a pointer to the previous commit. It allows developers to quickly switch between different code versions and track the changes.
Git also supports collaboration by allowing multiple developers to work on the same codebase simultaneously. Multiple developers can push and pull changes from each other’s local repositories when they work on the same repository. Git also provides features like branches, which allow developers to work on multiple features simultaneously without affecting the main codebase.
Table of Contents
How to Use Git in DevOps?
We use Git in the devOps for version controlling, which means it controls all minor to minor changes. We will get all changes with different commit IDs, so it is easy to manage & understand which changes have been done.
The multiple developers work simultaneously in Git because they will have code due to the branch feature of Git. All developers will be at the same stage of a project.
Git has three stages:
- Working Directory
- Stagging Area
- Local Directory
All developers work in the working directory. In this directory, we create files and do work on them.
After completing a work, we add our work in the stagging area.
We commit here from the stagging area. It is a directory where our code and work would be stored & create backup files.
If we lose our code due to any mishappening, do not worry. We can get all the code from the local repo.
We Define All Processes With Commands From the Working Directory to the Local Directory:
Convert the directory into a local directory
Create a file in which you have to do work
cat filename (write your code here)
Check the file in the working directory
git status (File in Red colour>> working directory or untracked file)
If it shows the Red colour, you need to add in the stagging area
git add filename
If you want to add all files
git add .
Check the file in the stagging area
git status (Green colour means >> file in the stagging area)
Now commit the file and add it to the local repository or directory
git commit -m “msg”
Type commit related massage so that you can easily understand what file has
Get commit ID
Check the file is successfully added to the local repo or not
Your file will be shown in the local repo. Now accidentally, if your file is deleted from the system, then you recover it from the local repository.
Branch in Git
It is the most important function of Git. We can create a feature branch of the main branch, and we can say it makes a copy of it. Due to this functionality, all old & new developers will have a code they continue to work on simultaneously.
Example: we have a login page code & 5 new developers come into the team. They want the old code for doing the rest of the work.
In this situation, we create feature branches, which means a copy of the code. We can create multiple feature branches at a time in Git. We will distribute the branches to the developers & now they can see the old code and start work ahead.
If they make any mistake in work & coding, the main branch will not be affected because developers are working on the feature branch (copy of code), not in the main branch.
Create Feature Branch
Git Branch branchname
See all branch
Git checkout feature-branch-name
Git branch -d branchname
Branch Merging in Git
In this concept, we merge the feature branch with the main branch.
When other developers finish their work, we need to merge all code when we use it.
We use the above process in DevOps, the benefits of Git, and it controls all the versions & stores securely. All versions have different commit IDs, which helps us understand what we committed. It provides such a platform where multiple developers can work simultaneously and, after working, they can merge all code in one place.
Even we are able to know which developer committed and added a code in which file.
Git helps to control all the changes and version management in DevOps easily.