Lompat ke konten

Pelajaran 20 ยท Collaboration and CI

Team Workflow: Git Hooks, Branches and Merges

Work together with DVC: onboard teammates, install Git hooks, review data changes in pull requests and resolve conflicts in dvc.yaml, dvc.lock and .dvc files.

DVC shines when several people work on one project. The Git workflow you already use applies, with a few extra habits: push data along with code, review data changes and handle conflicts in DVC files correctly.

Onboarding a teammate

A new person needs the code, access to the data storage and matching tool versions.

Terminal
# Read-only: first day on the project
git clone https://git.example.com/team/review-model
cd review-model
python -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt    # pins dvc and the storage extra
# configure credentials locally, for example:
dvc remote modify --local storage profile data-team
dvc pull
dvc status

Document these steps in the README, including who grants storage access.

Git hooks with dvc install

Terminal
# Read-only: add the hooks
dvc install

This adds three hooks:

Hook What it does
post-checkout Runs dvc checkout after you switch commits or branches
pre-commit Runs dvc status so you notice unrecorded changes
pre-push Runs dvc push before git push

Hooks live in your local .git/hooks and are not shared by Git, so each person installs them. If a hook file already exists, DVC raises an error; edit or remove that file by hand. If your project uses the pre-commit framework, run dvc install --use-pre-commit-tool and install all three hook types with pre-commit.

A pull request with data changes

  1. Create a branch.
  2. Change code, data or parameters, then run dvc repro or dvc exp run.
  3. Commit dvc.yaml, dvc.lock, .dvc files, parameters and small metrics.
  4. Run dvc push, then git push.
  5. In the pull request, paste the output of dvc metrics diff --md and a summary of dvc params diff, and describe what changed in the data.

Reviewers cannot read a hash, so describe the data change: source, row counts, column changes and privacy implications.

Resolve merge conflicts

Different files need different handling.

dvc.yaml: merge it like source code. Keep the correct stages, dependencies and parameters.

dvc.lock: do not hand-edit it. Delete the conflicted file, finish merging dvc.yaml, then run dvc repro to regenerate it. dvc commit can also work when you keep your current version's outputs.

.dvc files from dvc add: the two branches point to different data. Pick one version, or merge the actual data: get both versions into temporary copies, combine them correctly, then run dvc add on the merged file so a new .dvc file replaces the conflict.

For directories where both branches changed different files, DVC provides a Git merge driver:

Terminal
# Read-only: enable DVC's merge driver
git config merge.dvc.name 'DVC merge driver'
git config merge.dvc.driver \
  'dvc git-hook merge-driver --ancestor %O --our %A --their %B'
echo '*.dvc merge=dvc' >> .gitattributes

It fails when both branches added or changed the same file with different content, and in some deletion cases, so you must resolve those by hand.

.dvc files from dvc import or import-url: remove the conflicting values so only the shared fields remain, then run dvc update to fetch from the source again. The result may match neither side, so review it.

Try the idea

This stand-in performs a three-way merge of two folder listings, similar to the decision a directory merge has to make. Real DVC rules differ in details.

Python Stand-in: three-way merge of data listings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Agree on team rules

  • One owner for the shared storage and its access list.
  • Push data in the same session as code.
  • Protect the main branch and require reviews.
  • Tag releases of data and models.
  • Do not run garbage collection on shared storage without telling the team.
  • Record who may add sensitive data.

Common mistake: hand-merging dvc.lock line by line. The result may describe a state that never existed. Regenerate it with dvc repro instead.

For general branching and pull request practice, see the Git tutorial.

A realistic week

On Monday, Asha adds new data on a branch, pushes the data and the commit, and opens a pull request with a short note: new rows from October, no column changes. Ben reviews it. He pulls the branch, runs dvc pull and a validation script, and finds 40 duplicate IDs. He comments with the exact count. Asha fixes the source script, runs dvc add again, pushes data and code, and the review passes.

On Wednesday, Carl changes the training code on another branch. When he merges the main branch, dvc.lock conflicts. He follows the rule: remove the conflicted lock file, finish the merge of dvc.yaml, and run dvc repro. The new lock file reflects Asha's data and his code together. He compares metrics with the main branch and notes the difference in his pull request.

Nobody edited a hash by hand, and every state in history is something that was actually produced. That is the goal of these rules.

Review the lock file too

A lock file is machine-written, yet worth a glance. Unexpected new output hashes for a stage that should not have changed can reveal nondeterminism or an accidental dependency.

When a teammate leaves the loop

Plan for people being away. Everything needed to rebuild a result should be in Git or in shared storage, not on one laptop. Ask yourself whether the project would still work if your computer disappeared today. If the answer is no, push the missing data, commit the missing file or write down the missing step.

Revisit these rules after your first few pull requests. Keep the ones that prevented real problems, and simplify any step that people regularly skip or misunderstand.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which hook installed by dvc install uploads data before code leaves your computer?

    The pre-push hook runs dvc push.
  2. 2. How should you resolve a conflicted dvc.lock?

    Regenerating the lock file keeps it consistent with the real outputs.
  3. 3. Are Git hooks shared automatically with teammates?

    Hooks live in .git/hooks, which Git does not distribute.
  4. 4. What should a data pull request include besides hash changes?

    Reviewers need meaning that hashes cannot give.

Summary

  • Onboarding needs code, pinned tools, storage access and dvc pull.
  • dvc install adds post-checkout, pre-commit and pre-push hooks.
  • Describe data changes in pull requests.
  • Merge dvc.yaml normally, regenerate dvc.lock, and handle .dvc conflicts by type.
  • Agree on access, release and cleanup rules.

Next: Run DVC in CI

Progres Anda disimpan di browser ini. Tidak perlu akun.