Sub topic 15
Sharing work and using Git folders
This chapter assumes you can already create and run a notebook in a workspace. It also assumes you know the basic Git workflow of clone, branch, commit, push, and pull. It covers two ways to collaborate in Databricks. The first is sharing objects inside a workspace. The second is using Git folders to keep code under version control.
Two ways to share work
A workspace gives you two separate collaboration mechanisms. They solve different problems.
| Mechanism | What it does | Best for |
|---|---|---|
| Workspace permissions | Lets other users view, run, or edit an object in place | Quick sharing, review, shared dashboards |
| Git folders | Syncs a folder with a remote Git repository | Production code, review, history, CI/CD |
Workspace permissions are fast but weak at history. Git folders add the branching and review process that Git provides. Production code should live in Git.
Sharing inside the workspace
Notebooks, folders, queries, dashboards, and jobs each have an access control list. The notebook and folder permission levels are:
- CAN READ: view the notebook and its comments.
- CAN RUN: attach to compute and run cells, but not change them.
- CAN EDIT: change cells.
- CAN MANAGE: change permissions, rename, move, and delete.
Folders pass their permissions down to the objects inside them. If you grant a group CAN RUN on a folder, every notebook in it gets that level.
Two locations matter for sharing:
- Your home folder under
/Workspace/Users/<your-email>is private by default. /Workspace/Sharedis visible to all workspace users, so it can expose work to more people than you intend.
Several people can open the same notebook at once and see each other's edits in real time. They can also leave comments on cells. Notebooks keep a version history, which lets you restore an earlier state. That history is not a replacement for Git. It has no branches, no merge, and no review step.
Workspace permissions control the notebook, not the data. A user who can run your notebook still needs Unity Catalog privileges on the tables it reads. A notebook that works for you can fail for a colleague with a permission error on a table, even though they can open the notebook.
What a Git folder is
A Git folder is a workspace folder linked to a remote Git repository. The supported providers include GitHub, GitLab, Bitbucket, Azure DevOps, and AWS CodeCommit. The folder holds a local clone of the repository, and Databricks runs the Git operations for you from the UI.
The setup steps are:
- Link your Git account in your user settings, using OAuth or a personal access token.
- In the workspace, choose to create a Git folder and paste the repository URL.
- Databricks clones the repository into the folder.
You can then do these operations from the Git dialog:
- Create and switch branches.
- Pull remote changes.
- Commit and push local changes.
- Merge or rebase a branch.
- Resolve conflicts.
One Git folder per person
The most important rule is that every user works in their own Git folder. Do not share one Git folder between several people who edit on the same branch. The clone has one working state, so two people editing it overwrite each other's uncommitted changes and create confusing conflicts.
A typical workflow looks like this:
- Clone the repository into your own folder under your user directory.
- Create a feature branch.
- Edit notebooks and files, and run them against development data.
- Commit and push the branch.
- Open a pull request in your Git provider. Review and merge happen there, not in Databricks.
- Pull
maininto your folder to continue.
How notebooks are stored in Git
A notebook in a Git folder is saved as a plain source file by default. A Python notebook becomes a .py file whose first line is a marker comment. Cell boundaries are comment lines.
# Databricks notebook source
from pyspark.sql import functions as F
# COMMAND ----------
df = spark.table("main.tutorial.orders")
totals = df.groupBy("customer_id").agg(F.sum("amount").alias("total_amount"))
display(totals)Source files produce small, readable diffs. You can also choose the Jupyter .ipynb format. It stores outputs and metadata, which makes diffs noisy and can leak result data into the repository. Prefer source format unless you need to keep outputs.
Moving logic into modules
Git folders also hold ordinary files, such as Python modules, SQL files, and configuration. This is the main reason to use them. Logic you put in a module can be tested and reused, and notebooks stay thin.
A notebook inside a Git folder can import modules from the repository root without extra setup. Suppose the repository contains this layout:
my-repo/
src/
transforms.py
notebooks/
daily_orders.pyThe module defines a pure function:
# src/transforms.py
from pyspark.sql import DataFrame, functions as F
def customer_totals(df: DataFrame) -> DataFrame:
"""Sum order amounts per customer."""
return df.groupBy("customer_id").agg(F.sum("amount").alias("total_amount"))The notebook imports and uses it:
# Databricks notebook source
from src.transforms import customer_totals
# COMMAND ----------
orders = spark.table("main.tutorial.orders")
display(customer_totals(orders))Running jobs from Git
A scheduled job can read its code straight from a remote repository instead of from a workspace copy. This is the safer pattern for production. The job runs a specific branch, tag, or commit, so edits in someone's personal folder do not affect it.
This job definition fragment uses a Git source. The source value GIT tells the task to look in the repository, and the path is relative to the repository root without the file extension:
{
"name": "daily-orders",
"git_source": {
"git_url": "https://github.com/example-org/my-repo",
"git_provider": "gitHub",
"git_branch": "main"
},
"tasks": [
{
"task_key": "totals",
"notebook_task": {
"notebook_path": "notebooks/daily_orders",
"source": "GIT"
}
}
]
}Pinning a tag or commit instead of a branch gives the most repeatable runs. Using a branch means the job picks up whatever was last merged, which is convenient but means a merge changes production behavior.
Common problems
- Uncommitted changes block a pull. If you edited files locally and the remote changed the same files, the pull stops. Commit or discard your changes first, then resolve the conflicts.
- Credentials expire. A personal access token that expires causes pushes and pulls to fail with authentication errors. Renew the token in your linked account settings.
- Detached or wrong branch. Check the branch name shown in the Git dialog before you commit. Commits made on the wrong branch must be moved with Git operations.
- Editing in the wrong place. Changes made to a workspace copy of a notebook that a job reads from a Git source have no effect on the job. Push the change to the repository.
- Outputs in the repository. Using
.ipynbformat can commit cell outputs, which may contain data you did not mean to share.
For larger teams, Databricks Asset Bundles let you define jobs, pipelines, and their settings as files in the repository and deploy them through CI/CD. The Git folder then becomes a development environment rather than the deployment mechanism.
Exercise
Two colleagues both need to edit the notebooks in one repository on separate features. One suggests creating a single Git folder under /Workspace/Shared so both can work in it. What is wrong with this plan, and what should they do instead?
Show answer
A Git folder has one working state, so two people editing it would overwrite each other's uncommitted changes. The folder under /Workspace/Shared would also expose the code to every workspace user. Each person should clone the repository into their own Git folder, work on their own feature branch, push it, and merge through a pull request in the Git provider.
Exercise
A job is configured with a Git source that points to branch main and the path notebooks/daily_orders. A developer fixes a bug in their personal Git folder and saves the notebook, but the next job run still shows the bug. Why, and what must they do?
Show answer
The job reads the code from the remote repository, not from a personal workspace copy. The fix has to be committed, pushed, and merged into main (or the job pointed at the branch that contains it) before the job picks it up.