Zum Inhalt springen

Lektion 5 · Versioning Data

Switch Between Data Versions with checkout

Restore old datasets with git checkout and dvc checkout, compare revisions with dvc diff, inspect your workspace with dvc data status and fix missing files.

Versioning pays off when you can go back. Because Git stores the .dvc files, every commit remembers which data belonged to it. This lesson shows how to travel between versions and how to check where you are.

Two steps: Git, then DVC

git checkout changes the code and the .dvc files. It does not change your data. dvc checkout then makes the data match the .dvc files and dvc.lock.

Terminal
# Read-only: go back to the data from two commits ago
git log --oneline -5
git checkout HEAD~2
dvc checkout
# ...inspect or rerun something...
git switch -          # return to your branch
dvc checkout

To restore the old data but keep your current code, check out only the pointer file:

Terminal
# Read-only: restore one dataset, keep everything else
git checkout HEAD~2 -- data/reviews.csv.dvc
dvc checkout data/reviews.csv.dvc
git commit -m "Roll back reviews data"

The commit at the end records your decision, so teammates see the rollback in history.

What dvc checkout does

It reads the pointer files, takes the matching objects from the cache, and links or copies them into the workspace. It also removes workspace files that do not match. Useful flags:

Flag Effect
--summary Short summary instead of the full list
-f, --force Remove workspace files without asking
--relink Rebuild links to match the current cache.type
--allow-missing Succeed even if some objects are missing
-R Search folders for .dvc and dvc.lock files
-d Also restore upstream data of the targets

If an object is not in the local cache, checkout reports it and cannot restore that file. Run dvc pull to download it from the remote, or dvc repro to rebuild it when a pipeline produces it.

Install Git hooks with dvc install. A post-checkout hook then runs dvc checkout automatically. The team workflow lesson explains all three hooks.

Compare revisions with dvc diff

dvc diff answers "which tracked data changed?" between two Git revisions, or between a revision and your workspace.

Terminal
# Read-only: what changed since the last release tag?
dvc diff v1.0
dvc diff v1.0 v1.1

It reports paths as added, modified, deleted or renamed. It compares hashes and paths. It does not show which rows changed inside a CSV. For that you need your own comparison script, perhaps built on a small key-based diff like the one below.

Check the workspace

Two commands describe the current state:

  • dvc status reports changed dependencies and outputs of pipelines and tracked files. With -c it compares with a remote instead.
  • dvc data status groups changes into sections such as not in cache, uncommitted changes and committed changes. Add --granular to see individual files inside tracked folders, --untracked-files to list files that neither tool tracks, and --not-in-remote to find data you still need to push.

"DVC committed changes" means DVC has recorded the change in a pointer or lock file, which now waits for a Git commit. "Uncommitted changes" means the data changed and DVC has not recorded it yet.

Try the idea

This stand-in compares two folder listings like a very small dvc diff, then shows the rows that changed in one CSV, which real dvc diff does not do.

Python Stand-in: compare two data versions

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.

Mark important versions

Hashes are not memorable. Use Git tags for versions that matter:

Terminal
# Read-only: name a data version
git tag -a data-2026-11 -m "Reviews through November 2026"
git push origin data-2026-11
dvc push

A tag plus pushed data lets anyone with access recover that version later. Write in the tag message what changed and why, because the hash alone cannot explain it.

Common mistake: running git checkout and then assuming the data changed too. Run dvc checkout (or install the hook). Otherwise you may run old code on new data.

Missing data checklist

  1. Run dvc data status to see what is not in the cache.
  2. Run dvc pull to download from the default remote.
  3. If the pull fails, someone may have forgotten dvc push. Ask them to run it.
  4. If a pipeline produces the file, dvc repro can rebuild it, but the result may differ if the code is not deterministic.

A worked investigation

Imagine that the weekly report suddenly shows far fewer rows. You have tagged each release, so you can narrow the cause step by step:

  1. Run git log --oneline -- data/reviews.csv.dvc to list the commits that changed this dataset.
  2. Run dvc diff <older> <newer> to confirm which tracked paths differ.
  3. Check out the earlier pointer file into your workspace, run dvc checkout on it, and count the rows.
  4. Copy that old file to a temporary name, restore the current version the same way, and compare the two with your own script.
  5. Put the pointer file back with git checkout HEAD -- data/reviews.csv.dvc and run dvc checkout again.

The last step matters. After exploring, leave the workspace matching your current commit, so you do not accidentally commit an old pointer.

Keep the habit simple

Most days you only need three commands: git switch, dvc checkout and dvc data status. Run the last one whenever something looks odd. It tells you whether the problem is missing data, unrecorded changes or work waiting for a commit.

Switching branches with data

Branches work the same way as older commits. When you switch to a colleague's branch, run dvc checkout, and dvc pull if their data is not yet in your cache. When you switch back, run dvc checkout again. The post-checkout hook saves you from forgetting.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. You ran git checkout to an old commit. What brings the matching data into the workspace?

    Git restores pointer files; dvc checkout restores the data they describe.
  2. 2. dvc checkout says an object is missing from the cache. What is a sensible next step?

    The object may be on the remote. Pulling downloads it.
  3. 3. What does dvc diff tell you?

    DVC compares tracked paths and hashes, not file contents line by line.
  4. 4. How do you restore only one old dataset while keeping the current code?

    Restoring just the pointer file changes only that data.

Summary

  • Restoring a version takes git checkout followed by dvc checkout.
  • You can restore one pointer file to roll back a single dataset.
  • dvc diff compares revisions; dvc data status describes the current state.
  • Missing objects come back with dvc pull or, for pipeline outputs, dvc repro.
  • Tag important data versions and push the data with them.

Next: DVC Remotes: push and pull

Dein Fortschritt wird in diesem Browser gespeichert. Kein Konto nötig.