Sub topic 11
Reusing data across repositories
This chapter assumes you can commit, tag, and check out in Git. It also assumes you have already tracked a file with dvc add, configured a remote, and run dvc push and dvc pull. You should know how a .dvc file points to content in the cache.
Data is often shared between projects. A team may keep a cleaned dataset in one repository and train several models from it in others. Copying files by hand loses the history and makes it unclear which version each project used. DVC solves this with two commands, dvc get and dvc import, which read data from another DVC repository by its Git location.
The working model
A DVC repository describes its data with pointer files (.dvc files and dvc.lock) that are committed to Git. The data itself lives in a remote. Because of this, another project can find a file in two steps:
- Read the pointer file from the source repository at a chosen Git revision.
- Fetch the content with the matching hash from that repository's remote.
The consumer never needs a copy of the source repository's cache. It needs read access to the source Git repository and to its DVC remote.
The two commands differ in what they leave behind.
| Command | Result in your project | Link to the source | Updating later |
|---|---|---|---|
| `dvc get` | Plain downloaded files, nothing tracked | None | Run the command again |
| `dvc import` | Downloaded files plus a `.dvc` file that records the source | Recorded in the `.dvc` file | `dvc update` |
Think of dvc get as wget for versioned data, and of dvc import as dvc add with a recorded origin.
Downloading with dvc get
dvc get downloads a file or directory from a DVC repository. It does not need an initialized DVC project in your current directory.
dvc get https://github.com/example-org/dataset-registry data/raw.csvThis saves raw.csv in your current directory. Use -o to choose the output path and --rev to choose a Git revision, such as a tag, branch, or commit.
dvc get https://github.com/example-org/dataset-registry data/raw.csv \
--rev v1.0 \
-o data/raw.csvPinning a tag gives you the same bytes every time. Without --rev, DVC uses the default branch of the source, so the result can change as the source evolves.
dvc get also works for a file that is only tracked through a pipeline output in the source, not just files added with dvc add. The source repository must have pushed that data to its remote. If it has not, the command fails because the content cannot be found.
Since dvc get records nothing, treat its output as a one-time download. If you want Git to remember where the data came from, use dvc import.
Importing with dvc import
dvc import downloads the same data and also creates a .dvc file that stores the source location and revision.
dvc init
dvc import https://github.com/example-org/dataset-registry data/raw.csv \
--rev v1.0 \
-o data/raw.csv
git add data/raw.csv.dvc data/.gitignore
git commit -m "Import raw data v1.0 from dataset registry"The generated data/raw.csv.dvc file looks roughly like this:
md5: 3f1a2b9c8d7e6f5a4b3c2d1e0f9a8b7c
frozen: true
deps:
- path: data/raw.csv
repo:
url: https://github.com/example-org/dataset-registry
rev: v1.0
rev_lock: 5d2c9a7e1b0f4a6c8e3d2b1a9f7c6e5d4b3a2f1e
outs:
- md5: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5c4d
size: 1048576
path: raw.csvThe values here are illustrative. Three fields matter:
urlandrevsay where the data came from and which revision you asked for.rev_lockis the exact commit thatrevpointed to when you imported. It keeps the import reproducible even ifrevis a branch that moves later.frozen: truemeansdvc reprowill not try to refresh this dependency on its own.
The imported file is now tracked like any other DVC output. You can use it as a dependency in dvc.yaml, and dvc pull can fetch it. A collaborator who clones your project can run dvc pull and get the data from your own remote if you pushed it there. If the content is not in your remote, DVC falls back to fetching it from the source repository's remote.
Updating an import
To move to a newer revision of the source, use dvc update.
dvc update data/raw.csv.dvcThis looks at the rev recorded in the .dvc file. If rev is a branch, DVC resolves it to the latest commit and rewrites rev_lock and the hashes. If rev is a tag, nothing changes unless the tag was moved. To switch to a different tag, pass --rev:
dvc update data/raw.csv.dvc --rev v2.0
git add data/raw.csv.dvc
git commit -m "Update raw data to v2.0"The update is an ordinary change to a pointer file, so it appears in your Git history. A reviewer can see exactly when and why the dataset changed. If the new version breaks something, git revert the commit and run dvc checkout.
Importing a pipeline's output
Imports work with directories and with outputs of source pipelines. You can import a trained model from another project the same way you import data.
dvc import https://github.com/example-org/model-training \
models/model.pkl \
--rev v1.0 \
-o models/base_model.pklThis is useful for transfer learning, where one repository trains a base model and others fine-tune it. Each consumer records which base model version it started from.
Browsing a source repository
You do not have to know a path in advance. dvc list shows the files and directories that a repository tracks.
dvc list https://github.com/example-org/dataset-registry data
dvc list https://github.com/example-org/dataset-registry --rev v1.0 --dvc-onlyWithout --rev, the default branch is used. --dvc-only limits the output to paths tracked by DVC.
Using the Python API
Sometimes a script needs a file's contents and not a copy in the working directory. The dvc.api module reads data directly from a source repository.
import dvc.api
with dvc.api.open(
"data/raw.csv",
repo="https://github.com/example-org/dataset-registry",
rev="v1.0",
) as f:
header = f.readline()
print(header)dvc.api.get_url returns the storage URL of a file instead of its contents. dvc.api.read returns the whole file as text or bytes. These are good for notebooks and one-off scripts. They leave no .dvc file, so they do not give you the same record as dvc import.
Choosing between the options
| Situation | Use |
|---|---|
| A one-time download for exploration | `dvc get` |
| A dataset your pipeline depends on, which may change later | `dvc import` |
| Reading a file inside a script or notebook | `dvc.api.open` or `dvc.api.read` |
| Finding out what a source repository offers | `dvc list` |
Common errors
- The data is not found in the remote. The source repository committed a pointer file but never ran
dvc push. Ask the owner to push, or check that the revision you requested is the one that was pushed. - Authentication fails. Access to the source Git repository and to its DVC remote are separate. You may be able to clone the repository but lack permission on the bucket behind it. Configure credentials for the remote in your own environment.
- The result changes between runs. You omitted
--rev, so DVC used a moving branch. Pin a tag or commit. dvc updatesays nothing to update. Therevis a tag that has not moved. Pass--revwith the new tag.dvc reprodoes not refresh an import. Imports are frozen. Rundvc updateexplicitly and commit the result.- A path is rejected. The path is relative to the root of the source repository, not to your current directory. Use
dvc listto confirm it.
A worked scenario
A team keeps a dataset registry repository. Two projects, churn-model and fraud-model, both need data/customers.csv.
- The registry owner adds the file with
dvc add, pushes it, and tags the commitcustomers-v1. - Each project runs
dvc import <registry-url> data/customers.csv --rev customers-v1, commits the.dvcfile, and runsdvc pushto store a copy in its own remote. - A month later the owner publishes
customers-v2. Thechurn-modelteam runsdvc update --rev customers-v2, retrains, and compares metrics withdvc metrics diff. Thefraud-modelteam stays oncustomers-v1until it is ready.
Each project's Git history shows which dataset version produced which model. No one copied files by hand, and the two projects can move at different speeds.
Exercise
You imported a dataset with dvc import <url> data/raw.csv --rev main. Two weeks later a collaborator clones your project and runs dvc pull, but gets different data than you used for training because the source's main branch has moved. Which field in the .dvc file protects against this, and what should you have done to make the intent clearer?
Show answer
The rev_lock field stores the exact source commit that main resolved to at import time, so dvc pull fetches the data from that commit and not from the current tip of main. The different data most likely means someone ran dvc update, which rewrites rev_lock and the hashes, and that change was committed. To make the intent clear, import from a tag or commit such as --rev v1.0, and move to a new version only through a deliberate dvc update --rev that is committed and reviewed.
Exercise
A teammate wants the contents of data/raw.csv from the registry in a notebook, without adding anything to the notebook's Git repository. Which approach fits, and what do they give up compared with dvc import?
Show answer
Use dvc.api.open or dvc.api.read with repo= and rev= set to the registry URL and a tag. This reads the data directly with no files or pointer files in the project. They give up the recorded provenance: no .dvc file is created, so Git does not record which dataset version the notebook used, and dvc update cannot manage it.