Learning path: Data and Features in Production · Step 8 of 8
Data Versioning with DVC: Tie Every Model to the Exact Data That Trained It
Version data the way Git versions code. Add a car price CSV and a model with DVC, change one price, go back with git checkout and dvc checkout, get the same RMSE again, and trace a model back to the data that trained it.
What you will learn
- Explain what dvc add does: hash the file, cache it, write a small pointer for Git, and ignore the real file
- Commit code, data pointer, model pointer and run record together, and push the files to a DVC remote
- Go back to old data in two steps, git checkout then dvc checkout, and avoid the trap in between
- Trace a model file back to its commit, its data and its score
Before you start
- Git and GitHub Fundamentals
- Saving and Loading Models with joblib
- Python Virtual Environments
Imagine a library card that only records the title of a book: Home Repair Guide. Emma has the first edition, Jack has the second, and page 40 says something different in each. Same title, two different answers.
A better card also records the edition, and now it names one exact book. In our project, the title is the file name and the edition is a hash of what's inside the file. The card doesn't hold the book. It just says exactly which book.
That's DVC: Git for the code, DVC for the data. We'll add a dataset, change it, go back to the old one, and tie a model to the exact data that trained it. If Git itself is new, start with Git and GitHub Fundamentals.
Git keeps the card, DVC keeps the file
Git is very good with small text files, like code. But every version of a big file would stay in the repo's history forever. So the data and the model go to a separate store that DVC looks after, and Git keeps only a small card (a pointer file) for each one.
To be honest, our CSV is tiny (26,717 bytes) and Git could hold it fine. We're learning the habit, because real data doesn't stay tiny.
What dvc add really does
The project is a small car price package, carprice, with data/CarPrice.csv. Inside it (with DVC 3.67.1 installed):
git init
dvc init
dvc add data/CarPrice.csv
The last command prints:
To track the changes with git, run:
git add 'data\CarPrice.csv.dvc' 'data\.gitignore'
To enable auto staging, run:
dvc config core.autostage true
dvc init creates a hidden .dvc folder next to .git. Then dvc add does four things:
- Works out an MD5 hash of the file:
1aae15dac65274969aa3626e8587e394. - Copies the file into the cache, at
.dvc/cache/files/md5/1a/ae15dac65274969aa3626e8587e394. - Writes the card,
data/CarPrice.csv.dvc. - Adds the real file to
data/.gitignore(/CarPrice.csv), so Git never picks it up.
Five lines, and Git can read them
outs:
- md5: 1aae15dac65274969aa3626e8587e394
size: 26717
hash: md5
path: CarPrice.csv
The md5 is the edition, size is a quick check, and path names the file it belongs to. The data is 26,717 bytes; the card is 98. Later the model gets a card too: 1,479,331 bytes of model, a 104-byte card. A bigger file doesn't mean a bigger card.
One more line gives DVC a shared place to keep files. Here it's a folder beside the project; on a team it would be shared storage.
dvc remote add -d store ../dvc-store
Setting 'store' as a default remote.
Code, data, model and score in one commit
python -m carprice.train
test RMSE 2195
wrote run.json
Training writes run.json, a small run record: the seed, the Python and library versions, a SHA-256 fingerprint of the data, and the score ("rmse": 2195.191). Now add the model the same way:
dvc add models/car_model.joblib
ERROR: bad DVC file name 'models\car_model.joblib.dvc' is git-ignored.
An error, and it's our own .gitignore: it ignored the whole models/ folder, and DVC won't write a card that Git can't see. Take that line out, run dvc add again, and it works. Then commit and push:
git add .
git commit -m "first data, its model, its record"
dvc push
[master (root-commit) 9e25b86] first data, its model, its record
20 files changed, 208 insertions(+)
2 files pushed
git ls-files data models
data/.gitignore
data/CarPrice.csv.dvc
models/.gitignore
models/car_model.joblib.dvc
(The commit output also lists the 20 files it created; they're left out here.) Git has only the cards. dvc push copied both real files to the store. This one commit says exactly what made that score.
One typed zero, and the score moves
Now change the data. This is our own edit, on purpose: one extra zero on the first car's price, like a typing mistake.
from pathlib import Path
CSV = Path("data/CarPrice.csv")
OLD, NEW = b",13495\r\n", b",134950\r\n" # an extra zero
raw = CSV.read_bytes()
CSV.write_bytes(raw.replace(OLD, NEW, 1)) # the first car only
print("first car's price: 13495 -> 134950")
Run dvc add data/CarPrice.csv again, and look at what Git sees:
git diff data/CarPrice.csv.dvc
diff --git a/data/CarPrice.csv.dvc b/data/CarPrice.csv.dvc
index 9a59064..a1603f6 100644
--- a/data/CarPrice.csv.dvc
+++ b/data/CarPrice.csv.dvc
@@ -1,5 +1,5 @@
outs:
-- md5: 1aae15dac65274969aa3626e8587e394
- size: 26717
+- md5: 47d28cac51d3f61d2d5914f90230a684
+ size: 26718
hash: md5
path: CarPrice.csv
Two old lines out, two new lines in. Git never sees the CSV. Train again:
test RMSE 2882
wrote run.json
Up by 687. Same code, same seed, same 41 test rows, and that car is in the training rows, so one bad price did all of this. Add the new model, commit (ac786dd our edit: one wrong price) and push, and the store now holds both versions of each file: four files.
The score won't always tell you the data changed. The hash always does. A one-digit edit, tried separately, still printed test RMSE 2195, but the data got a completely different MD5 (ceb42943...).
Going back takes two steps
git checkout moves the card. dvc checkout fetches the file that card names. Stop after the first step and you train on the wrong data, with no error.Step 1: git checkout 9e25b86, the first commit. Git brings back the old card, and that's all it brings.
dvc status
data\CarPrice.csv.dvc:
changed outs:
modified: data\CarPrice.csv
models\car_model.joblib.dvc:
changed outs:
modified: models\car_model.joblib
Old cards, new files. They disagree. And this is the trap: stop here and train, and you get test RMSE 2882 on the old commit. No error, just the wrong data.
Step 2: dvc checkout. It reads each card, finds that exact file in the cache, and puts it back.
M data\CarPrice.csv
M models\car_model.joblib
dvc status
Data and pipelines are up to date.
Prove it's really the old state
python -m carprice.predict car.json
python -m carprice.train
git status --short
dvc status
predicted 29904
test RMSE 2195
wrote run.json
Data and pipelines are up to date.
The old model is back (it predicts 29,904; the edited model said 27,369). Training gives 2195 again, with the same decimals as the record, so git status prints nothing: run.json came out exactly the same. And dvc status says the new model has the same bytes as well. Same setup, same bytes. Another machine is a different question.
Which data trained this model?
Someone hands you a model and asks: which data trained this? Start from the model's MD5, 1baf9ea00488017c4ee01499306a0416.
git log --oneline --reverse --all -S 1baf9ea00488017c4ee01499306a0416 -- models/car_model.joblib.dvc
9e25b86 first data, its model, its record
ac786dd our edit: one wrong price
git log -S searches the history for that text. Two commits: the older one added it, the newer one replaced it, so take the first.
git show 9e25b86:data/CarPrice.csv.dvc
git show 9e25b86:run.json
outs:
- md5: 1aae15dac65274969aa3626e8587e394
size: 26717
hash: md5
path: CarPrice.csv
{
"seed": 42,
"python": "3.13.14",
"versions": {
"scikit-learn": "1.9.1",
"pandas": "3.1.0",
"numpy": "2.5.4",
"scipy": "1.18.1",
"joblib": "1.6.0"
},
"data_sha256": "2c78d99359a34cb6",
"rmse": 2195.191
}
Two fingerprints of one file: DVC's MD5 (1aae15...) and our own record's SHA-256 (2c78d9...), plus the score. From a model file, back to its commit, its data and its score.
Common mistakes
- Running only
git checkout. The card moves, the file doesn't. Always follow it withdvc checkout, thendvc status. - Git-ignoring the folder that holds a tracked file. DVC refuses to write a card Git can't see.
- Forgetting
dvc push. The commit then names files nobody else can fetch. - Trusting the score to show a data change. A one-digit edit kept RMSE 2195. Check the hash.
Quick recap
| Command | What it does | In this project |
|---|---|---|
dvc add | Hash, cache, write the card, git-ignore the file | 26,717-byte CSV, 98-byte card |
dvc push | Copy cached files to the remote | 2 files, then 4 after the edit |
git checkout | Brings back old cards only | Training here gave the wrong 2882 |
dvc checkout | Puts back the files the cards name | RMSE 2195 again, byte for byte |
git log -S <md5> | Finds the commit that added a file | Model to data to score |
Try it yourself
Run git clone on your repo into a new folder. Is the CSV there? Then run dvc pull and dvc status. What changed?
The project and the dataset are in the StudyEasy AI Community, free to join.
Built this? Share your solution in the StudyEasy AI Community.
Join free on Skool →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.
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.
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.