Sub topic 1
Why Git is not enough for data and models
Git tracks source code well. Machine learning projects also depend on datasets, trained models, and the exact way each model was produced. This chapter explains where Git breaks down for those artifacts and what a data version control tool has to do differently. It ends with the core idea behind DVC, which the following chapters use.
Prerequisites
This chapter assumes you can do the following:
- Use Git for everyday work: commit, branch, tag, and check out an older commit.
- Work comfortably in a terminal.
- Read a simple Python training script and a short YAML file.
You do not need DVC installed to follow this chapter. The one hands-on demonstration uses only Git and a shell. The next chapter installs and initializes DVC.
What a machine learning project actually contains
A trained model is the product of several inputs. If any one of them changes, the model can change.
- Code: preprocessing, feature engineering, and training scripts.
- Data: raw files, cleaned files, and derived features.
- Parameters: hyperparameters such as the number of trees or the learning rate.
- Environment: library versions and hardware, which this tutorial does not cover.
- Outputs: the trained model file and the metrics that describe it.
Git handles the first and third items well, because they are small text files. The data and the model outputs are different. They can be hundreds of megabytes or many gigabytes, and they are often binary.
When someone asks "Which data produced the model we shipped in March?", the answer should be one git checkout away. With plain Git, that is only true if the data lives in Git. Putting it there causes the problems below.
Where Git struggles
Git is a distributed system. Every clone holds the full history of every file. That design is the source of most of the trouble.
Repository size grows with every change
Git stores each version of a file as an object in .git. It compresses objects and can delta-compress similar ones in packfiles. Binary data, such as images, serialized models, and compressed archives, usually compresses and deltas poorly. Each revision therefore adds close to its full size.
You can see this directly. The script below commits a 50 MB file of random bytes, replaces it, commits again, and prints the size of .git after each commit. Random bytes are a worst case for compression. Real binary files such as pickled models or Parquet files are often close to this.
set -e
git init size-demo
cd size-demo
git config user.name "Demo"
git config user.email "[email protected]"
head -c 50000000 /dev/urandom > data.bin
git add data.bin
git commit -q -m "data v1"
du -sh .git
head -c 50000000 /dev/urandom > data.bin
git add data.bin
git commit -q -m "data v2"
du -sh .gitOn a typical system the first du reports about 48 MB and the second about 95 MB. Two edits to one file left the history holding two full copies. Fifty edits would hold fifty copies.
Because every clone gets all history, every new teammate and every CI job downloads all of it. You cannot take only the latest version of the data and ignore the rest, unless you use shallow clones. Shallow clones then remove the history you wanted to keep.
Hosting limits and slow operations
Hosting services restrict file size. GitHub rejects files above 100 MB in normal pushes, and other hosts set their own limits. Even below those limits, git status, git add, and git gc slow down as large files accumulate. Git has to hash and compare each large file to tell whether it changed.
Deleting a file does not delete its history
Suppose you commit a 2 GB dataset by mistake and delete it in the next commit. The repository is still 2 GB larger, because the old commit still references the blob. Reclaiming the space means rewriting history with a tool such as git filter-repo. That changes every later commit hash and breaks everyone else's clones.
Diffs and merges are meaningless
Git's line-based diff and three-way merge work on text. For a binary model file, Git can only report that the file differs. It cannot show what changed, and it cannot merge two branches that both modified the file. For a large CSV, a line diff is technically possible but unreadable. Reviewers cannot tell whether a change is a bug fix or a corrupted export.
Large data often does not belong on the code host
Datasets often need different storage than code. They may live in an object store, on a shared network drive, or in a bucket with specific access controls and regional placement. Code hosting platforms are not designed for that.
Why not copy files by hand?
Many teams start with a shared folder and names like train_v2_final_FIXED.csv. This avoids the Git size problem but creates others:
- Nothing links a data file to the code commit that used it.
- Nobody can tell which file produced which model.
- Files get overwritten, and the old version is lost.
- Two people can edit the same file and silently diverge.
The goal is to keep the benefits of Git (history, branches, tags, review) without storing the data inside Git.
Git LFS compared with DVC
Git LFS (Large File Storage) is a common first fix. It replaces large files in Git with small pointer files and stores the real content on a separate server. This is the same pointer idea DVC uses. The two tools differ in what they add around it.
| Aspect | Git LFS | DVC |
|---|---|---|
| Pointer files in Git | Yes | Yes (`.dvc` files and `dvc.lock`) |
| Where content is stored | An LFS server, usually tied to the Git host | Any configured remote, such as S3, Azure, Google Cloud Storage, SSH, or a local path |
| Pipelines and reproducibility | Not part of the tool | Stages, dependencies, and `dvc repro` |
| Parameters and metrics tracking | Not part of the tool | Built in |
| Local cache and linking | Downloaded objects in `.git/lfs` | A content-addressed cache with optional links |
Neither tool is always better. If you only need to store a few large assets next to code on one Git host, LFS is simpler. If you need to describe how data becomes a model and reproduce that, DVC covers more of the workflow. The rest of this tutorial focuses on the second case.
The core idea: Git versions pointers, DVC stores content
DVC keeps the Git workflow and changes what Git stores. For each tracked file or directory, Git holds a small text file that records a content hash. The data itself goes to a cache on your machine and to a remote storage location that the team shares.
A .dvc file looks like this. The hash and size are illustrative.
outs:
- md5: 22a1a2931c8370d3aeedd7183606fd7f
size: 14445097
hash: md5
path: raw.csvThe md5 value identifies the exact content of raw.csv. When the data changes, the hash changes, and the new .dvc file shows up as an ordinary small text diff in a Git commit. Checking out an older commit gives you the older hash, and DVC can restore the matching content from the cache or the remote.
This split gives each system the job it does best.
- Git keeps history, branches, tags, review, and the link between code and data versions. Its repository stays small.
- DVC cache and remote hold large content, keyed by hash. The same content is stored once, no matter how many commits refer to it.
Because storage is keyed by content hash, DVC avoids storing duplicates. Two commits that point to the same hash share one stored copy. The next chapters cover the cache and remotes in detail.
What this gives you
With this model, the questions that were hard before become routine.
- Which data trained this model? Check out the commit or tag. The pointer files name the exact data.
- Can a teammate get the same data? They clone the Git repository and download the content from the remote.
- Can I go back to last month's dataset? Check out the old commit and restore the matching files.
- Is a model out of date? Because stages record the hashes of their inputs, DVC can detect when inputs changed and rerun only what needs it. The pipeline chapters cover this.
Common misconceptions
- DVC replaces Git. It does not. DVC runs next to Git and relies on Git commits for history. Without commits, there is nothing to tie data versions to code versions.
- DVC stores your data in Git. It does the opposite. Git only receives the small pointer files.
- A pointer file is enough to get data. The content must be present somewhere reachable. If you commit a
.dvcfile but never push the data to a remote, teammates get a pointer to content they cannot download. - Checking out a commit also changes the data files. Git changes only the files it tracks. The pointer files change, and you need a DVC command to update the workspace data to match. The chapter on moving between versions covers this.
Recap
Git works for code because code is small, textual, and mergeable. Datasets and models are large, mostly binary, and expensive to duplicate in every clone. Storing them in Git inflates the repository, hits host limits, makes history permanent, and gives useless diffs. DVC keeps Git as the source of truth for history and stores a content hash in a pointer file. The actual bytes live in a cache and a shared remote. The next chapter sets up a DVC project.
Exercise
You commit a 500 MB model file to Git, retrain, and commit a new 500 MB file under the same name. Then you delete the file in a third commit. Roughly how much data does a fresh clone download, and why? What does DVC change about this?
Show answer
About 1 GB, assuming the model files compress and delta poorly. Git keeps both versions as objects in history, and deleting the file in a later commit does not remove the earlier blobs. Every clone downloads all history. With DVC, Git history contains only small pointer files. The model content sits in the cache and the remote, and a user downloads only the version their checked-out commit refers to.
Exercise
A teammate clones your repository, sees data/raw.csv.dvc in Git, but cannot find data/raw.csv. The pointer file is committed and pushed. Name the most likely cause and the thing that was missed.
Show answer
The data content was never uploaded to a shared remote. The pointer file only holds a hash, and nothing in Git carries the bytes. The content must be pushed from the original machine's cache to a remote (the next chapters cover dvc push and dvc pull) before anyone else can retrieve it.
Exercise
Give two reasons why Git LFS and DVC both use pointer files, yet a project may choose DVC when it has a multi-step training workflow.
Show answer
Both tools keep Git history small by storing hashes in Git and content elsewhere. DVC also lets you define pipeline stages with dependencies, parameters, and outputs, and it records input and output hashes so a stage reruns only when its inputs change. DVC also stores content in any supported remote, instead of one server tied to the Git host. LFS offers neither pipelines nor parameter and metric tracking.