Skip to main content

How to Save a New Version of a DVC-Tracked File

· 8 min read
Jagdish Kumawat
Founder @ Dewiride

When a file DVC tracks changes, the change isn't saved anywhere yet: the .dvc file still points to the old version. Saving the new one takes three commands: dvc add, git commit and dvc push. This guide adds a customer to a tracked CSV file, saves the new version with DVC 3.67.1, and shows that the old version is still kept.

Syntax​

Syntax
dvc add <file>
git add <file>.dvc
git commit -m "<message>"
dvc push
PartWhat it means
dvc add <file>Hashes the changed file, copies the new version into the cache, and writes the new hash into <file>.dvc
git add <file>.dvcStages the updated .dvc file
git commit -m "<message>"Saves it in Git. This commit is the new version, and <message> says what changed
dvc pushUploads the new version to the remote, if the project has one

DVC's Get Started guide puts it in one sentence: "After modifying the data, run dvc add again to track the latest version".

The Example​

customer-list is a Git repository where dvc init has run. Its data/customers.csv, a list of three customers, is tracked with dvc add, committed, and pushed to the project's default remote, a folder in the home folder.

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 File​

Add a fourth customer, then check what DVC sees:

Terminal
echo "C-004,Dana,Lyon" >> data/customers.csv
dvc status
Output
data/customers.csv.dvc:
changed outs:
modified: data/customers.csv

The terminal after a new customer line is added to data/customers.csv: dvc status reports the file as modified.

modified means the file no longer matches the hash in its .dvc file. Until you save it, the change exists only in this one file on your disk.

Step 2: Save the New Version with dvc add​

Terminal
dvc add data/customers.csv
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 78.83file/s]

To track the changes with git, run:

git add data/customers.csv.dvc

To enable auto staging, run:

dvc config core.autostage true

See what changed in the .dvc file:

Terminal
git diff data/customers.csv.dvc
Output
diff --git a/data/customers.csv.dvc b/data/customers.csv.dvc
index 78ae69e..69a777b 100644
--- a/data/customers.csv.dvc
+++ b/data/customers.csv.dvc
@@ -1,5 +1,5 @@
outs:
-- md5: 5c612b4edea5df90d64bd4632fd556e1
- size: 61
+- md5: 0bcef2d0b86f01a19edce9285d87995d
+ size: 77
hash: md5
path: customers.csv

The terminal after dvc add data/customers.csv and git diff data/customers.csv.dvc: the md5 changed from 5c612b4e to 0bcef2d0, and the size from 61 to 77 bytes.

Two lines changed: the new md5, and the new size, 16 bytes more for the line C-004,Dana,Lyon and its line break. The new version is now in the cache, and the old one is still there too. If you follow along on Windows, your hash and size can differ from these, because Windows can end each line with two characters instead of one. What matters is that both change. The commands are the same in PowerShell.

Step 3: Commit the .dvc File​

Run the command DVC suggested, and commit:

Terminal
git add data/customers.csv.dvc
git commit -m "Add customer Dana"
Output
[main e93bc12] Add customer Dana
1 file changed, 2 insertions(+), 2 deletions(-)

The terminal after git add data/customers.csv.dvc and git commit, which saves commit e93bc12 with 2 insertions and 2 deletions.

This commit is the new version: it points to the data with four customers, while the commit before it still points to the data with three.

Step 4: Upload the New Version​

Terminal
dvc push
Output
Collecting |1.00 [00:00, 771entry/s]
Pushing
1 file pushed

The terminal after dvc push, which uploads 1 file.

One file went up: the new version. The old one was pushed when the file was first tracked.

Step 5: See Both Versions​

The Git history has a commit for each version:

Terminal
git log --oneline
Output
e93bc12 (HEAD -> main) Add customer Dana
3da4bcb Store DVC data in dvc-storage
b52fdf1 Track customers.csv with DVC
be352b5 Initialize DVC

And the remote holds both files:

Terminal
find $HOME/dvc-storage -type f
Output
/Users/you/dvc-storage/files/md5/0b/cef2d0b86f01a19edce9285d87995d
/Users/you/dvc-storage/files/md5/5c/612b4edea5df90d64bd4632fd556e1

The terminal after git log --oneline, which lists four commits ending with Add customer Dana, and find $HOME/dvc-storage -type f, which lists two files, one for each version.

Each file is named after a version's hash: 5c/612b4e… is the first version, from the - line of the diff in Step 2, and 0b/cef2d0… the new one. Commit b52fdf1 points to the first and e93bc12 to the second, so checking out an older commit brings its data back: How to Go Back to an Earlier Version of Your Data with dvc checkout shows how.

Skip the git add Hint with Autostage​

Every dvc add ends with the git add you need to run next. The core.autostage setting makes DVC run it for you. The configuration reference says: "if enabled, DVC will automatically stage (git add) DVC files created or modified by DVC commands. The files will not be committed."

Turn it on, and change the file once more:

Terminal
dvc config core.autostage true
echo "C-005,Eli,Bern" >> data/customers.csv
Terminal
dvc add data/customers.csv
Output
100% Adding...|███████████████████████████████████████████████████████████|1/1 [00:00, 60.53file/s]
Terminal
git status --short
Output
M .dvc/config
M data/customers.csv.dvc

The terminal after dvc config core.autostage true, a new line, and dvc add, which prints only its progress bar, then git status --short, where data/customers.csv.dvc is already staged.

dvc add printed no hint, and data/customers.csv.dvc is already staged: the M in the first column means staged. .dvc/config is changed but not staged, because the setting itself was saved there. Commit it too, and everyone who clones the project gets the setting. You still write the commit yourself.

Common Mistakes​

MistakeWhat happensDo this instead
Committing without dvc addThe .dvc file still points to the old version, and the change isn't saveddvc add first, then commit
Running dvc add but not committingThe cache has the new version, but no commit points to itCommit the .dvc file
Committing but not pushingClones get the new .dvc file, but dvc pull can't find its datadvc push after the commit

Troubleshooting​

SymptomLikely causeFix
dvc status shows modified after a commitThe file changed again, or dvc add was skippeddvc add the file and commit again
git status shows nothing after a changeGit ignores the data file; only dvc add updates the .dvc fileRun dvc add

FAQ​

Does DVC keep the old version?​

Yes. The cache, and the remote after dvc push, keep every version you saved with dvc add, each under its own hash, as Step 5 shows.

Do I need dvc push for every version?​

Only to share it or to get it back on another computer. Without a push, the version exists only in your own cache.

Conclusion​

dvc add data/customers.csv saved the changed file in the cache and wrote its new hash and size into the .dvc file. A commit made that the new version, and dvc push uploaded it, so the remote held both versions, one for each commit. With core.autostage on, dvc add staged the .dvc file itself.

Additional Resources​