Git isn't just for developers—it's the backbone of modern DevOps. From tracking infrastructure-as-code to managing deployment configurations, Git is everywhere. Before you can use GitHub Actions, Terraform, or any modern tool, you need Git fundamentals. In this guide, you'll learn the Git commands you'll use every single day.

1. Learning Objectives

By the end of this lesson, you will be able to:

  • Initialize a Git repository with git init
  • Track files with git add and commit with git commit
  • Check repository status with git status and history with git log
  • Create and switch between branches
  • Merge branches together
  • Write meaningful commit messages

2. Why This Matters

Real-world scenario: You're managing Terraform infrastructure for 20 AWS resources. Something breaks. Without Git, you have no idea what changed, who changed it, or how to revert. With Git, you can see every change, who made it, when, and why—and undo it in seconds.

Key benefits for DevOps:

  • Track changes to Dockerfiles, Kubernetes YAML, and Terraform code
  • Collaborate with team members without overwriting work
  • Roll back bad deployments instantly
  • Audit who changed what and when

3. Core Concepts

What is Git?

Git is a distributed version control system. It tracks changes to files so you can see history, revert changes, and collaborate with others. Unlike Dropbox or Google Drive, Git keeps EVERY version of EVERY file forever.

The Three States of Git

# Configure your identity (do this FIRST)
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

# Verify configuration
git config --list

# Set default editor (optional, but helpful)
git config --global core.editor "nano"
First-time Git setup

Initialize a Repository

# Create a new project directory
mkdir my-devops-project
cd my-devops-project

# Initialize Git repository
git init

# Check what happened
ls -la
# You'll see a .git directory - THIS IS YOUR REPOSITORY

# Check Git status
git status
# Output: On branch main
# No commits yet
# nothing to commit
Creating your first Git repository
kubectl get pods -w
$ git init
Initialized empty Git repository in /home/user/my-devops-project/.git/
$ git status
On branch main

No commits yet

nothing to commit (create/copy files and use "git add" to track)
Fresh repository with no commits

Tracking Files: Add and Commit

# Create some files
echo "# My DevOps Project" > README.md
echo "version=1.0" > config.txt
mkdir scripts
echo "echo 'Hello World'" > scripts/hello.sh

# Check status - Git sees untracked files
git status

# Add files to staging area
git add README.md          # Add single file
git add config.txt         # Add another
git add scripts/           # Add entire directory

# Add all files in current directory
git add .                 # Most common

# Check status after add
git status
# Changes to be committed:
#   new file:   README.md
#   new file:   config.txt
#   new file:   scripts/hello.sh

# Commit (save) the changes
git commit -m "Initial commit: Add project files"

# See your commit
git log
Tracking files through staging to commit
kubectl get pods -w
$ git commit -m "Initial commit: Add project files"
[main (root-commit) a1b2c3d] Initial commit: Add project files
3 files changed, 5 insertions(+)
create mode 100644 README.md
create mode 100644 config.txt
create mode 100755 scripts/hello.sh
Successful commit with unique hash (a1b2c3d)

Viewing History: git log

# Basic log
git log

# One line per commit (cleaner)
git log --oneline

# Show graph of branches
git log --graph --oneline

# Show changes in each commit
git log -p

# Last 2 commits
git log -2

# Commits by author
git log --author="Alex"

# Pretty format
git log --pretty=format:"%h - %an, %ar : %s"
Viewing commit history

Branching: Working on Features in Isolation

Branches allow you to work on different features without affecting the main code. Think of branches as parallel universes for your code.

# List branches (* shows current branch)
git branch

# Create a new branch
git branch feature-add-docker

# Switch to the new branch
git checkout feature-add-docker

# Create and switch in one command
git checkout -b feature-add-kubernetes

# Make changes on branch
echo "FROM ubuntu:latest" > Dockerfile
git add Dockerfile
git commit -m "Add Dockerfile"

# Switch back to main
git checkout main

# See that Dockerfile doesn't exist on main
ls
Creating and switching branches
kubectl get pods -w
$ git branch
feature-add-docker
* main

$ git checkout feature-add-docker
Switched to branch 'feature-add-docker'

$ git branch
* feature-add-docker
main
Asterisk shows current branch

Merging: Combining Branches

# First, make sure you're on the destination branch
git checkout main

# Merge the feature branch
git merge feature-add-docker

# If merge is successful, delete the branch
git branch -d feature-add-docker

# View history to see the merge
git log --graph --oneline
Merging branches together
kubectl get pods -w
$ git checkout main
Already on 'main'
$ git merge feature-add-docker
Updating a1b2c3d..e4f5g6h
Fast-forward
Dockerfile | 1 +
1 file changed, 1 insertion(+)
create mode 100644 Dockerfile
Fast-forward merge (no conflicts)

4. Hands-on Practice Lab

# Setup practice environment
mkdir ~/git-practice
cd ~/git-practice
git init

# Create initial files
echo "# DevOps Project" > README.md
echo "nginx:
  image: nginx:latest
  ports:
    - '80:80'" > docker-compose.yml

git add .
git commit -m "Initial commit: Add README and docker-compose"

# Create and switch to feature branch
git checkout -b feature/add-monitoring

# Add monitoring configuration
echo "prometheus:
  image: prom/prometheus
  ports:
    - '9090:9090'

grafana:
  image: grafana/grafana
  ports:
    - '3000:3000'" >> docker-compose.yml

git add docker-compose.yml
git commit -m "Add Prometheus and Grafana services"

# Switch back to main and merge
git checkout main
git merge feature/add-monitoring

# View your work
git log --oneline --graph
Complete Git workflow practice

5. Common Errors & Solutions

6. Summary Checklist

7. Next Steps

Next lesson: Git Lesson 3.2: Remote Repositories and GitHub Collaboration

You'll learn to:

  • Connect local repos to GitHub
  • Push and pull changes
  • Collaborate with teammates
  • Handle merge conflicts

Gataya Med

DevOps Engineer & Backend Developer. Sharing insights on cloud, automation, and scalable systems.

Comments (0)

Sarah Chen January 19, 2025

This is exactly what I needed! The initContainer approach solved our migration issues completely. Thanks for the detailed guide!

Reply

Leave a Comment