Sub topic 3
Tracking data with dvc add
This chapter assumes you have run git init and dvc init in a project, as covered in the previous chapter. It also assumes you can commit, branch, and read a diff in Git. You will see what dvc add does to your files, what the .dvc pointer file contains, how directories are handled, and which errors you are most likely to hit.
What dvc add does
dvc add takes a file or directory that is too large or too binary for Git and splits the job in two:
- It copies the content into the DVC cache, a content-addressed store under
.dvc/cache. - It writes a small text file, the
.dvcfile, that records the content's hash and where the data belongs in the workspace.
Git then tracks the small .dvc file. The data stays out of Git.
A first run
Create a small dataset so you can follow along. In a real project this file would be much larger.
mkdir -p data
printf "id,age,label\n1,34,0\n2,51,1\n3,29,0\n" > data/raw.csv
dvc add data/raw.csv
git status --shortGit now reports two new files, data/raw.csv.dvc and data/.gitignore. It does not report data/raw.csv, because DVC added that path to data/.gitignore. Commit both:
git add data/raw.csv.dvc data/.gitignore
git commit -m "Track raw data with DVC"The command also printed a reminder to run that git add. The .gitignore entry matters as much as the pointer file. Without it, a later git add . would put the raw data into Git.
Reading the pointer file
Open data/raw.csv.dvc:
outs:
- md5: 3f1c5a0e7b2d4c96a8e01f5d7c3b9a42
size: 43
hash: md5
path: raw.csvThe fields mean the following. The hash shown here is an example, and yours will differ.
| Field | Meaning |
|---|---|
| `md5` | The MD5 hash of the file's content. This is the identity of this version of the data. |
| `size` | The size in bytes. |
| `hash` | The hash algorithm. DVC 3.x writes this field. Older files may omit it. |
| `path` | The file name, relative to the directory that contains the `.dvc` file. |
The path is relative to the .dvc file, not to the project root. That is why it says raw.csv and not data/raw.csv. You can move a .dvc file together with its data without breaking the link.
Each outs entry is an output that DVC manages. A single .dvc file can list more than one, but dvc add creates one .dvc file per target.
Where the data goes: the cache
The first two characters of the hash become a subdirectory name, and the remaining characters become the file name. In DVC 3.x the layout looks like this:
.dvc/cache/files/md5/3f/1c5a0e7b2d4c96a8e01f5d7c3b9a42DVC 2.x omits the files/md5 part. Two consequences follow from addressing files by content:
- Identical content is stored once, even if it appears under different names or in different versions.
- A file in the cache is never edited in place. A changed file has a new hash and therefore a new cache entry.
The next chapter covers the cache in more detail. For now, keep in mind that the cache is your local copy of every version you have added. The remote, introduced later, is the shared copy.
Workspace links: copy, reflink, symlink, hardlink
After dvc add, data/raw.csv still exists in your workspace. It is not necessarily a second full copy of the data. DVC links the workspace file to the cache using the first of these link types that works, in this default order: reflink, then copy.
| Link type | Disk use | Edit in place safely? |
|---|---|---|
| `reflink` | Shares blocks (copy-on-write), so almost no extra space | Yes |
| `copy` | Full second copy | Yes |
| `hardlink` | No extra space, one inode | No, edits change the cache |
| `symlink` | No extra space, a pointer to the cache | No, edits change the cache |
Reflinks require a file system that supports them, such as Btrfs, XFS, APFS, or ReFS. On ext4 DVC falls back to a full copy, which can double disk use for a large dataset.
You can switch to hardlinks or symlinks to save space:
dvc config cache.type "reflink,hardlink,symlink,copy"Tracking a directory
You can pass a directory instead of a file:
mkdir -p data/images
printf "a" > data/images/a.txt
printf "b" > data/images/b.txt
dvc add data/imagesDVC creates data/images.dvc, and the md5 in it ends in .dir:
outs:
- md5: 9b2f61c0d4a8e37f5c10ab2e6d94f7c8.dir
size: 2
nfiles: 2
hash: md5
path: imagesThe .dir entry in the cache is a small JSON file that lists every file in the directory with its relative path and its own hash. Each file is cached individually. The consequences are practical:
- Adding one new image to a directory of 100,000 images stores only the new file and a new
.dirlisting. It does not copy the whole directory again. - The directory hash changes whenever any file is added, removed, or modified, so the
.dvcfile changes and Git records a new version. nfileslets DVC report how many files the directory contains without walking it.
Tracking a directory is usually better than tracking many files one by one. You get one pointer file and one place to run dvc pull.
Updating tracked data
When the data changes, run dvc add on the same path again. Do not edit the .dvc file by hand.
printf "4,62,1\n" >> data/raw.csv
dvc status
dvc add data/raw.csv
git diff data/raw.csv.dvc
git add data/raw.csv.dvc
git commit -m "Add fourth row to raw data"dvc status reports that data/raw.csv.dvc has a changed output, because the file's current hash no longer matches the recorded one. After dvc add, the diff shows only the md5 and size lines changing. The old version stays in the cache, and the previous commit's pointer still refers to it. That is what makes the version switching in a later chapter possible.
Common errors and how to fix them
The file is already tracked by Git
ERROR: output 'data/raw.csv' is already tracked by SCM (e.g. Git).
You can remove it from Git, then add to DVC.
To stop tracking from Git:
git rm -r --cached 'data/raw.csv'
git commit -m "stop tracking data/raw.csv"This happens if the file was committed to Git before you used DVC. Run the commands DVC suggests, then run dvc add again. The file stays on disk. The data remains in earlier Git commits, so history is not rewritten. If the file is very large, you would need a history-rewriting tool to shrink the repository, which is a separate decision.
Adding a file inside a tracked directory
If data/images is tracked, dvc add data/images/a.txt fails with an error saying the output overlaps with another one. A path can belong to only one .dvc file. Track the whole directory, or track the files separately and never the directory.
Forgetting to commit the `.gitignore`
dvc add edits .gitignore in the data directory. If you commit only the .dvc file, a teammate who later adds new data may find that the ignore rule is missing. Always stage the .dvc file and the .gitignore together, as in the first example.
Seeing "data is not in cache" after cloning
A fresh clone has the .dvc files but an empty cache. The data files do not appear until you run dvc pull, which needs a configured remote. Pointer files in Git do not carry the data.
Moving or renaming a tracked file
Do not use mv on the data file. Use dvc move, which updates the .dvc file and the .gitignore entry. A plain mv leaves a pointer file whose path no longer matches anything.
Stopping tracking
To remove a file from DVC but keep it on disk, use dvc remove on the .dvc file:
dvc remove data/raw.csv.dvcThis deletes the .dvc file and removes the .gitignore entry. The data file stays in your workspace. Cache entries are not deleted. They are removed later by dvc gc, which is covered in a later chapter.
Useful options
| Command | Effect |
|---|---|
| `dvc add --no-commit data/raw.csv` | Write the pointer file but skip copying into the cache. Run `dvc commit` afterward. This is useful for very large data you want to cache later. |
| `dvc add -o other/path data/raw.csv` | Not for this purpose. Use `dvc add --out` only with `dvc import-url`. Use the target argument to choose what gets tracked. |
| `dvc add --glob "data/*.csv"` | Expand the pattern and add each match as its own `.dvc` file. |
| `dvc add --file custom.dvc data/raw.csv` | Name the pointer file yourself. The default is the target name plus `.dvc`. |
Summary of the workflow
- Run
dvc add <path>on a file or directory. - Commit the
.dvcfile and the.gitignorechange to Git. - After changing the data, run
dvc addagain and commit the changed.dvcfile. - Use
dvc statusto see whether the workspace matches the pointers.
At this point your data is versioned locally, but only on your machine. Sharing it requires a remote, which comes after the cache chapter.
Exercise
You run dvc add data/train.csv and then git add . followed by git commit. A teammate clones the repository and runs dvc pull, which succeeds, but you notice train.csv itself is not in the Git history. What stopped Git from storing the file, and which two files must be in your commit for the teammate to get the data?
Show answer
dvc add wrote an entry for train.csv into data/.gitignore, so git add . skipped the data file. The commit must contain data/train.csv.dvc (the pointer holding the hash) and data/.gitignore (so the data stays ignored). The teammate's dvc pull then uses the hash in the pointer file to download the content from the remote, which assumes you also ran dvc push.
Exercise
A 40 GB directory is tracked with dvc add data/images. You add one new 2 MB image and run dvc add data/images again. Roughly how much new data is stored in the cache, and what changes in data/images.dvc?
Show answer
About 2 MB plus a small new .dir listing file. DVC caches each file by its own hash, and the existing files already have entries. In data/images.dvc, the md5 (which ends in .dir), size, and nfiles values change.