Skip to main content

Fixed: "bad DVC file name '…' is git-ignored" When You Run dvc add

· 7 min read
Jagdish Kumawat
Founder @ Dewiride

dvc add stopped with ERROR: bad DVC file name 'data/raw/hour.csv.dvc' is git-ignored. before it tracked anything. The project's .gitignore ignored the whole data folder, and with it the .dvc file DVC wanted to create there. Removing the folder-wide rules, and letting DVC write its own .gitignore files, fixed it.

What I Was Doing​

A machine learning project that predicts hourly bike rentals keeps three things it doesn't want in Git: the downloaded dataset, data/raw/hour.csv, the training and test sets it prepares from it, in data/prepared, and the trained model, models/model.skops. Until now, two lines in its .gitignore kept them out:

.gitignore (the end)
# Data and models: the pipeline downloads and creates them
/data/
/models/

The next step was to version all three with DVC (Data Version Control). DVC is installed in the project, so it runs through uv run:

Terminal
uv run dvc add data/raw/hour.csv data/prepared models/model.skops

What I Expected​

Three .dvc files, one next to each target, and DVC's usual hint to git add them.

What I Got​

Output
ERROR: bad DVC file name 'data/raw/hour.csv.dvc' is git-ignored.

The terminal after uv run dvc add data/raw/hour.csv data/prepared models/model.skops: ERROR: bad DVC file name 'data/raw/hour.csv.dvc' is git-ignored.

DVC stopped at the first target, with exit code 1, and created nothing.

What I Checked​

The message names a .dvc file that didn't exist yet, so I asked Git which rule would ignore each of the three .dvc files:

Terminal
git check-ignore -v data/raw/hour.csv.dvc data/prepared.dvc models/model.skops.dvc
Output
.gitignore:13:/data/ data/raw/hour.csv.dvc
.gitignore:13:/data/ data/prepared.dvc
.gitignore:14:/models/ models/model.skops.dvc

The terminal after git check-ignore -v for the three .dvc files: line 13 of .gitignore, /data/, ignores the first two, and line 14, /models/, the third.

-v shows the rule behind each answer: the file, the line number and the pattern. Line 13, /data/, ignores everything inside data, so it would ignore data/raw/hour.csv.dvc and data/prepared.dvc too. Line 14, /models/, does the same for models/model.skops.dvc. How to See Why Git Ignores a File with git check-ignore explains the command.

Why It Happens​

A .dvc file is the only record of which version of the data belongs to a commit, so it has to be committed. A .dvc file that Git ignores is never committed, never pushed, and never reaches anyone who clones the project.

DVC checks for this before it writes the file. In DVC 3.67.1, dvc/dvcfile.py asks Git whether the path is ignored and raises the FileIsGitIgnored error with this message. The check exists because of DVC issue treeverse/dvc#5650: before it, dvc add accepted such a file, and then, in the issue's words, "attempts to push to a remote is met with a silent failure", and "most alarmingly a dvc pull deletes the data from the workspace". treeverse/dvc#5907 closed that issue, and since then dvc add refuses up front.

The Fix​

Remove the folder-wide rules from .gitignore. DVC doesn't need them: dvc add writes a small .gitignore next to each tracked target, which ignores only the data.

.gitignore (the change)
# Virtual environments
.venv
-
-# Data and models: the pipeline downloads and creates them
-/data/
-/models/

The change to .gitignore in a macOS-style code window: four lines removed, the comment about data and models and the rules /data/ and /models/.

After the Fix​

The same command worked:

Terminal
uv run dvc add data/raw/hour.csv data/prepared models/model.skops
Output
100% Adding...|███████████████████████████████████████████████████████████|3/3 [00:00, 21.54file/s]

To track the changes with git, run:

git add data/raw/hour.csv.dvc models/.gitignore data/.gitignore data/prepared.dvc data/raw/.gitignore models/model.skops.dvc

To enable auto staging, run:

dvc config core.autostage true

Git now sees the three .dvc files and the .gitignore files DVC wrote, and none of the data:

Terminal
git status --short --untracked-files=all
Output
M .gitignore
?? data/.gitignore
?? data/prepared.dvc
?? data/raw/.gitignore
?? data/raw/hour.csv.dvc
?? models/.gitignore
?? models/model.skops.dvc

The terminal after the fix: dvc add tracks all three targets, and git status --short --untracked-files=all lists the three .dvc files and three new .gitignore files, without any data file.

Each of DVC's .gitignore files holds one line, for the one thing tracked in its folder:

FileLine
data/raw/.gitignore/hour.csv
data/.gitignore/prepared
models/.gitignore/model.skops

So the data stays out of Git exactly as before, and the .dvc files can be committed.

Common Mistakes​

MistakeWhat happensDo this instead
Ignoring a whole folder that will hold .dvc filesdvc add refuses with is git-ignoredLet dvc add write the ignore rules for the data
Adding a !*.dvc exception insteadA file inside an ignored folder can't be re-included: the gitignore documentation says "It is not possible to re-include a file if a parent directory of that file is excluded."Remove the folder rule

Troubleshooting​

SymptomLikely causeFix
bad DVC file name '…' is git-ignoredA .gitignore rule matches the .dvc file DVC wants to writeFind it with git check-ignore -v <file>.dvc, then remove or narrow it
'…' is git-ignored from dvc removeThe same, for a .dvc file that already existsThe same fix

FAQ​

Do I lose anything by removing /data/ from .gitignore?​

No. dvc add puts each tracked file or folder into a .gitignore next to it, so Git still ignores the data. Files in data that DVC doesn't track would show up in git status, which is a reminder to track or ignore them.

Does the same happen with dvc.yaml?​

The same check runs for dvc.yaml files, which describe pipelines: the error's code is shared by both kinds of DVC file.

Conclusion​

ERROR: bad DVC file name 'data/raw/hour.csv.dvc' is git-ignored. meant that a folder-wide rule in .gitignore, /data/, also ignored the .dvc file DVC needed to write. git check-ignore -v found the rule, removing /data/ and /models/ fixed it, and dvc add wrote its own narrower .gitignore files.

Additional Resources​