Git & Version Control
1. What is Git and Why It Matters
Git is a distributed version control system that helps developers track changes to their
code over time. It enables multiple people to collaborate on the same codebase without
overwriting each other's work.
Real-Life Analogy:
Imagine writing a group project on a Word document. Everyone keeps sending different
versions via email (Final.docx, Final_Final.docx, Final_edited_by_Ayo.docx). It quickly
becomes confusing.
Now imagine a system where:
● You can all work on the document at the same time
● Every change is tracked
● You can go back to any previous version
● You can experiment with changes without affecting the original
That’s what Git does for code.
Why It Matters in DevOps
● Enables collaborative development across distributed teams
● Ensures accountability by tracking who made which changes
● Allows for continuous integration/deployment (CI/CD)
● Helps in recovering from errors (you can rollback to a stable version)
2. Git Basics
Here are the core Git commands and what they do, explained using a photographer’s
workflow:
git init
Starts a new Git repository in your folder.
Scenario:
You’ve just started a new photoshoot project. You create a new album for it — git init is
like creating that new album where all your photos (code) will be stored and organized.
git init
git add
Stages changes (files) to be committed. It’s like choosing which photos to include in your
final album.
git add <filename> # add one file
git add . # add all files in the folder
git commit
Takes a snapshot of the current state of the project — like saving the final version of
selected photos with a label.
git commit -m "Add homepage layout"
git push
Sends your local commits to a remote repository (e.g., GitHub).
Scenario:
You’re done editing locally, and now you're uploading your photos to the cloud to share with
the team.
git push origin main
git pull
Fetches and merges changes from the remote repository.
Scenario:
Your teammate added new photos to the shared album. You use git pull to download
them and update your local copy.
git pull origin main
git branch
Creates a new branch for you to work on features independently.
Scenario:
You want to experiment with black-and-white filters without affecting the original photos. You
create a "filter-test" copy. That’s your new branch.
git branch feature-xyz
To switch branches:
git checkout feature-xyz
git merge
Combines changes from one branch into another.
Scenario:
You like how your black-and-white edits turned out, and now you want to include them in the
main photo album. That’s a merge.
git checkout main
git merge feature-xyz
3. Using GitHub
GitHub is a cloud-based hosting platform for your Git repositories. It helps:
● Collaborate on projects
● Track issues
● Review code (pull requests)
● Run CI/CD workflows
Common GitHub Workflow:
1. Create a GitHub repo
Clone it locally
git clone https://github.com/username/project.git
2.
3. Make changes and push
4. Open a Pull Request (PR) to review changes
5. Merge after approval
4. Basic Bash Scripting for Automation
Bash scripts are used in DevOps to automate repetitive tasks like deployments, backups,
and configuration.
Basic Structure
#!/bin/bash
echo "Starting backup..."
cp -r /var/www /backup/site
echo "Backup completed!"
Scenario: Daily Code Backup
#!/bin/bash
# File: backup.sh
DATE=$(date +%F)
BACKUP_DIR="/home/devops/backups/$DATE"
mkdir -p $BACKUP_DIR
cp -r /home/devops/projects/* $BACKUP_DIR
echo "Backup done at $DATE"
Make it executable:
chmod +x backup.sh
Then run:
./backup.sh
You can automate it with a cron job:
crontab -e
# Run every day at 8PM
0 20 * * * /home/devops/backup.sh
Real-World DevOps Scenario
You’re working on a web app with your team. Everyone:
● Works on their own feature branch
● Commits and pushes code daily
● Opens Pull Requests on GitHub
● Jenkins or GitHub Actions runs tests automatically
● Once approved, changes are merged into main and deployed to production via a
Bash deployment script
Class task:
Each student will contribute a section to a shared project — a simple HTML-based team
portfolio — using Git and GitHub.
The difference between Git and GitHub is like the difference between a tool and a website
that hosts projects made with that tool.
🔧 Git
● Git is a version control system.
● It runs locally on your computer.
● It lets you:
○ Track code changes
○ Revert to previous versions
○ Work with branches
○ Collaborate with others
Think of Git as Microsoft Word's "track changes" feature for code.
✅ Git is installed on your machine (e.g., via git init, git commit).
☁️ GitHub
● GitHub is a cloud platform that hosts Git repositories.
● It helps you:
○ Store code online
○ Collaborate with others via pull requests
○ View history, diffs, and issues in a web interface
○ Deploy apps, manage CI/CD
Think of GitHub as Google Docs for your Git-tracked code — with
collaboration, comments, and sharing.
✅ GitHub uses Git behind the scenes but adds a web UI, team tools, and hosting.
🔁 Relationship
You can use Git without GitHub, but you can't use GitHub without Git.
Would you like a visual diagram showing how they work together?