Intermediate

Data Versioning with DVC

Track large data files and directories, push and pull from remote storage, and switch between data versions using Git.

Tracking Files

Bash - dvc add workflow
# Track a single file
dvc add data/train.csv

# This creates:
# data/train.csv.dvc  - Pointer file (commit this to Git)
# .gitignore update   - data/train.csv added to .gitignore

# Track a directory
dvc add data/images/

# Commit the pointer files to Git
git add data/train.csv.dvc data/.gitignore
git commit -m "Track training data with DVC"

# Push data to remote storage
dvc push

The .dvc File

YAML - Contents of train.csv.dvc
outs:
- md5: a1b2c3d4e5f6a1b2c3d4e5f6
  size: 52428800
  hash: md5
  path: train.csv

# This lightweight file is what Git tracks.
# The actual data (52 MB) is stored in DVC cache
# and pushed to remote storage.

Push and Pull

Bash - Syncing data with remotes
# Push all tracked data to remote storage
dvc push

# Pull all tracked data from remote storage
dvc pull

# Push/pull specific files
dvc push data/train.csv.dvc
dvc pull data/train.csv.dvc

# Fetch without checkout (download to cache only)
dvc fetch

# Checkout from local cache (no download)
dvc checkout

Switching Data Versions

Bash - Version switching with Git + DVC
# Tag the current version
git tag data-v1

# Update the data
# ... modify data/train.csv ...

# Track the new version
dvc add data/train.csv
git add data/train.csv.dvc
git commit -m "Update training data v2"
git tag data-v2
dvc push

# Switch back to v1
git checkout data-v1 -- data/train.csv.dvc
dvc checkout data/train.csv.dvc

# Switch to v2
git checkout data-v2 -- data/train.csv.dvc
dvc checkout data/train.csv.dvc

Accessing Data Programmatically

Python - DVC Python API
import dvc.api

# Get URL of a tracked file (for cloud-native access)
url = dvc.api.get_url(
    path='data/train.csv',
    repo='https://github.com/user/project'
)

# Read file contents directly
with dvc.api.open(
    'data/train.csv',
    repo='https://github.com/user/project',
    rev='data-v1'   # specific Git tag/branch/commit
) as f:
    import pandas as pd
    df = pd.read_csv(f)

# Get parameters
params = dvc.api.params_show()
print(params['train']['learning_rate'])

Key Commands

CommandDescriptionGit Equivalent
dvc addStart tracking a file/directorygit add
dvc pushUpload data to remote storagegit push
dvc pullDownload data from remotegit pull
dvc fetchDownload to cache (no checkout)git fetch
dvc checkoutRestore data from local cachegit checkout
dvc statusShow changes in tracked datagit status
dvc diffShow differences between versionsgit diff
Content-addressable storage: DVC uses file content hashes (MD5) for storage. If two datasets contain identical files, they are stored only once, saving storage space. This also means dvc push only uploads new or changed files.

Ready to Go Deeper?

Live instructor-led courses from our partners. Affiliate disclosure.