Skip to main content
In today’s DevOps and Cloud environments, Git is an essential tool for developers, system administrators, customer support professionals, and project managers alike. This guide explores Git fundamentals, including installing, initializing, and managing Git repositories. You’ll learn how to clone remote repositories, make modifications to your code, and use commits to track those changes over time. We illustrate these concepts using a simple Python web application built with Flask. The application includes a LICENSE file, a README file, a requirements.txt file, and the primary Python file.

Main Application Code

Below is the code from the main Python file (main.py):
Initially, the code is straightforward. However, changes might be made – for example, modifying the welcome message in the root route. Consider the following update as an example:
Tracking such modifications becomes critical as your application grows and more developers contribute simultaneously. This is where source control management (SCM) systems like Git come into play. Git excels at tracking changes, merging contributions from multiple developers, and maintaining a clear history of your application over time. Consider another version of the application with minor modifications:
Git allows you to track such modifications and record who made changes and when, ultimately helping you maintain and manage a robust history of your project.

Initializing and Using Git

Before tracking any changes, Git must be installed on your system. For CentOS systems, you can use the following command:
After installation, verify Git by checking its version:
Example output:

Repository Initialization

To start tracking your code, initialize a Git repository by running the following command in your project’s root directory:
This command creates a hidden directory named .git, which stores all metadata and information about your repository.

Tracking and Staging Files

Before adding files to your repository, check the current status with:
Initially, Git will classify all files as untracked. To track files while excluding, for example, notes.txt, you can add specific files using:
After running the above command, executing git status shows these files as staged for commit. Modifications to any file (e.g., main.py) will be marked as modified. Stage these changes with:
A typical git status output might look like this:

Committing Changes

Once files are staged, commit the changes with a descriptive message:
The output confirms the commit and lists the changes:
Each commit in Git captures a new version of your repository. Subsequent commits should be clear and descriptive to indicate the nature of the changes applied.

Git Workflow Overview

Git operates in three main steps:
  1. Initialization: Set up a new repository.
  2. Staging: Track your file changes.
  3. Committing: Capture and save the state of your project.
This workflow ensures that the evolution of your code is well-documented and that you can easily revert or analyze the changes over time.
The image illustrates a Git workflow, showing a repository with files, versions, and staging, alongside steps for initializing, configuring, tracking, staging, and committing changes.
While this guide presents the basic Git workflow, real-world projects may require additional configurations and best practices tailored to specific development environments.
That concludes the introduction to Git. In the next section, you’ll have the opportunity to reinforce these concepts with hands-on exercises and further exploration of Git commands and workflows.

Watch Video

Practice Lab