Lesson 1 of 3 Source Control and GitHub Actions for MLOps

Git for Machine Learning Projects

This lesson explains what belongs in a machine learning repository, how branches and pull requests protect it, and how jobs record their Git commit.

Learning outcomes

After completing this lesson, you will be able to:

  • Identify what to commit and what to reference as assets.
  • Describe ignore files and notebook hygiene.
  • Explain what pull request checks protect.
  • Describe how jobs record Git information.

What belongs in the repository

A machine learning repository holds everything needed to reproduce the project except the data and the trained artifacts. Data and models are large and change separately, so they are stored as versioned assets in Azure Machine Learning and referenced by name and version from files in Git.

Commit to Git
CodeTraining, scoring and data preparation scripts; tests
DefinitionsJob, component, pipeline, environment, endpoint and compute YAML; Bicep; workflows; prompts
Data and modelsReferences to asset names and versions
SecretsNever
Choose when Small, reviewable text that defines the project
Keep out of Git
CodeGenerated files and caches
DefinitionsNothing
Data and modelsDatasets, model binaries, checkpoints
SecretsKeys, passwords, connection strings, tokens
Choose when Large, sensitive or generated content

A .gitignore file excludes data folders, virtual environments and outputs, and an .amlignore file excludes files from the code snapshot that Azure Machine Learning uploads with each job. Notebook outputs should be cleared before committing, for example with a pre-commit hook such as nbstripout, because outputs can contain data and make diffs unreadable.

Repository structure

A predictable layout helps automation find what it needs. The following structure is typical of an MLOps repository that contains both infrastructure and machine learning definitions.

.github/workflows/      # CI, training and deployment workflows
infra/                  # Bicep templates and per-environment parameter files
src/                    # Python packages: data prep, training, scoring
components/             # Component YAML files
pipelines/              # Pipeline job definitions
environments/           # Environment YAML and conda files
endpoints/              # Endpoint and deployment YAML
tests/                  # Unit tests and data validation tests
notebooks/              # Exploration notebooks, outputs cleared

Separating source code into packages with unit tests, rather than leaving it in notebooks, makes it testable in a pull request workflow without running a full training job.

Branching and pull requests

Most teams use short-lived feature branches merged into main through pull requests. Branch protection rules on main require a pull request, passing status checks and at least one reviewer. The pull request is where automation runs: linting, unit tests, data validation, a Bicep what-if, a short training run on sample data, and, for generative AI, evaluations of changed prompts.

  1. Create a branch

    A developer branches from main for one change.

  2. Commit and push

    Changes to code, YAML or prompts are committed with clear messages.

  3. Open a pull request

    Workflows run tests, validations and previews and report status checks.

  4. Review

    A reviewer checks the change and the automated results.

  5. Merge

    Merging to main triggers the training or deployment workflow.

Releases can be marked with tags, such as model-v7, so that a deployed model or prompt version can be traced to a commit and rebuilt or reverted.

Git information in jobs

When a job is submitted from a folder that is a Git working copy, Azure Machine Learning records the repository URL, branch and commit hash with the job, and whether the working copy had uncommitted changes. The values appear in the job's properties, such as mlflow.source.git.commit and azureml.git.dirty, and in the studio.

This links every model registered from a job to the exact code that trained it. A job submitted with a dirty working copy breaks that link, because its code is not in any commit; production training should therefore run from a workflow that checks out a clean commit. Compute instances can also clone repositories directly, authenticating to GitHub with an SSH key or personal access token stored for the user.

Worked example

Tracing a model to its code

An incident review asks which version of the feature engineering code produced the model now in production. The model was registered from a job, but that job was submitted from a data scientist's compute instance, and the job properties show azureml.git.dirty as True.

The job recorded a commit, but the dirty flag means the code that ran included uncommitted changes, so the commit alone does not reproduce it. The job's code snapshot stored in the workspace shows the files that actually ran, which answers the immediate question. To prevent recurrence, production training should run only from a GitHub Actions workflow that checks out a clean commit, and the release process should reject models whose source job has a dirty working copy.

Searching the repository history for the commit would show code that differs from what ran. Asking the data scientist relies on memory. Retraining from the current main branch would produce a different model rather than explaining the existing one.

Check yourself

Why should large training datasets and model files not be committed to the repository?

Show answer

Git is designed for text that changes in small steps. Large binaries bloat every clone and are not managed as versioned assets, so data and models are referenced as data and model assets in the workspace, with the repository holding the code and definitions.

Exam Tips

Remember for the exam

  • Commit code, definitions and prompts; reference data and models as assets; never commit secrets.
  • Use .gitignore and .amlignore, and clear notebook outputs.
  • Pull requests run tests and validations before merge.
  • Jobs record the Git commit and a dirty flag; train production models from clean commits.