跳到正文

第 4 课 · Versioning Data

Track Data with dvc add

Use dvc add to track files and folders, read the .dvc file it writes, commit it with Git, update data safely, and skip unwanted files with a .dvcignore file.

dvc add is the command you will use most when you version data by hand. It tells DVC: "take care of this file or folder". This lesson walks through a first version, an update and the files involved.

Your first tracked file

Say you have data/reviews.csv. Do not run git add on it. Run:

Terminal
# Read-only: track one file
dvc add data/reviews.csv
git add data/reviews.csv.dvc data/.gitignore
git commit -m "Track reviews dataset, first version"

Several things happen. DVC hashes the file, puts its content in the cache, writes data/reviews.csv.dvc, and adds the data file to data/.gitignore so Git leaves it alone. You commit the two small files. If you set core.autostage, DVC stages them for you.

Inside a .dvc file

The .dvc file is short YAML. A typical one looks like this (the values are illustrative):

YAML
outs:
- md5: 3f1c9a7e5b2d4c8a9e0f1a2b3c4d5e6f
  size: 5242880
  hash: md5
  path: reviews.csv
  • path is relative to the .dvc file's folder.
  • md5 is the content hash, the pointer into the cache.
  • size is the size in bytes.
  • For a folder, you also see nfiles, and the hash ends in .dir.

You can read these files in code review. A changed hash tells reviewers that the data changed, even though they cannot see the rows in the diff.

Tracking a folder

Terminal
# Read-only: track a directory of images
dvc add data/images

DVC writes one file, data/images.dvc, for the whole folder. It does not create one per image. If you add or delete files inside it, run dvc add data/images again and DVC updates the listing, including removals. Prefer tracking a folder of files over a ZIP or TAR archive: with an archive, any tiny change produces a whole new large object, and DVC cannot reuse unchanged files.

Updating tracked data

The workflow for a new version is the same as for the first:

Terminal
# Read-only: record a new version
python scripts/append_new_reviews.py      # changes data/reviews.csv
dvc status                                # shows that the data changed
dvc add data/reviews.csv                  # new hash in the .dvc file
git add data/reviews.csv.dvc
git commit -m "Add November reviews"

If your cache uses hardlinks or symlinks, the file is read-only. Run dvc unprotect data/reviews.csv before editing it.

  • dvc add --no-commit writes the .dvc file but does not copy content into the cache yet. Finish later with dvc commit.
  • dvc commit records changes to files DVC already tracks. Prefer rerunning a pipeline when one exists, because commit simply accepts what is on disk and DVC cannot guarantee how it was made.
  • dvc move old new renames a tracked file and updates its .dvc file.
  • dvc remove data/reviews.csv.dvc stops tracking it. The data file stays on disk, and the .gitignore entry is removed.
  • dvc add -o <path> <source> copies data from another location into a workspace path and tracks it.

Skip files with .dvcignore

Folders often contain junk: editor backups, operating system files, temporary exports. A .dvcignore file uses the same pattern rules as .gitignore.

TEXT
# .dvcignore
*.tmp
.DS_Store
data/scratch/

Test your patterns with dvc check-ignore <paths>. Remember three cautions from the documentation:

  1. Ignored files are never cached, so DVC behaves as if they do not exist.
  2. dvc repro and dvc exp run may remove ignored files, and such files may be lost for good.
  3. Do not put a .dvcignore inside a tracked or dependency directory; DVC raises an error. Put the rules higher up.

Try the idea

The stand-in below imitates what happens when a folder is added: ignored files are skipped, and a summary like a .dvc entry is produced. It is not DVC output.

Python Stand-in: add a folder with ignore rules

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistake: running git add on the data file first. If Git already tracks a file, DVC will not take it over. Remove it from Git's index with git rm -r --cached <path>, commit, then run dvc add. The old copy stays in Git history, so secrets or huge files need a history cleanup too.

What not to track

Do not use DVC to store passwords, or personal data that you are not allowed to copy. Once data is in a cache and a remote, deleting it everywhere takes deliberate work. Check your organization's rules about retention and access before you version sensitive datasets.

Choosing what to track as one unit

The unit you add decides how you can use it later. If you add a whole data/ folder, one pointer file covers everything, and a teammate who needs one subfolder may still deal with the whole unit. If you add many separate files, you get many pointer files to review, but each can change and be rolled back on its own.

A practical rule is to group files that change together and are used together. For example:

Layout Pointer files Good when
dvc add data One The data is always released as one set
dvc add data/train data/test Two Train and test evolve separately
dvc add data/*.csv with --glob One per file Files are independent sources

Decide early. You can change the layout later with dvc remove and a new dvc add, but the history will show two different structures, which makes comparisons harder.

Check before you commit

Before committing, run git status. You should see the new .dvc file and a changed .gitignore, and you should not see the data file itself. If the data file appears, stop and find out why before you commit it.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. After dvc add data/reviews.csv, what should you commit with Git?

    Git keeps the pointer file and the ignore rule, not the data.
  2. 2. How many .dvc files does dvc add create for a folder with 500 images?

    One .dvc file describes the whole directory.
  3. 3. Why is tracking a folder usually better than tracking a ZIP of it?

    With a folder, only changed files become new cache objects.

Summary

  • dvc add caches data, writes a .dvc file and tells Git to ignore the data.
  • Commit the .dvc file and .gitignore; rerun dvc add after changes.
  • A folder gets a single .dvc file.
  • .dvcignore skips junk files, with care around pipeline outputs.
  • dvc remove stops tracking without deleting your data file.

Next: Switch Between Data Versions

你的学习进度保存在此浏览器中,无需账号。