Skip to main content

How to Track a Large File with dvc add

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

Git isn't built for big data files: every version stays in the repository forever. dvc add takes a file out of Git's hands, keeps its contents in DVC's cache, and leaves a tiny .dvc text file for Git to track instead. This guide tracks one CSV file, reads the .dvc file line by line, commits it, and fixes the two errors you're most likely to see, with DVC 3.67.1.

Syntax​

Syntax
dvc add <file>
PartWhat it means
dvc addHashes the file, copies it into DVC's cache, and writes <file>.dvc next to it
<file>The path of the file to track, from the current folder

The dvc add reference adds: "The command will add the targets to .gitignore to prevent them from being committed to the Git repository".

The Example​

shop-sales is a Git repository where dvc init has run. Its data/sales.csv holds a shop's daily sales, and it's about to grow far beyond what belongs in Git. The real file here is tiny, so the outputs stay short; the commands are the same for a file of several gigabytes.

Prerequisites​

  • DVC, in a Git repository where dvc init has run. Run dvc --version to check; this guide uses DVC 3.67.1.

Step 1: See the File Git Wants to Add​

Terminal
git status --short
Output
?? data/

?? means untracked: Git would add the whole data folder, data file and all.

Step 2: Track the File with DVC​

Terminal
dvc add data/sales.csv
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 8.76file/s]

To track the changes with git, run:

git add data/.gitignore data/sales.csv.dvc

To enable auto staging, run:

dvc config core.autostage true

The terminal after dvc add data/sales.csv: a full progress bar, then the hint to run git add data/.gitignore data/sales.csv.dvc.

DVC copied the file into its cache, .dvc/cache, and made two small files for Git: data/sales.csv.dvc and data/.gitignore. The hint at the end is the next command to run.

Step 3: Read the .dvc File​

Terminal
cat data/sales.csv.dvc
data/sales.csv.dvc
outs:
- md5: 179cb6f809e6b6875de3f981f822a7c3
size: 75
hash: md5
path: sales.csv

data/sales.csv.dvc in a macOS-style code window: an outs list with md5 179cb6f809e6b6875de3f981f822a7c3, size 75, hash md5 and path sales.csv.

This is all Git will ever store about the data. The .dvc files page describes each field:

FieldHereWhat it means
md5179cb6f8…The file's MD5 hash, its fingerprint. Any change to the file changes it
size75The file's size in bytes
hashmd5The hash type; the page says "only md5 is currently supported"
pathsales.csvThe tracked file, relative to the .dvc file's folder

And the new .gitignore keeps the real file out of Git:

Terminal
cat data/.gitignore
Output
/sales.csv

The terminal after cat data/.gitignore: one line, /sales.csv.

Step 4: Commit the .dvc File​

Run the command DVC suggested, then commit:

Terminal
git add data/sales.csv.dvc data/.gitignore
git commit -m "Track sales.csv with DVC"
Output
[main dee2398] Track sales.csv with DVC
2 files changed, 6 insertions(+)
create mode 100644 data/.gitignore
create mode 100644 data/sales.csv.dvc

To see exactly what Git now tracks, list its files:

Terminal
git ls-files
Output
.dvc/.gitignore
.dvc/config
.dvcignore
data/.gitignore
data/sales.csv.dvc

The terminal after the commit and git ls-files: Git tracks data/sales.csv.dvc and data/.gitignore, and not data/sales.csv itself.

data/sales.csv.dvc is there, and data/sales.csv isn't: Git versions the pointer, and DVC keeps the data. Every command in this guide works unchanged in PowerShell.

Error: Already Tracked by SCM​

A second file, data/prices.csv, had been committed to Git before DVC came along. dvc add refuses it:

Terminal
dvc add data/prices.csv
Output
Adding...
ERROR: output 'data/prices.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/prices.csv'
git commit -m "stop tracking data/prices.csv"

The message has the fix. git rm --cached stops Git tracking the file without deleting it from your disk:

Terminal
git rm -r --cached data/prices.csv
Output
rm 'data/prices.csv'
Terminal
git commit -m "Stop tracking prices.csv in Git"
Output
[main cdb7de0] Stop tracking prices.csv in Git
1 file changed, 3 deletions(-)
delete mode 100644 data/prices.csv
Terminal
dvc add data/prices.csv
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 72.97file/s]

To track the changes with git, run:

git add data/prices.csv.dvc data/.gitignore

To enable auto staging, run:

dvc config core.autostage true

The terminal after dvc add data/prices.csv fails because Git already tracks the file, then git rm -r --cached data/prices.csv, the commit, and dvc add data/prices.csv again, which now succeeds.

data/.gitignore now lists both files, /sales.csv and /prices.csv. The old versions of prices.csv stay in Git's history; only new versions go to DVC.

Error: Does Not Exist​

A typo in the path, salse instead of sales, stops dvc add with the operating system's own error:

Terminal
dvc add data/salse.csv
Output
Adding...
ERROR: output 'data/salse.csv' does not exist: [Errno 2] No such file or directory: '/Users/you/source/shop-sales/data/salse.csv'

Check the name with ls data, and run it again.

Common Mistakes​

MistakeWhat happensDo this instead
Committing the data file to Git firstdvc add says it's already tracked by SCM (e.g. Git)git rm -r --cached <file>, commit, then dvc add
Committing only the .dvc filedata/.gitignore stays untracked, so the rule that keeps the data out of Git isn't shared with anyone who clones the repositoryAdd both files DVC names in its hint

Troubleshooting​

SymptomLikely causeFix
output '...' is already tracked by SCM (e.g. Git)Git tracks the fileThe two commands in the message, then dvc add again
output '...' does not exist: [Errno 2] No such file or directoryA wrong pathFix the path; paths are relative to the current folder
bad DVC file name '...' is git-ignored.A .gitignore rule also ignores the .dvc file DVC wants to writeFixed: "bad DVC file name '…' is git-ignored" When You Run dvc add

FAQ​

Where did the file's contents go?​

Into .dvc/cache, under a name made from its MD5 hash. Your data/sales.csv is still in place, and you use it as before.

Can I stop DVC from printing the git add hint?​

Yes: dvc config core.autostage true makes dvc add run the git add for you. The hint says so itself.

Does it work the same for a folder?​

Yes, with one .dvc file for the whole folder; the folder guide below shows it.

Conclusion​

dvc add data/sales.csv moved the file's contents into DVC's cache, kept the file out of Git with data/.gitignore, and left data/sales.csv.dvc, five lines with the file's MD5 hash and size, for Git to commit. A file Git already tracked needed git rm -r --cached first.

Additional Resources​

  • dvc add — every option, including --no-commit.
  • .dvc files — every field a .dvc file can hold.