Learning path: Ship Your First Model: From Notebook to a FastAPI Service · Step 6 of 9
Git and GitHub Fundamentals: Commits, Branches, Push and Pull
The Git you use every day, on a tiny hello-ml project: commits as save points, the staging area, a branch merged with a fast-forward, then push, clone, pull and pull requests with a teammate.
What you will learn
- Explain a commit as a save point and the difference between Git and GitHub
- Make a first commit with git init, git status, git add and git commit
- Try an idea on a branch and merge it back with a fast-forward
- Share work with git remote, push, clone and pull, and know what a pull request is
Before you start
- Running commands in a terminal
- Python Virtual Environments: One Project, One Toolbox
Without Git, a project folder slowly turns into this: train.py, train_v2.py, train_final.py, train_final_REAL.py. Which one is the latest? Nobody knows.
With Git, you keep one file, and Git keeps the history for you. Every change is saved, with who made it and why. That's problem one solved.
Problem two is the team. Jack, Emma and Liam work on one project. Instead of emailing zip files around, they share one copy on GitHub.
- Git is the tool on your laptop. It records the history.
- GitHub is a website that keeps the shared copy for the team.
Our example is a tiny project called hello-ml, with a README.md and one training script:
def train():
print("Training the model...")
if __name__ == "__main__":
train()
A commit is a save point
Think of a video game. You save before the hard level, and if it goes wrong, you load the save. A commit is a save point for your code: a snapshot of the files, with who, when and why.
Jack makes three save points on this project. Git gives each one a short id:
Your first commit
Tell Git who you are
Once per laptop, set your name. It goes into every commit you make. (Git also wants your email, set the same way with user.email.)
git config --global user.name "Jack Miller"
Start the history
Inside the project folder:
git init
Initialized empty Git repository in C:/code/hello-ml/.git/
The whole history now lives in a hidden .git folder inside the project.
See what changed
git status --short
?? README.md
?? train.py
Two question marks mean new files that Git isn't tracking yet.
Stage, then commit
git add .
git commit -m "First version"
[main (root-commit) f8e1e04] First version
2 files changed, 9 insertions(+)
create mode 100644 README.md
create mode 100644 train.py
git add . picks the changes and puts them in the staging area. git commit -m saves everything staged as one snapshot, with a message saying why. That's our very first save point, f8e1e04.
git add picks changes for the next save point. git commit saves what was picked as one snapshot in the history.Read the history
git log --oneline
f8e1e04 (HEAD -> main) First version
HEAD -> main means: this is where you are right now, on the branch called main.
Try it on a branch, then merge
What if you want to try something without breaking the working code? Make a branch. A branch is a safe place to experiment, and main stays as it is.
git switch -c seed
Switched to a new branch 'seed'
Jack adds a fixed seed to train.py:
SEED = 42
def train():
print(f"Training the model with seed {SEED}...")
if __name__ == "__main__":
train()
and commits it. -am stages every tracked file that changed and commits in one step:
git commit -am "Add a fixed seed"
[seed c851f06] Add a fixed seed
1 file changed, 4 insertions(+), 1 deletion(-)
This commit lives on the seed branch only. Happy with it? Go back to main and merge:
git switch main
git merge seed
Updating f8e1e04..c851f06
Fast-forward
train.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
Git says fast-forward. Why? Because main had nothing new of its own since the branch started, so Git simply moves the main label forward to the seed commit.
git log --oneline
c851f06 (HEAD -> main, seed) Add a fixed seed
f8e1e04 First version
Both commits are now on main. Jack then adds a "how to run" line to the README and saves a third time:
git commit -am "Add how to run"
git log --oneline
0b0e3a4 (HEAD -> main) Add how to run
c851f06 (seed) Add a fixed seed
f8e1e04 First version
main has moved on, and the seed label stays where the branch ended.
Your laptop, GitHub, and a teammate
So far, everything lives on Jack's laptop. Now the code goes to GitHub. On GitHub, Jack creates a new repository, keeps it empty, and copies its address. GitHub's docs page "Creating a new repository" walks you through it.
The commands below are shown without output, and the address is a placeholder for your own repository's address:
# Jack: connect the project to GitHub, then upload
git remote add origin https://github.com/<your-name>/hello-ml.git
git push -u origin main
# Emma: get her own full copy, history included
git clone https://github.com/<your-name>/hello-ml.git
# Emma changes something, commits it, and uploads it
git commit -am "<what she changed>"
git push
# Jack: bring Emma's commit down to his laptop
git pull
originis just a short nickname for the GitHub address.-uon the first push remembers where to push next time, so later it's justgit push.git clonegives Emma the files and the whole history.
Many people, one project
- Everyone has a full copy. Each laptop has the whole history, so nobody waits for anybody.
- The owner decides who can push. You invite people with read or write access.
- A pull request asks before merging. You push your branch and ask, "can I merge this?" A teammate reviews it, and then it gets merged. GitHub's docs call it a proposal to merge code changes.
- Conflicts are normal. If two people change the very same line, Git marks that line, you pick what stays, and you commit.
GitHub isn't the only host. GitLab and Bitbucket work the same way, and GitLab calls a pull request a merge request.
Common mistakes
- Keeping
train_final_REAL.pycopies. One file, many commits. The history is the version list. - Experimenting on
main. Switch to a branch first, so the working code stays safe. - Starting work without pulling. Pull first, so you build on your teammates' latest commits.
- Committing the toolbox. The
.venvfolder stays out of Git; commitrequirements.txtinstead (see virtual environments).
Recap
| Idea | Command | In one line |
|---|---|---|
| Commit | git add . then git commit -m "..." | A save point |
| Branch | git switch -c seed | Try an idea safely |
| Merge | git switch main then git merge seed | Bring the idea back into main |
| Push / pull | git push, git pull | Send your work up, bring others' work down |
| Pull request | on GitHub | Ask for a review before the merge |
The daily loop is simple: commit often, push when it works, and pull before you start.
Try it yourself: make a folder, run git init, commit twice, then try a branch and merge it back. Check each step with git log --oneline.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →Python Virtual Environments: One Project, One Toolbox
Next →Testing a FastAPI App with pytest: Three Small Tests
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Machine Learning Engineer: From First Model to ML Engineer
Learn machine learning in Python, then ship it: data analysis, regression, classification, tuning, deployment and MLOps.View on Udemy ↗: Machine Learning Engineer: From First Model to ML Engineer (opens in a new tab)Related articles
Python Virtual Environments: One Project, One Toolbox
Why every Python project gets its own virtual environment: two projects that need different versions of requests, python -m venv .venv, activation, pip install and the PyCharm setting that activates it for you.
Testing a FastAPI App with pytest: Three Small Tests
A tiny FastAPI app with a Hello World route and a name route that refuses numbers, three pytest tests that check it, and what happens when the validation is deleted by mistake.
Config and Logging Instead of Hard-Coded Values in ML Training
Take the hard-coded values out of a car price training script: one config.toml read with tomllib, logging with levels and a run id on every line, and secrets read from the environment. Same RMSE, 2195, before and after.
SQL Essentials for Python: SELECT, WHERE, JOIN and GROUP BY
The SQL a Python developer needs, on a tiny shop with four customers and eight orders: CREATE TABLE, INSERT with placeholders, SELECT with WHERE, JOIN, GROUP BY, pandas.read_sql, and why values never go in f-strings.