Sub topic 3
Tracking Data with `dvc add`
This chapter assumes that you know basic Git commands, can use a terminal, and have DVC installed. You should also understand that a Git commit records the state of tracked files at one point in time.
Git works well for source code and small text files. Large datasets and trained models create different problems. They make repositories slow to clone, increase repository size permanently, and make it difficult to store many versions efficiently. DVC, or Data Version Control, stores a small pointer file in Git while keeping the actual file in a separate cache.
Initialize a DVC project
Start in an existing Git repository or create one:
mkdir fraud-model
cd fraud-model
git init
dvc init
git commit -m "Initialize DVC"The dvc init command creates DVC configuration files and a local cache directory. The cache stores file contents by their content hash. A content hash is a value calculated from the file contents. If the contents do not change, the hash remains the same.
DVC also updates Git-related configuration, usually by adding DVC files and cache paths to .gitignore. Inspect the changes before committing them:
git status
git add .dvc .gitignore
git commit -m "Initialize DVC"Add a dataset
Suppose data/raw.csv contains the source dataset:
mkdir -p data
cp /path/to/raw.csv data/raw.csv
dvc add data/raw.csvThe dvc add command performs several actions:
- It calculates a hash for
data/raw.csv. - It copies or links the file into the DVC cache.
- It creates
data/raw.csv.dvc, a small pointer file. - It adds
data/raw.csvto.gitignoreso Git does not track the large file directly.
The pointer file describes which cached object represents the dataset. Git should track this pointer, not the dataset itself:
git add data/raw.csv.dvc data/.gitignore
git commit -m "Track raw dataset with DVC"A later change to data/raw.csv produces a new hash. Running dvc add again updates the pointer file:
dvc add data/raw.csv
git add data/raw.csv.dvc
git commit -m "Update raw dataset"Git now records the history of the pointer files. DVC uses that history to find the corresponding dataset versions.
Inspect and verify tracked files
Use dvc status to check whether the workspace differs from the tracked DVC state:
dvc statusIf you edit data/raw.csv without running dvc add, DVC reports that the file has changed. This is useful because a Git commit alone does not update the dataset pointer.
You can inspect the pointer file directly:
cat data/raw.csv.dvcIt contains the path and a hash for the cached data. Do not edit this file manually. Run dvc add instead so that the pointer and cache stay consistent.
A model file can be tracked the same way:
dvc add models/model.pkl
git add models/model.pkl.dvc models/.gitignore
git commit -m "Track trained model"Treat model outputs like datasets. Git records which model version belongs to each commit, while DVC manages the potentially large binary file.
Exercise
You changed data/raw.csv and want the next Git commit to refer to the new contents. Which commands should you run, and what does each command do?
Show answer
Run dvc add data/raw.csv to update the cache and pointer file. Then run git add data/raw.csv.dvc and git commit -m "Update raw dataset" to version the new pointer in Git.