Git Basics for DevOps

Your first step towards automation

Table of contents

No heading

No headings in the article.

Introduction to Version Control

A version control system is software that tracks changes to a file or set of files over time so that you can recall specific versions later. It also allows you to work together with other programmers.

Developers can compare earlier code versions with an older version to fix the mistakes.

Git

Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git tracks changes in the source code, enabling multiple developers to work together on non-linear development.

Installing Git

  1. To download the Git installer, visit Git's official site and go to the download page. The link for the download page is https://git-scm.com/downloads.

  2. Click on the package given on the page as download 2.44.0 for Windows. The download will start after selecting the package.

  3. Now, the Git installer package has been downloaded.

  4. Click on the downloaded installer file and select yes to continue. After selecting yes the installation begins,

  5. Click on next, next ... .Until the installation process completes.

Basic Git Commands

git init: Initializing a new Git repository.

The main purpose of the git init command is to initialize the new working environment or a new git repository to work on a new project. Before performing the git init command git will not track the files that are in the system it will start to track only when you initialize the git init command form there you can commit the files and push them to the remote repository.

git init

initialize the repository in the specific directory by giving the path.

git –bare will initialize the bare repository it will not consist of any working directory it is used to share the file between the users.

git init –q will remove the unnecessary outputs during the git initialization.

git init –shared you can the repository between multiple users if you create by using this command.

git clone: Cloning an existing repository.

Cloning is the act of making a copy of any target repository. The target repository can be remote or local. You can clone your repository from the remote repository to create a local copy on your system.

The git clone is a command-line utility that is used to make a local copy of a remote repository. It accesses the repository through a remote URL.

git clone <repository-link>

git add - Adding files to the staging area.

The git add command is used to add file contents to the Index (Staging Area). This command updates the current content of the working tree to the staging area. It also prepares the staged content for the next commit. Every time we add or update any file in our project, it is required to forward updates to the staging area.

The git add command is a straightforward command. It adds files to the staging area. We can add single or multiple files at once in the staging area. It will be run as:

git add <File name>

git commit: Committing changes to the repository.

The commit command will commit the changes and generate a commit-id. The commit command without any argument will open the default text editor and ask for the commit message. We can specify our commit message in this text editor. It will run as follows:

git commit

git commit -m “commit message”

git push: Pushing changes to a remote repository.

The "git push" command is used to push into the repository. The push command can be considered as a tool to transfer commits between local and remote repositories. The basic syntax is given below:

git push <remote> <branch>

The <remote> option refers to the remote repository to which you want to push your files it will refer to its alias name where the name is mapped with the remote repository URL

The <branch> option represents the branch of the GitHub repository that you want to push.

git pull: Pulling updates from remote repository.

The pull command is used to access the changes (commits)from a remote repository to the local repository. It updates the local branches with the remote-tracking branches.

git pull REMOTE-NAME BRANCH-NAME

Working with Branches

A branch is a version of the repository that diverges from the main working project. A Git project can have more than one branch. These branches are a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug, you spawn a new branch to summarize your changes.

Branches allow you to work on different parts of a project without impacting the main branch. When the work is complete, a branch can be merged with the main project. You can even switch between branches and work on different projects without them interfering with each other.

Create Branch

You can create a new branch with the help of the git branch command. This command will be used as:

$ git branch <branch name>

Switch Branch

Git allows you to switch between the branches without making a commit. You can switch between two branches with the git checkout command. To switch between the branches, the below command is used:

$ git checkout<branch name>

Merge Branch

Git allows you to merge the other branch with the currently active branch. You can merge two branches with the help of the git merge command. The below command is used to merge the branches:

$ git merge <branch name>

Git in DevOps

Git is a DevOps tool used for source code management. It is a free and open-source version control system used to handle small to very large projects efficiently. Git tracks changes in the source code, enabling multiple developers to work together on non-linear development.

A version control system that keeps track of all the changes is necessary for the DevOps methodology. With the help of the distributed version control system Git, developers can store a local copy of their commits. The foundation of the DevOps philosophy is faster release cycles and collaboration, which GIT, as a DevOps tool, facilitates. One of the best DevOps strategies is version control. Developers working on a project can share, collaborate, merge, have backups, and version the software with version control. Git is useful for tracking changes made by individual teams when working on a project in large organizations when several teams collaborate on the same project. It facilitates version control, code tracking, and efficient code management. The most important tool for anyone interested in pursuing a career in DevOps is GIT; it is the foundational instrument that should be used first.

Best Practices for using Git

Tips on commit messages

  1. Separate the subject from the body with a blank line

  2. Limit the subject line to 50 characters

  3. Capitalize the subject line

  4. Do not end the subject line with a period

  5. Use the imperative mood in the subject line

  6. Wrap the body at 72 characters

  7. Use the body to explain what and why vs. how

Branching strategies

Master branch

This is the main branch and one of the repositories in which we have the latest stable code of production.

Integration branch

This is the most important and active branch of the repository from which we make releases to the production server.

Staging branch

This is another stable branch of the repository for the QA environment from which we make releases to the QA server.

Dev-deploy branch

This branch will be used primarily for deploying ongoing development work to the Dev environment. Since multiple teams may work on different features, projects, and bugs at the same time, all of them need a Dev environment to test the changes before moving to QA. The idea of the Dev-deploy branch is to merge multiple feature branches to a common branch and deploy the same.

Merge conflicts

Merge conflicts happen when people make different changes to the same line of the same file, or when one person edits a file and another person deletes the same file. You must resolve all merge conflicts before merging a pull request on GitHub.

To get started with DevOps technology, Start by practicing with simple projects then move forward towards complex projects. Git is an essential part of DevOps. It's a distributed version control system that enables non-linear workflows in a distributed way by offering data assurance to develop first-quality software.

git stash, git reset, git bisect, git squash, and git rebase are the advanced git concepts, Once you are comfortable with Basic git then learn the advanced git concept.