Skip to main content

How to Go Back to an Earlier Version of Your Data with dvc checkout

· 9 min read
Jagdish Kumawat
Founder @ Dewiride

Every commit of a DVC project points to one version of its data, through its .dvc files. Going back takes two commands: git checkout brings back the old .dvc file, and dvc checkout brings back the data it points to. This guide does it for one file and for the whole project with DVC 3.67.1, and returns to the newest version each time.

Syntax​

Syntax
git checkout <commit> -- <file>.dvc
dvc checkout <file>.dvc
PartWhat it means
git checkout <commit> -- <file>.dvcPuts the .dvc file as it was in <commit> into the project folder. -- separates the commit from the file's path
<commit>A commit hash, or HEAD~1 for the commit before the last one
dvc checkout <file>.dvcReplaces the data file with the version that .dvc file points to, taken from the cache

DVC's Get Started guide describes it as "a commonly used workflow": "use git checkout to switch to a branch or checkout a specific .dvc file revision, followed by a dvc checkout to sync data into your workspace". It adds that "DVC is technically not a version control system by itself! It manipulates .dvc files, whose contents define the data file versions."

The Example​

exam-scores tracks data/scores.csv with DVC, and has two versions of it, one in each commit: the first exam scores of Ana, Ben and Cara, and a later commit that corrects Ben's score and adds Dana.

Prerequisites​

  • DVC, in a Git repository where dvc init has run. Run dvc --version to check; this guide uses DVC 3.67.1.

Step 1: See the History and the Current Data​

Terminal
git log --oneline
Output
a35f5a3 (HEAD -> main) Correct Ben and add Dana
abac74a Add the first exam scores
3c7a6cd Initialize DVC
Terminal
cat data/scores.csv
Output
student,score
Ana,71
Ben,68
Cara,88
Dana,79

The terminal after git log --oneline, which lists three commits, and cat data/scores.csv, which shows the newest scores: Ana 71, Ben 68, Cara 88 and Dana 79.

The project folder holds the newest version, from a35f5a3. The goal is the version from abac74a, the commit before it.

Step 2: Get One File's Earlier Version​

Terminal
git checkout HEAD~1 -- data/scores.csv.dvc
dvc checkout data/scores.csv.dvc
Output
Building workspace index |2.00 [00:00, 328entry/s]
Comparing indexes |3.00 [00:00, 3.16kentry/s]
Applying changes |1.00 [00:00, 1.42kfile/s]
M data/scores.csv
Terminal
cat data/scores.csv
Output
student,score
Ana,71
Ben,64
Cara,88

The terminal after git checkout HEAD~1 -- data/scores.csv.dvc and dvc checkout data/scores.csv.dvc, which reports M data/scores.csv, then cat data/scores.csv, which shows the first scores: Ben 64 and no Dana.

git checkout printed nothing: it replaced data/scores.csv.dvc with its version from HEAD~1. dvc checkout then replaced the data to match, and M means it modified data/scores.csv. The file has the first scores again: Ben's 64, and no Dana. DVC took that version from its cache, without downloading anything.

The commands are the same in PowerShell, which passes HEAD~1 and -- to Git unchanged (checked in PowerShell 7.6).

Step 3: See What Changed in Git​

Terminal
git status --short
Output
M data/scores.csv.dvc

The terminal after git status --short, which shows data/scores.csv.dvc as staged with M in the first column.

The old .dvc file is staged, ready to commit. From here you have two choices:

  • Keep the old version as the current one: commit the .dvc file. The Get Started guide does it with git commit data/data.xml.dvc -m "Revert dataset updates", adding that there is "no need to do dvc push this time since this original version of the dataset was already saved".
  • Return to the newest version, as the next step does.

Step 4: Return to the Newest Version​

HEAD is the last commit, so the same two commands with HEAD bring the newest version back:

Terminal
git checkout HEAD -- data/scores.csv.dvc
dvc checkout data/scores.csv.dvc
Output
Building workspace index |2.00 [00:00, 453entry/s]
Comparing indexes |3.00 [00:00, 3.15kentry/s]
Applying changes |1.00 [00:00, 1.42kfile/s]
M data/scores.csv
Terminal
cat data/scores.csv
Output
student,score
Ana,71
Ben,68
Cara,88
Dana,79

The terminal after git checkout HEAD -- data/scores.csv.dvc and dvc checkout, then cat data/scores.csv, which shows the newest scores again, with Dana.

Go Back with the Whole Project​

To see the whole project as it was at an older commit, with its code, its .dvc files and its data, check out the commit itself:

Terminal
git checkout HEAD~1
Output
Note: switching to 'HEAD~1'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:

git switch -c <new-branch-name>

Or undo this operation with:

git switch -

Turn off this advice by setting config variable advice.detachedHead to false

HEAD is now at abac74a Add the first exam scores

The terminal after git checkout HEAD~1: Git's note about the detached HEAD state, ending with HEAD is now at abac74a Add the first exam scores.

"Detached HEAD" means you're looking at a commit rather than a branch; How to Check Out an Earlier Commit in Git explains it. Git changed the .dvc file, but not the data, which Git doesn't track. dvc status shows the mismatch, and dvc checkout fixes it:

Terminal
dvc status
Output
data/scores.csv.dvc:
changed outs:
modified: data/scores.csv
Terminal
dvc checkout
Output
Building workspace index |2.00 [00:00, 630entry/s]
Comparing indexes |3.00 [00:00, 3.42kentry/s]
Applying changes |1.00 [00:00, 1.62kfile/s]
M data/scores.csv
Terminal
cat data/scores.csv
Output
student,score
Ana,71
Ben,64
Cara,88

The terminal after dvc status, which reports data/scores.csv as modified, dvc checkout, which updates it, and cat data/scores.csv, which shows the first scores.

Without a target, dvc checkout updates every DVC-tracked file in the project. To return, switch back to the main branch and run dvc checkout again:

Terminal
git checkout main
Output
Previous HEAD position was abac74a Add the first exam scores
Switched to branch 'main'
Terminal
dvc checkout
Output
Building workspace index |2.00 [00:00, 599entry/s]
Comparing indexes |3.00 [00:00, 3.24kentry/s]
Applying changes |1.00 [00:00, 1.31kfile/s]
M data/scores.csv
Terminal
cat data/scores.csv
Output
student,score
Ana,71
Ben,68
Cara,88
Dana,79
Terminal
dvc status
Output
Data and pipelines are up to date.

The terminal after git checkout main, dvc checkout, cat data/scores.csv with the newest scores, and dvc status, which says Data and pipelines are up to date.

Everything is back on the newest version. The commands in this section are the same in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Running only git checkoutThe .dvc file is old, but the data is still the newest version, and dvc status shows modifiedRun dvc checkout after every git checkout
Running dvc checkout over a file with unsaved changesDVC refuses with Can't remove the following unsaved files without confirmationdvc add the change first to keep it, or see the fix below
Staying in detached HEADNew commits don't belong to any branchgit checkout main when you're done looking

Troubleshooting​

SymptomLikely causeFix
modified: right after git checkoutThe data wasn't switched yetdvc checkout
Can't remove the following unsaved files without confirmationThe data file has changes that aren't saved with dvc addFixed: "Can't remove the following unsaved files without confirmation" in dvc checkout
You are in 'detached HEAD' stateYou checked out a commit, not a branchgit checkout main to return

FAQ​

Does dvc checkout download anything?​

No. It takes the version from the cache: the dvc checkout reference says it "restores the corresponding versions of all DVC-tracked data files and directories from the cache to the workspace." In a fresh clone, where the cache is empty, dvc pull downloads the data first.

Can Git run dvc checkout for me?​

The reference says: "For convenience, a Git hook is available to automate running dvc checkout after git checkout." The dvc install command sets it up.

Conclusion​

git checkout HEAD~1 -- data/scores.csv.dvc and dvc checkout data/scores.csv.dvc brought back one file's earlier version from the cache, and the same two commands with HEAD returned to the newest. git checkout HEAD~1 moved the whole project to the older commit, where dvc checkout matched the data to it, and git checkout main with dvc checkout brought everything back.

Additional Resources​