Track Configuration with Git

GitBeginner
Practice Now

Introduction

Linux administrators often edit configuration files. Git is useful because it records what changed, when it changed, and why. Even when a full infrastructure workflow is not required, a small Git repository can help you track local configuration changes before and after an edit.

In this lab, you will track a small configuration file named app.conf inside /home/labex/project. You will initialize a Git repository, configure local author identity, create an initial commit, edit the file, inspect the diff, and create a second commit. The lab uses Git history as the durable record of the configuration changes.

Inspect the Configuration File

In this step, you will inspect the file that Git will track. Before creating a repository, make sure you know which file matters and what its current contents are.

Move into the LabEx project directory.

cd /home/labex/project

List the configuration file. The -l option shows metadata such as size, owner, and permissions.

ls -l app.conf

Read the file. This configuration uses simple key=value lines, which are common in Linux application configuration.

cat app.conf

You should see log_level=info. Later you will change only that setting so the Git diff stays focused.

Initialize a Local Git Repository

In this step, you will turn /home/labex/project into a Git repository. A Git repository stores history in a hidden .git directory. Git also needs an author name and email before it can create commits. In real work this might be configured globally, but in labs it is safer to configure identity only for this repository.

Initialize the repository. The -b main option creates the initial branch with the explicit name main, which avoids Git printing a default-branch warning.

git init -b main

Set local author identity. The --local scope stores these values in this repository only. The third command disables colored Git output inside this lab repository so short status, diff, and log output are easier to read in a browser terminal.

git config --local user.name "LabEx Learner"
git config --local user.email "learner@example.com"
git config --local color.ui false

Run git status --short. The file should appear as untracked because Git sees it but is not tracking it yet. In short status output, ?? means an untracked file.

git status --short

Commit the Initial Configuration

In this step, you will create the first commit. Git has two main areas to understand at this point: the working tree and the staging area. The working tree is the files you see and edit. The staging area is the set of changes that will go into the next commit.

Stage only the configuration file. Avoid git add . while learning, because it can accidentally stage unrelated files.

git add app.conf

Check the short status again. A means the file is staged as a new file.

git status --short

Create the first commit with a clear message.

git commit -m "Track initial app configuration"

Inspect the recent history. --oneline keeps the output compact. The --no-pager option prints the log directly in the terminal instead of opening an interactive viewer.

git --no-pager log --oneline

Review and Commit a Configuration Change

In this step, you will edit the configuration and inspect the change before committing it. This is the core Git habit for configuration tracking: make a focused change, review the diff, then commit it with a message that explains the intent.

Change log_level from info to debug. The sed -i command edits the file in place. The pattern starts with ^ and ends with $ so it changes only the full line log_level=info.

sed -i 's/^log_level=info$/log_level=debug/' app.conf

Show the difference between the committed version and your working tree. --no-pager keeps the diff directly in the terminal.

git --no-pager diff -- app.conf

The diff should show one removed line and one added line:

-log_level=info
+log_level=debug

Stage and commit the focused change.

git add app.conf
git commit -m "Enable debug logging"

View the compact history again. You should now see two commits.

git --no-pager log --oneline

Finally, check that the working tree is clean for app.conf.

git status --short app.conf

Summary

You practiced basic Git operations for Linux configuration tracking. You inspected a configuration file, initialized a repository, set local author identity, checked status, staged a specific file, created an initial commit, reviewed a focused diff, and committed a configuration change. These habits help administrators understand and recover configuration changes instead of editing files blindly.