Sub topic 11
Cloning a repository and working on a branch
This chapter assumes you have connected a Git provider credential (the previous chapter) and know basic Git: clone, branch, commit, push. It covers how to clone a remote repository into a Databricks Git folder, how branches behave inside one, and the mistakes that most often cause trouble.
What a clone creates
A Git folder is a clone of a remote repository stored in your workspace. It holds a working copy of one branch at a time. Databricks keeps the Git metadata for you. You work through the Git dialog in the workspace rather than a local terminal.
Three properties shape how you work:
- A Git folder has exactly one checked-out branch. Switching branches replaces the files you see.
- A Git folder is a separate copy per user. Your edits do not appear in a colleague's Git folder until you push and they pull.
- You cannot nest a Git folder inside another Git folder.
Cloning from the workspace
Use the UI when you want the quickest path.
- In the workspace sidebar, open Workspace and go to your home folder (or another folder where you may create items).
- Select Add (or Create), then Git folder.
- Paste the repository's HTTPS URL, for example
https://github.com/my-org/orders-pipeline.git. - Check the provider. Databricks usually detects it from the URL.
- Accept or change the folder name, then select Create Git folder.
The clone checks out the repository's default branch, usually main. Large repositories take longer to clone. If cloning fails with an authentication error, the problem is almost always the credential from the previous chapter. Check that the token has repository read access and has not expired.
Sparse checkout for large repositories
If the repository is a monorepo and you need one directory, enable sparse checkout in the clone dialog. You then give cone patterns, such as pipelines/orders, and only matching paths appear in the Git folder. This reduces clone time and keeps the workspace tidy. Sparse checkout settings are chosen when you create the folder. If you need a different set of paths later, the usual route is to create a new Git folder.
Cloning from the CLI or API
For repeatable setups, such as onboarding a team, script the clone. The Databricks CLI wraps the Git folder API.
databricks repos create https://github.com/my-org/orders-pipeline.git gitHub \
--path /Workspace/Users/[email protected]/orders-pipeline
databricks repos listThe first argument is the URL and the second is the provider name. --path sets where the folder appears. Run databricks repos create --help to confirm the argument form in your CLI version, because the CLI changes over time.
Creating and switching branches
Open the Git folder, then select the branch name shown at the top. This opens the Git dialog.
- To create a branch, choose Create branch, type a name such as
feature/clean-orders, and confirm. Databricks creates it from the branch currently checked out and switches to it. - To switch to an existing branch, pick it from the branch dropdown. Branches that exist only on the remote are listed after Databricks fetches them.
The dialog performs the same steps as these plain Git commands:
git checkout -b feature/clean-orders
# edit notebooks in the Git folder, then:
git add -A
git commit -m "Clean orders notebook"
git push -u origin feature/clean-ordersThe CLI can switch the branch of an existing Git folder. You need the folder's numeric ID from databricks repos list:
databricks repos update 123456789 --branch feature/clean-ordersHere 123456789 stands for your folder's ID.
Why you should not work on main
Nothing in the clone stops you from editing the default branch. Most teams protect main on the provider side so that changes arrive through pull requests. In that setup, a push from a Git folder to main is rejected, and you must move your edits to a feature branch. It is easier to create the branch before you edit anything.
A common naming scheme:
| Pattern | Example | Use |
|---|---|---|
| `feature/<topic>` | `feature/clean-orders` | New work |
| `fix/<topic>` | `fix/null-dates` | Bug fixes |
| `<user>/<topic>` | `ana/orders-experiment` | Personal experiments |
One folder per branch, or one folder reused?
Because a Git folder holds one branch, you must decide how to handle parallel work.
- Reuse one folder. Switch branches as needed. This is simple, but a notebook attached to compute may be running code from the branch you just left, and a job pointing at that folder would run whatever is checked out.
- Clone the same repository twice. Keep one folder on
mainfor reference and one for your feature work. This costs a second clone but avoids accidental switches.
Never point a production job at a personal Git folder. Jobs should read from a Git source or a deployed copy (covered in later chapters), because a personal folder changes whenever its owner switches branches.
Seeing what the clone looks like
Notebooks in a Git folder appear as normal notebooks but are stored as source files, as described in the source-format chapter. Other files, such as .py modules, requirements.txt, and README.md, appear as workspace files. After cloning, expect a layout like this:
# List the contents of your Git folder from a notebook.
import os
repo_path = "/Workspace/Users/[email protected]/orders-pipeline"
for name in sorted(os.listdir(repo_path)):
print(name)Replace the path with your own folder. The output lists notebooks, modules, and the repository's other files. This confirms the clone worked and shows which branch's files you have.
Common errors
- Authentication failed on clone. The token is missing, expired, or lacks repository scope. Fix the credential and retry.
- Branch missing from the dropdown. The branch was created on the remote after your last fetch. Reopen the Git dialog and pull or refresh to fetch it.
- Cannot create a Git folder here. You are inside another Git folder. Choose a location outside it.
- Push rejected to
main. The branch is protected. Create a feature branch and open a pull request. - Working on stale code. Your folder is behind the remote. Pull before you start.
Exercise
A colleague says "I pushed my changes, so you can see them in your Git folder right away." Is that correct? What must happen before you see their changes, and why?
Show answer
No. Each user's Git folder is a separate clone. A push updates only the remote repository. You must be on the same branch and pull in the Git dialog to bring the remote commits into your own Git folder.
Exercise
You need to fix a bug but your Git folder is on main, which is protected. List the steps you take in the Git dialog before editing.
Show answer
Pull to get the latest main, create a new branch such as fix/null-dates (which is checked out automatically), then edit notebooks on that branch. Commit and push the branch, then open a pull request on the provider.