Sub topic 8
How a notebook is stored as a source file
This chapter assumes you know basic Git (commit, branch, diff) and have written a Python or SQL cell in a Databricks notebook. It explains what Git actually stores when you commit a notebook from a Git folder.
Notebooks are plain text in Git
In the workspace, a notebook looks like a set of cells with outputs. In a Git repository, a Python notebook is a single .py file. Databricks writes the cell structure into that file using comments, so the file is still valid Python and you can review it in a pull request like any other code.
# Databricks notebook source
# MAGIC %md
# MAGIC # Daily load
# COMMAND ----------
df = spark.range(5)
display(df)
# COMMAND ----------
# MAGIC %sql
# MAGIC SELECT 1 AS oneThe markers
Three conventions carry all of the structure.
| Marker | Meaning |
|---|---|
| `# Databricks notebook source` | First line. Tells Databricks to open the file as a notebook, not as a plain script. |
| `# COMMAND ----------` | Separates one cell from the next. |
| `# MAGIC ...` | A cell written in another language or mode, such as `%md` or `%sql`. The `# MAGIC ` prefix is added to each line. |
The comment character depends on the language. A Python notebook uses #. A SQL notebook uses --, and Scala uses //. The file extension (.py, .sql, .scala, .r) records the notebook's default language.
What is not stored
By default, the source file holds code only. Cell outputs, such as tables and charts, are not in the .py file, so a pull request shows only code changes. Cell outputs live in the workspace revision history, not in Git. Because of this, a freshly cloned notebook has no results until you run it.
Some settings and metadata, such as which compute is attached, also stay in the workspace and are not part of the committed file.
Why this matters
- Readable diffs. A changed cell appears as a changed block between two
COMMANDlines. - Merge conflicts are text conflicts. Two people editing the same cell conflict like any two edits to the same lines. Keep the markers intact when you resolve them.
- Editing outside Databricks. You can edit the
.pyfile in an IDE. If you keep the first-line header and the separators, Databricks still opens it as a notebook.
Common errors
- A
# MAGICline without the%command on its first line means the cell has no language, so the text runs as the notebook's default language. - Removing a
# COMMAND ----------line merges two cells into one. - Pasting a magic cell without the
# MAGICprefix on every line breaks the cell in a Python notebook.
Exercise
You open a notebook's .py file in an IDE. A cell contains the lines # MAGIC %sql and # MAGIC SELECT 1 AS one. Which language does that cell run in, and why do both lines start with # MAGIC?
Show answer
It runs as SQL because of the %sql magic on the first line. Both lines start with # MAGIC so the file stays valid Python: Databricks reads the prefixed text as the cell content, and any other tool sees it as comments.
Exercise
A teammate says they cannot see the chart you produced in a notebook after they pulled your branch. Why?
Show answer
Cell outputs are not stored in the source file committed to Git. They exist only in the workspace where the cell ran, so your teammate must run the cell to see the chart.