Sub topic 2
Setting up a DVC project
Assume you know basic Git commands, can use a terminal, and have DVC installed. DVC, or Data Version Control, is a tool that connects large data and model files to Git without storing their full contents in Git. This chapter creates the project structure that later chapters will use.
Why initialize DVC?
Git works well for source code and small text files. Large datasets and trained models create different problems:
- A dataset can be too large for a practical Git repository.
- Binary files do not produce useful line-by-line diffs.
- Every changed copy can increase repository storage.
- A teammate needs both the Git files and the matching data files to reproduce a result.
DVC keeps small metadata files in Git. These metadata files identify the exact data or model version. The actual file contents live in a local DVC cache and, optionally, remote storage.
The basic relationship looks like this:
Create a Git repository
DVC expects a project to use Git. Start with an empty directory:
mkdir dvc-demo
cd dvc-demo
git initA Git repository now exists, but it has no commits. You can add a simple project description and make the first commit:
printf "# DVC demo\n" > README.md
git add README.md
git commit -m "Create project"If Git asks for your identity, configure a name and email first:
git config user.name "Your Name"
git config user.email "[email protected]"These settings affect commit metadata. They do not send anything to a remote server.
Initialize DVC
Run dvc init from the root directory of the Git repository:
dvc initThe command creates a .dvc directory. It also updates Git-related configuration so that DVC's generated and cached files are handled correctly.
Inspect the result:
git status
find .dvc -maxdepth 2 -type f -printThe .dvc directory contains project configuration. Some files are intended for Git, while local cache contents are ignored. The exact file list can vary slightly between DVC versions.
Commit the initialization:
git add .dvc
git commit -m "Initialize DVC"At this point, Git records that the project uses DVC. No dataset has been added yet.
Check the installation
Use dvc doctor to print information about the DVC installation and environment:
dvc doctorYou can also ask DVC for its version:
dvc --versionIf the shell reports that dvc is not found, install DVC using the method recommended for your operating system or Python environment. A project can be initialized only after the command is available.
Understand the initial layout
After initialization, the important project relationship is:
| Item | Stored by | Purpose |
|---|---|---|
| Source code and DVC metadata | Git | Defines the project and its history |
| Large data and model contents | DVC cache or remote | Stores file contents efficiently |
| `.dvc` project configuration | Git | Describes how the DVC project operates |
Initialization is a one-time project setup step. The next step is to add a dataset with dvc add. That command creates a pointer file that Git can version while DVC manages the dataset contents.
Exercise
Which command initializes DVC, and where must you run it?
Show answer
Run dvc init from the root directory of an existing Git repository.