Using Git in Databricks
As in the typical software development environment, Git is the primary way of sharing your code and collaborating on software projects in Databricks. Most of the core features you need to edit code, such as branching, pulling, dev and prod branches, are all available in Git.
This is a beginner tutorial on how to use Git, specifically GitHub, in Databricks. Using GitHub I will show you how to connect your account, create a dev branch, commit code, and push code. Finally, I will show you how to use git to build a Workflow.
How sharing works in Git for Databricks
Your GitHub repository is not shared or visible to other Databricks users. It lives inside your user folder, which only you can see and modify. The way you share data is creating new branches, and pushing those branches to GitHub. In GitHub the code is reviewed and pulled into the code base. Then, when you pull the latest changes to your branch, their code with your changes will be brought up to date. This is how sharing works with Git in Databricks, by pushing, merging, and pulling branches.
The remainder of this article explains step-by-step how to set up a new Git repo in Databricks for the absolute beginner.
Connect Databricks to GitHub
Click on your user Icon and click on settings.

Under settings click "link account"

Log into Git Hub and then click. Link your GitHub account.

It should link automatically.
Create a Git repo in GitHub
Go to your organization in GitHub and click the green "new button" to create a new repo.


Make sure to select Add A Readme when prompted. This will create an initial commit.

copy the HTTPS URL.

Add your GitHub repo to Databricks
Got to Databricks and click Create and select Git Folder.

Fill out the information to add your GitHub repo to your workspace.

Oh no! When I tried to create the connection I got an error "Error creating GIT Folder".

This is because the Databricks GitHub app must be installed in BOTH your organization and on your personal profile, and repositories are set to All. Follow the link to install the Databricks GitHub app for your personal account, and the organization if it isn't installed already.
When you create a Git Folder, you will see your repo in your workplace.
Open the folder and click "Main". This is the branch name. By clicking on the branch name, you open the Git editor.

Inside the editor
When you make an edit to the code or add code, you can click "Create Branch" to create a dev branch for these changes.

To commit code, fill in the comment box and commit.
You CANNOT merge branches in Databricks. Rather, you must merge branches in GitHub following your organizations review procedures (as you should).
Now you have your dev and prod branches.
The above steps will help you program and do data analysis. But what if you want to do data engineering and implement your workbook into production with nightly runs?
Creating a workflow with Python
This section shows how to create a workflow using GitHub as the source of truth for the code base.
Let's connect the main branch to a workflow. Go to the workflows section. and create a new workflow.

Fill in the task name and type as notebook. Then Select Source as git provider and then click "add a git reference"
.
Fill in the necessary information for your repo and copy the name of the notebook you would like to run.
Paste the name of the notebook under Path, omitting the file extension "ipynb".

Select a runner for your job. The most efficient are Serverless or a specialized job-running cluster.
Now your workflow is set to pull from GitHub as the single source of truth, and will run the specified notebook. Any changes you made in testing and development will not affect your pipeline until the development branch is committed and pulled into the designated production branch in GitHub. Once merged, when the workflow executes it will pull the latest code from the production branch, so the only code base you need to keep an eye on is the production branch.
Summary
This tutorial explained step-by-step how to:
- connect GitHub to Databricks
- creating a new branch in GitHub
- adding the remote repo to your Databricks Workspace.
- using the git editor in Databricks
-
using git to create workflow jobs.
Thank you for reading my tutorial on how to get up and running with Git and GitHub in Databricks.
Leave A Reply