Skip to main content

How to Compare Data Versions with dvc diff

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

git diff on a .dvc file shows only that a hash changed. dvc diff tells you which data files were added, modified or deleted, even inside a tracked folder. This guide compares the project folder with the last commit, then two commits, then prints the result as a Markdown table, with DVC 3.67.1.

Syntax​

Syntax
dvc diff [--md] [<a_rev>] [<b_rev>]
PartWhat it means
dvc diffLists the DVC-tracked files and folders that were added, modified or deleted
--mdOptional: prints the list as a Markdown table
<a_rev>Optional: the older Git revision, such as a commit hash, a branch name or HEAD~1. Without it, DVC uses HEAD, the last commit
<b_rev>Optional: the newer revision. Without it, DVC uses the project folder as it is now

The dvc diff reference says: "It defaults to comparing the current workspace and the last commit (HEAD), if arguments a_rev and b_rev are not specified."

The Example​

survey-results is a Git repository where dvc init has run. It tracks two things with dvc add: data/answers.csv, the answers of a small survey that Ana and Ben have filled in, and the folder data/forms, with one form file for each person. Both are committed, so dvc diff prints nothing yet.

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: Change the Data and Compare​

Cara answers the survey too. Add her line to the answers and her form to the folder:

Terminal
echo "Cara,yes" >> data/answers.csv
echo "Form 3: Cara" > data/forms/form-3.txt

Compare the project folder with the last commit:

Terminal
dvc diff
Output
Added:
data/forms/form-3.txt

Modified:
data/answers.csv
data/forms/

files summary: 1 added, 1 modified

The terminal after two echo commands and dvc diff: data/forms/form-3.txt is added, data/answers.csv and data/forms/ are modified, and the summary says 1 added, 1 modified.

dvc diff looks inside the tracked folder:

  • Added: data/forms/form-3.txt: a new file inside data/forms.
  • Modified: data/answers.csv: the file has a new line, so its hash changed.
  • Modified: data/forms/: the folder changed because it holds a new file. The trailing / marks a folder.
  • files summary counts files only, so the folder isn't in it.

The PowerShell lines gave byte-for-byte the same files, and the same dvc diff output, in PowerShell 7.6 on macOS.

Step 2: Save the Change and Compare Again​

Save the new version of both, and commit the two .dvc files:

Terminal
dvc add data/answers.csv data/forms
Output
100% Adding...|███████████████████████████████████████████████████████████|2/2 [00:00, 97.17file/s]

To track the changes with git, run:

git add data/answers.csv.dvc data/forms.dvc

To enable auto staging, run:

dvc config core.autostage true
Terminal
git commit -am "Add Cara's answer and form"
Output
[main f21beb6] Add Cara's answer and form
2 files changed, 5 insertions(+), 5 deletions(-)

-a stages every changed file Git already tracks, here the two .dvc files, before it commits. Compare again:

Terminal
dvc diff

The terminal after dvc add data/answers.csv data/forms, git commit -am, which saves commit f21beb6, and dvc diff, which prints nothing.

dvc diff prints nothing now: the project folder matches the last commit. The commands are the same in PowerShell.

Step 3: Compare Two Commits​

To see what a commit changed, compare it with the one before it. List the commits first:

Terminal
git log --oneline
Output
f21beb6 (HEAD -> main) Add Cara's answer and form
2d78992 Track the survey answers and forms
189a014 Initialize DVC

Then compare the last commit, HEAD, with the one before it, HEAD~1:

Terminal
dvc diff HEAD~1 HEAD
Output
Added:
data/forms/form-3.txt

Modified:
data/answers.csv
data/forms/

files summary: 1 added, 1 modified

The terminal after git log --oneline, which lists three commits, and dvc diff HEAD~1 HEAD, which lists the same added and modified files as before the commit.

HEAD is the last commit, f21beb6, and HEAD~1 is the one before it, 2d78992. The result is the same list as in Step 1, because that commit saved exactly those changes. The reference says both revisions accept "any Git revision - branch or tag name, Git commit hash, etc." The commands are the same in PowerShell.

Step 4: Print a Markdown Table​

Terminal
dvc diff --md HEAD~1 HEAD
Output
| Status | Path |
|----------|-----------------------|
| added | data/forms/form-3.txt |
| modified | data/answers.csv |
| modified | data/forms/ |

The terminal after dvc diff --md HEAD~1 HEAD, which prints a Markdown table with a Status and a Path column.

Paste it into a pull request, an issue or a README, and it shows as a table:

StatusPath
addeddata/forms/form-3.txt
modifieddata/answers.csv
modifieddata/forms/

Common Mistakes​

MistakeWhat happensDo this instead
Using git diff to see data changesIt shows only a changed hash in a .dvc filedvc diff
Taking no output for an errorIt means nothing changed between the twoCheck the revisions with git log --oneline
Putting the newer commit firstdvc diff HEAD HEAD~1 lists the new form as DeletedThe older revision first, then the newer one

Troubleshooting​

SymptomLikely causeFix
dvc diff prints nothingNo DVC-tracked file changedExpected; compare other revisions if you meant to
A folder is listed as ModifiedA file inside it was added, changed or removedThe file's own line, above or below, says which

FAQ​

Does dvc diff show which lines changed?​

No. It lists files, not lines. To read an earlier version of a file, get it back with git checkout and dvc checkout, as How to Go Back to an Earlier Version of Your Data with dvc checkout shows.

Can it show the hashes?​

Yes: the reference says --show-hash prints "file and directory hash values along with their path".

Conclusion​

dvc diff compared the project folder with the last commit and listed the new form as added and the answers and the forms folder as modified. After dvc add and a commit it printed nothing, dvc diff HEAD~1 HEAD showed what that commit changed, and --md printed the same list as a Markdown table.

Additional Resources​

  • dvc diff — every option, including --json, --show-hash and --targets.