Skip to main content

How to Initialize DVC in a Git Repository with dvc init

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

dvc init turns a Git repository into a DVC project. It adds a .dvc folder for DVC's settings and its cache, and a .dvcignore file, and stages them in Git for you. This guide runs it in a small repository, looks at each file it made, commits them, and explains the two errors you're likely to meet, with DVC 3.67.1.

Syntax​

Syntax
dvc init

Run it in the top folder of a Git repository: the folder that holds .git. The dvc init reference says the command "(without flags) expects to run in a Git repository root (a .git/ directory should be present)."

The Example​

recipes is a Git repository with one commit, a README.md. It will hold recipe photos later, which are too big for Git, so it gets DVC first.

Prerequisites​

  • DVC. Run dvc --version to check; this guide uses DVC 3.67.1.

Step 1: Run dvc init​

In the repository's top folder:

Terminal
dvc init
Output
Initialized DVC repository.

You can now commit the changes to git.

What's next?
------------
- Check out the documentation: <https://dvc.org/doc>
- Get help and share ideas: <https://dvc.org/chat>
- Star us on GitHub: <https://github.com/treeverse/dvc>

The terminal after dvc init: Initialized DVC repository, a reminder to commit the changes to git, and a What's next list with three links.

If DVC's usage analytics are on, a red box about them comes first. This run had them off.

Step 2: See What It Changed​

Terminal
git status --short
Output
A .dvc/.gitignore
A .dvc/config
A .dvcignore

The terminal after git status --short: three new files, .dvc/.gitignore, .dvc/config and .dvcignore, each marked A for added.

A in the first column means the file is already staged: the reference says the .dvc folder "is automatically staged with git add, so it can be easily committed with Git." Here's what each file is:

FileWhat it's for
.dvc/configDVC's settings for this project, empty for now. The internal files page calls it "the default DVC configuration file"
.dvc/.gitignoreKeeps DVC's own private files out of Git
.dvcignorePatterns for files DVC should skip, like .gitignore for Git

Step 3: Look Inside the .dvc Folder​

Terminal
ls -a .dvc
Output
. .. .gitignore config tmp
Terminal
cat .dvc/.gitignore
Output
/config.local
/tmp
/cache

The terminal after ls -a .dvc, which lists .gitignore, config and tmp, and cat .dvc/.gitignore, which lists /config.local, /tmp and /cache.

PowerShell lists the same three entries, one per line, and cat works there too. .dvc/.gitignore keeps three things out of Git:

  • /config.local: a settings file for values that must stay on your computer, such as passwords. It doesn't exist until you use it.
  • /tmp: DVC's temporary files.
  • /cache: the cache, where DVC keeps a copy of every version of your data. It appears the first time you track a file.

.dvcignore holds only comments for now:

.dvcignore
# Add patterns of files dvc should ignore, which could improve
# the performance. Learn more at
# https://dvc.org/doc/user-guide/dvcignore

Step 4: Commit the DVC Files​

The files are staged, so commit them:

Terminal
git commit -m "Initialize DVC"
Output
[main 5651922] Initialize DVC
3 files changed, 6 insertions(+)
create mode 100644 .dvc/.gitignore
create mode 100644 .dvc/config
create mode 100644 .dvcignore

The terminal after git commit -m "Initialize DVC": commit 5651922 on main with three files, .dvc/.gitignore, .dvc/config and .dvcignore.

Now everyone who clones the repository gets a DVC project. The command works unchanged in PowerShell.

Error: '.dvc' exists​

Running dvc init a second time stops:

Output
ERROR: failed to initiate DVC - '.dvc' exists. Use `-f` to force.

The project is already initialized, so there's nothing to do. Don't reach for -f: the reference says it will "remove .dvc/ if it exists before initialization. Will remove any existing local cache."

Error: Not Tracked by Any Supported SCM Tool​

In a folder that isn't a Git repository, dvc init stops with this:

Output
ERROR: failed to initiate DVC - /Users/you/source/no-git-folder is not tracked by any supported SCM tool (e.g. Git). Use `--no-scm` if you don't want to use any SCM or `--subdir` if initializing inside a subdirectory of a parent SCM repository.

SCM means source control, such as Git. The same error appears in a subfolder of a Git repository, here recipes/notes, because dvc init wants the repository's top folder:

Output
ERROR: failed to initiate DVC - /Users/you/source/recipes/notes is not tracked by any supported SCM tool (e.g. Git). Use `--no-scm` if you don't want to use any SCM or `--subdir` if initializing inside a subdirectory of a parent SCM repository.

Run git init first, or move to the top folder. The two options in the message are for special setups: --no-scm for a project without Git, and --subdir for several DVC projects inside one Git repository.

Common Mistakes​

MistakeWhat happensDo this instead
Running dvc init before git initis not tracked by any supported SCM tool (e.g. Git)Run git init first
Running it in a subfolder of the repositoryThe same errorMove to the folder that holds .git
Not committing the new filesPeople who clone the repository don't get the DVC projectgit commit after dvc init

Troubleshooting​

SymptomLikely causeFix
failed to initiate DVC - '.dvc' existsDVC is already set up hereNothing to do; carry on with dvc add
is not tracked by any supported SCM tool (e.g. Git)No Git repository in this foldergit init, or run it in the repository's top folder

FAQ​

Do I need Git to use DVC?​

DVC works without it, through dvc init --no-scm, but the reference says it "works best in a Git repository", because Git is what keeps track of each version of your data.

Where is the cache?​

In .dvc/cache, once you track your first file. .dvc/.gitignore already keeps it out of Git.

Conclusion​

dvc init created .dvc/config, .dvc/.gitignore and .dvcignore, staged them, and one commit made the repository a DVC project. Its two errors mean the project is already set up, or the folder isn't the top of a Git repository.

Additional Resources​

  • dvc init — every option, including --subdir and --no-scm.
  • Internal files — what each file in .dvc is for.