Introduction
Git is an essential tool for modern software development, particularly within the DevOps ecosystem. As a distributed version control system, Git allows developers to track changes, collaborate on code, and manage project history effectively.
This guide will delve into Git’s features, its configuration, and how to use it in conjunction with platforms like GitHub, emphasizing its crucial role in DevOps practices.
Why Git?
- Git is favored by over 70% of developers worldwide, and its popularity is well-deserved.
- It provides a robust framework for version control, enabling teams to work simultaneously on code from anywhere in the world.
- With Git, you can see the full history of a project, revert to previous versions, and manage multiple branches without affecting the main codebase.
These capabilities are especially vital in a DevOps environment, where continuous integration and continuous delivery (CI/CD) require reliable and efficient source code management.
Configuring Git for the First Time
Download git from this link - https://git-scm.com/downloads
Once installed, open the terminal and run following command to check if git is installed or not:
git --version

Before you start using Git, it’s important to configure it with your information, which will be used to track your commits:
$ git config --global user.name "Your Name"
$ git config --global user.email "your.email@example.com"
Tip: Use git config --list to verify your configuration and ensure everything is set up correctly.
Git Operations
Initializing a Repository
The first step in using Git is to initialize a repository, which sets up the necessary files and folders for Git to start tracking changes:
git init
Run git init in the root directory of your project. This ensures that all subdirectories and files under the root directory are included in the Git repository.

This will create a new .git directory (It is hidden) inside your project folder, where all version control data will be stored.
Staging Files
After modifying files, you need to stage them before committing:
$ git add <filename>
To stage all modified files, use:
$ git add --all
Committing Changes
Once files are staged, you can commit them to the repository:
$ git commit -m "Your commit message"
For small changes, you can skip staging and commit directly:
$ git commit -a -m "Your commit message"
In a typical DevOps workflow, developers push code to a Git repository, which triggers a CI/CD pipeline to build, test, and deploy the code automatically. This integration ensures that software can be reliably released at any time, reducing time to market and improving quality.
Git Branching
Branching in Git allows developers to create separate lines of development. This is crucial as multiple features or fixes may be developed simultaneously without affecting the main codebase.
• Creating a New Branch:
git branch <branch-name>
• Switching to a Branch:
git checkout <branch-name>
• Merging Branches:
Before merging, ensure you are on the master branch:
git checkout master
git merge <branch-name>
Working with GitHub
GitHub is a cloud-based Git repository hosting service. It provides all of Git’s distributed version control and source code management functionality, along with its own features like bug tracking, feature requests, task management, and wikis for every project.
• Pushing to GitHub:
First, add a remote repository:
git remote add origin <repository-url>
Then, push your changes:
git push origin master
If you wish to push any other branch just change the name of the branch with master
git push --set-upstream origin feature-branch
This way, you don’t have to specify the remote and branch names in future push and pull commands.
• Pulling from GitHub:
To update your local repository with changes from GitHub:
git pull origin
Git Undo Operations
In DevOps, quick and effective rollback mechanisms are essential. Git provides several commands to undo changes:
• Revert:
This creates a new commit that undoes the changes of a previous commit:
git revert <commit-hash>
• Reset:
This moves the current branch to a previous commit, discarding changes after that point:
git reset <commit-hash>
• Amend:
If you need to modify the most recent commit, use:
git commit --amend -m "Updated commit message"