Sub topic 6
Moving between data versions
This chapter assumes you can commit, tag, and check out in Git. It also assumes you have run dvc add and dvc push and can read a .dvc file, which earlier chapters covered. You need a project with a configured remote. The goal here is to move a project back and forth between data states reliably, and to understand what happens in the workspace when you do.
The working model
A DVC project has three places where data lives:
- The workspace. These are the real files you open and edit, such as
data/raw.csv. - The cache. This is the local
.dvc/cachedirectory. Each file is stored under a name derived from the hash of its content. - The remote. This is shared storage that mirrors the cache.
Git never sees file content. It sees only the small pointer files (*.dvc, and dvc.lock for pipeline outputs), each of which records a hash. A "data version" is therefore a Git commit. The pointer files in that commit name the hashes the workspace should contain.
Moving between versions takes two steps, and each tool does one of them:
git checkoutswaps the pointer files.dvc checkoutreads the pointer files and makes the workspace match them, taking content from the cache.
git checkout alone leaves your data files untouched. DVC lists them in .gitignore, so Git does not move them. After a plain git checkout your code and pointers are at the old version while your data is still at the new one. This mismatch causes most of the confusion described later in this chapter.
A worked example
Start with a project that has two committed data versions. The first commit holds the original file, and the second holds a larger file.
git init
dvc init
git commit -m "Initialize DVC"
mkdir -p data
printf "id,label\n1,cat\n2,dog\n" > data/raw.csv
dvc add data/raw.csv
git add data/raw.csv.dvc data/.gitignore
git commit -m "Raw data, first version"
git tag v1.0
printf "id,label\n1,cat\n2,dog\n3,bird\n4,fish\n" > data/raw.csv
dvc add data/raw.csv
git add data/raw.csv.dvc
git commit -m "Raw data, second version"
git tag v2.0The second dvc add stores the new content in the cache under a new hash. The old content stays in the cache under its own hash, so it is still available. The only Git-visible change is a new hash in data/raw.csv.dvc.
Now go back to the first version and check the result:
git checkout v1.0
cat data/raw.csv # still shows 4 rows: the workspace has not moved
dvc status # reports that data/raw.csv changed
dvc checkout
cat data/raw.csv # now shows 2 rowsTo return to the newest version, repeat the two steps:
git checkout main
dvc checkoutUse your own default branch name if it is not main.
Inspecting a pointer file
Open the pointer to see exactly what a version contains. git show reads it from any commit without changing your workspace:
git show v1.0:data/raw.csv.dvcThe output has this shape, with your own hash and size:
outs:
- md5: <hash-of-v1.0-content>
size: 22
hash: md5
path: raw.csvThe path is relative to the .dvc file's directory. The md5 value is the cache lookup key. Because the hash identifies the content, two versions that happen to contain the same bytes share one cache entry.
What `dvc checkout` does and does not do
dvc checkout compares each tracked output in the workspace with what the pointer says, and fixes any difference:
- If the file is missing or has different content, DVC replaces it with the cached version.
- If the file already matches, DVC leaves it alone, so repeated checkouts are cheap.
- For a tracked directory, DVC reads a manifest of the files the version contains. It adds the missing files, replaces the changed ones, and removes files that are not part of that version.
It does not contact the remote. If a needed hash is not in the local cache, the checkout of that output fails and reports the missing files. The fix is to download them with dvc pull.
Fetching versus checking out
dvc pull is two operations combined:
dvc fetchdownloads content from the remote into the cache.dvc checkoutlinks or copies from the cache into the workspace.
On a fresh clone, or when switching to a version nobody on your machine has used before, you normally want the combined form:
git checkout v1.0
dvc pullUse dvc fetch on its own when you want to prepare data ahead of time. For example, a CI job can fetch the cache and check out later. You can also restrict either command to one target, as in dvc pull data/raw.csv.dvc, to avoid downloading unrelated large files.
Restoring a single file
You do not have to move the whole project. To restore one tracked file to its old state while everything else stays put, take the old pointer from Git, then check out that one target:
git checkout v1.0 -- data/raw.csv.dvc
dvc checkout data/raw.csv.dvcYour Git working tree now contains a modified data/raw.csv.dvc. You can commit it to make the rollback permanent, or run git checkout HEAD -- data/raw.csv.dvc followed by dvc checkout to undo it.
Looking at an old version without switching
Sometimes you only need to compare, or to read one file from the past. Two commands avoid a full checkout:
dvc diff v1.0 v2.0
dvc get . data/raw.csv --rev v1.0 -o raw_v1.csvdvc diff lists which tracked files were added, modified, or deleted between two Git revisions. dvc get with . as the source reads from the current repository. It writes the file as it existed at --rev to a path you choose, and it does not touch your tracked workspace file. DVC pulls the data from the remote if it is not in your cache.
How files get from the cache into the workspace
The cache and the workspace are separate copies of the data unless DVC links them. The cache.type setting controls how a checkout materializes a file:
| Link type | Behavior | Trade-off |
|---|---|---|
| `reflink` | Copy-on-write clone | No extra disk use and safe to edit, but only some filesystems support it (for example APFS, Btrfs, XFS) |
| `copy` | Full duplicate | Works everywhere, but doubles disk use and is slow for large files |
| `hardlink` | Second name for the same data | No extra space, but editing the workspace file edits the cache entry |
| `symlink` | Workspace file points into the cache | Same editing risk, and some tools mishandle symlinks |
The default is reflink,copy, which means DVC tries a reflink and falls back to a copy. If your datasets are tens of gigabytes, a copy on every version switch is slow. You can opt in to links:
dvc config cache.type reflink,symlink
dvc checkout --relink--relink recreates the workspace files using the current link type.
Unsaved changes and prompts
dvc checkout is destructive to workspace data that DVC has not saved. If you edited data/raw.csv and did not run dvc add, the edited content is in no cache. A checkout that replaces the file loses that content for good. Before switching, run:
dvc status
git statusIf a tracked output has changed, run dvc add and commit, or copy the file elsewhere. By default DVC asks for confirmation before removing workspace data that it cannot restore, and dvc checkout --force skips that prompt. Use --force in scripts only when you are certain the workspace holds nothing you want.
Automating the second step
Forgetting dvc checkout after git checkout is the most common mistake. DVC can install Git hooks that remove it:
dvc installThis adds a post-checkout hook that runs dvc checkout after every git checkout. It also adds a pre-commit hook that checks dvc status and a pre-push hook that runs dvc push. The checkout hook only works from the cache, so missing data still needs dvc pull. Hooks live in .git/hooks, so they are not shared by cloning. Each team member must run dvc install once.
Pipelines and model files
A version switch also restores pipeline outputs. These are the entries listed under outs in dvc.yaml. Their hashes are recorded in dvc.lock, so after git checkout v1.0, dvc checkout restores data/prepared and models/model.pkl as they were when that lock file was committed. The result is a matching set of code, parameters, data, and model.
Outputs declared with cache: false, such as metrics.json in this tutorial, are stored directly in Git. git checkout restores them, and dvc checkout has nothing to do for them.
Common errors
| Symptom | Cause | Fix |
|---|---|---|
| Code is old but data is new | Ran `git checkout` only | Run `dvc checkout`, or install the hook |
| `dvc checkout` reports missing cache files | Data was never fetched on this machine, or was never pushed | `dvc pull`. If the pull also fails, the author never ran `dvc push` |
| `dvc status` shows changes right after checkout | Link type or file mode changed, or a tool rewrote the file | Run `dvc checkout --relink`. If the content really differs, investigate the tool |
| Old data is gone after a switch | Edited the file without `dvc add`, then ran a forced checkout | Not recoverable from DVC. Add data before switching |
| Switch is very slow | `copy` link type on large data | Use `reflink` or a link type from the table above |
Exercise
You run git checkout v1.0 on a teammate's project and then train a model. The results look identical to v2.0, and git status shows a clean tree. What went wrong, which command shows it, and what do you run to fix it? If that fix fails with missing cache files, what do you run next?
Show answer
Only the pointer files and code moved. The data files were never switched, so training used the v2.0 data. dvc status (or dvc data status) shows the tracked outputs as modified relative to the v1.0 pointers. Run dvc checkout to bring the workspace in line. If it reports missing cache files, the v1.0 data is not in the local cache, so run dvc pull to download it from the remote and check it out. Running dvc install once avoids forgetting the checkout.