Introduction to Git

25 minLesson 1 of 4

Learning Objectives

  • Understand why version control matters for DevOps
  • Install and configure Git
  • Initialize a repository and track files
  • Master the add → commit workflow
  • View history with git log

Why Version Control?

Version control tracks changes to files over time, enabling:

  • Collaboration — Multiple people work on the same project
  • History — See who changed what and when
  • Rollback — Revert to any previous state
  • Branching — Work on features without affecting stable code

Installing Git

# Ubuntu/Debian
sudo apt install git-all
 
# Verify
git --version

Configuration

git config --global user.name "Your Name"
git config --global user.email "you@example.com"
git config --global init.defaultBranch main
 
# View config
git config --list

Initializing a Repository

mkdir my-project && cd my-project
git init

This creates a hidden .git directory that tracks everything.

The Git Workflow

Working Directory → Staging Area → Repository
     (edit)          (git add)     (git commit)

Checking Status

git status

Adding Files

# Track a specific file
git add filename.txt
 
# Track multiple files
git add file1.txt file2.txt
 
# Track all changes (use carefully)
git add .

Committing Changes

git commit -m "Add initial project files"

Write clear, concise commit messages describing WHAT changed.

Viewing History

git log
git log --oneline
git log --oneline --graph

Deleting Files

git rm filename.txt
git commit -m "Remove unused file"

Practical Exercise

mkdir devops-lab && cd devops-lab
git init
echo "# My DevOps Project" > README.md
git add README.md
git commit -m "Initial commit: add README"
echo "print('hello')" > app.py
git add app.py
git commit -m "Add application entry point"
git log --oneline

Summary

  • git init creates a repository
  • git add stages changes
  • git commit -m "message" records changes
  • git status shows current state
  • git log shows history
  • Always commit with meaningful messages

Next Steps

Next, we'll cover branching, merging, and resolving conflicts.