Chapter 10.1 – Git & GitHub
- Git is the backbone of modern software development and MLOps.
Every AI engineer, Data Engineer, Software Engineer, and MLOps Engineer uses Git every day to track code changes, collaborate with teams, and manage projects.
Learning Objectives
By the end of this chapter, you will be able to
- Understand Version Control Systems (VCS).
- Learn Git architecture.
- Install Git.
- Use essential Git commands.
- Work with branches.
- Merge code changes.
- Resolve merge conflicts.
- Use GitHub effectively.
- Collaborate using Pull Requests.
- Understand GitHub Actions.
Apply Git best practices in AI/ML projects.
1. Introduction
Imagine you are developing a Machine Learning model.
Day 1
You create
- house_price_model.py
- Day 5
- You improve the model.
- Day 10
You accidentally delete an important section of code.
How do you recover it?
Without Git
- Very difficult.
With Git
✅ Simply restore the previous version.
This is why Git exists.
2. What is Git?
Definition
Git is a distributed Version Control System (VCS) that tracks changes in files, allowing developers to collaborate, maintain history, and restore previous versions.
Git was created by Linus Torvalds in 2005 to support Linux kernel development.
3. What is Version Control?
Version Control is the process of tracking changes made to files over time.
Instead of
- Project_Final.py
- Project_Final2.py
- Project_Final3.py
- Project_Final_Final.py
- Project_Final_Really_Final.py
Git stores every change in an organized history.
4. Why Do We Need Git?
Suppose three developers work on the same project.
Without Git
- Files get overwritten.
- Changes are lost.
- Collaboration becomes difficult.
With Git
- Everyone works independently.
- Changes are merged safely.
- History is preserved.
5. What is GitHub?
Git stores your project locally.
GitHub is a cloud platform that hosts Git repositories online, making collaboration easier.
Think of it this way
| Git | GitHub |
|---|---|
| Version Control System | Cloud hosting platform for Git repositories |
| Installed on your computer | Accessible through the web |
| Tracks code history | Enables collaboration, code review, and repository hosting |
6. Git Architecture
Working Directory
│
git add
▼
Staging Area
│
git commit
▼
Local Repository
│
git push
▼
GitHub Repository
7. Working Directory
The Working Directory contains your project files.
Example
Project/
app.py
model.py
requirements.txt
Changes here are not yet tracked until you stage them.
8. Staging Area
The Staging Area is a temporary place where you choose which changes will be included in the next commit.
Example
git add app.py
Only app.py is staged.
9. Commit
A commit is a snapshot of your project at a specific point in time.
Example
git commit -m "Added prediction API"
Good commit messages describe what changed and, when useful, why.
10. Local Repository
The Local Repository stores the complete history of your project on your computer.
Example
11. Remote Repository
The Remote Repository is hosted on platforms such as GitHub.
Example
Local Repository
↓
git push
↓
GitHub
This enables collaboration and backup.
12. Git Workflow
Modify Files
│
▼
git status
│
▼
git add
│
▼
git commit
│
▼
git push
13. Installing Git
Windows
Download Git from
https://git-scm.com/
Verify Installation
git --version
Example Output
git version 2.45.0
(The version number will vary.)
14. Configure Git
Before using Git
git config --global user.name "Sreehari" git config --global user.email "your_email@example.com"
Check configuration
git config --list
15. Initialize a Repository
Create a new Git repository.
git init
Output
Initialized empty Git repository
16. Check Repository Status
git status
Example Output
On branch main
Untracked files
app.py
17. Add Files
Add one file
git add app.py
Add all files
git add .
18. Commit Changes
git commit -m "Initial commit"
This creates the first snapshot.
19. View Commit History
git log
Example
- Commit 1
- Commit 2
- Commit 3
A more compact view
git log --oneline
20. Clone a Repository
Download an existing repository
git clone https://github.com/user/project.git
21. Connect to GitHub
Add a remote repository
git remote add origin https://github.com/user/project.git
View configured remotes
git remote -v
22. Push Changes
Upload commits
git push origin main
23. Pull Changes
Download updates
git pull origin main
This fetches changes from the remote repository and merges them into your current branch.
24. Branches
Branches allow multiple developers to work independently.
25. Create a Branch
git branch feature-api
Switch to it
git checkout feature-api
Or create and switch in one command
git checkout -b feature-api
Modern Git also supports
git switch -c feature-api
26. Merge Branches
git checkout main git merge feature-api
Now the feature becomes part of the main branch.
27. Merge Conflict
Suppose
Developer A edits
learning_rate = 0.01
Developer B edits
learning_rate = 0.001
Git cannot decide automatically.
Conflict markers appear
<<<<<<< HEAD
learning_rate = 0.01
=======
learning_rate = 0.001
>>>>>>> feature
Resolve manually.
Then
git add . git commit
28. Pull Requests (PR)
Instead of merging directly,
developers create a Pull Request.
Workflow
Benefits
- Code review
- Automated testing
- Team collaboration
29. GitHub Actions (CI)
GitHub Actions automate workflows.
Example
Every push
↓
Example workflow file
name: Python Tests
on: [push]
jobs
test
runs-on: ubuntu-latest
30. Common Git Commands
| Command | Purpose |
|---|---|
| git init | Create a repository |
| git clone | Download a repository |
| git status | Check status |
| git add . | Stage all changes |
| git commit -m | Save changes |
| git log | View history |
| git branch | List branches |
| git checkout | Switch branches |
| git merge | Merge branches |
| git pull | Download changes |
| git push | Upload changes |
31. Git in Machine Learning Projects
Example project
HousePricePrediction/
│
├── data/
├── notebooks/
├── models/
├── src/
├── app/
- ├── requirements.txt
- ├── README.md
- └── .gitignore
Git tracks
- Source code
- Configuration files
- Documentation
Large datasets and trained models are often managed using tools like DVC, cloud storage, or model registries rather than storing them directly in Git.
32. The .gitignore File
Some files should not be committed.
Example
- __pycache__/
- .env
- *.log
- venv/
- .ipynb_checkpoints/
This prevents unnecessary or sensitive files from entering the repository.
33. Best Practices
- Write meaningful commit messages.
- Commit small, logical changes.
- Pull before pushing to avoid conflicts.
- Use feature branches.
- Review Pull Requests before merging.
- Never commit secrets (API keys, passwords).
- Keep the main branch stable.
34. Common Mistakes
- Committing passwords or API keys.
- Working directly on the main branch.
- Making one huge commit with unrelated changes.
- Ignoring merge conflicts.
- Forgetting to pull before pushing.
35. Interview Questions
Beginner
- What is Git?
- What is GitHub?
- What is Version Control?
- What is a Commit?
- What is the Staging Area?
Intermediate
- Difference between Git and GitHub?
- What is a Branch?
- What is Merge?
- What is a Pull Request?
- What is a Merge Conflict?
Advanced
- Explain the Git workflow.
- What is GitHub Actions?
- How would you manage large ML datasets in Git?
- Why should secrets never be committed?
- How do you resolve merge conflicts?
Mini Project
Version-Control an ML Project
- Objective
- Create a Git repository for a machine learning project.
- Tasks
- Initialize a repository.
- Add a .gitignore file.
- Commit the initial project.
- Create a feature branch.
- Add a new prediction feature.
Merge the branch using a Pull Request.
Push the project to GitHub.
Chapter Summary
Git is a distributed version control system that tracks changes to your code, while GitHub is a cloud-based platform for hosting Git repositories and collaborating with others. Together, they enable developers to work efficiently, maintain a complete project history, review code through Pull Requests, and automate workflows using GitHub Actions. Git is a foundational skill for software engineering, data engineering, machine learning, and MLOps.
Git Workflow Cheat Sheet
Create Project
│
▼
git init
│
▼
Modify Files
│
▼
git status
│
▼
git add .
│
▼
git commit -m "message"
│
▼
git push origin main
│
▼
GitHub Repository
What's Next?
In Chapter 10.2 – Docker, you'll learn how to package your machine learning application—including Python code, dependencies, and configuration—into a portable container that runs consistently on your laptop, a server, or the cloud. Docker is one of the most important technologies in modern MLOps because it eliminates the classic "it works on my machine" problem.