Sub topic 13
Resolving merge conflicts
This chapter assumes you can clone a repository into a Git folder, create a branch, and commit and push from the Git dialog. It also assumes you know what a merge is in Git. Here you will see why conflicts happen with notebooks, how Databricks shows them, and how to resolve them.
Why conflicts happen
A merge conflict occurs when two branches change the same lines of a file and Git cannot decide which version to keep. Notebooks are stored as source files, where each cell is a block of text separated by # COMMAND ----------. Two people who edit the same cell on different branches will therefore produce a conflict. Edits in different cells usually merge cleanly.
In a Git folder, conflicts appear when you pull or merge. They can also appear when you rebase a branch onto another one.
What a conflict looks like
When the merge stops, Databricks lists the conflicted files and tells you to resolve them before you continue. Open the notebook and you will see standard Git markers inside the source:
# COMMAND ----------
<<<<<<< HEAD
df = spark.read.table("sales.orders").filter("status = 'OPEN'")
=======
df = spark.read.table("sales.orders").filter("status <> 'CLOSED'")
>>>>>>> origin/main
display(df)The text above ======= is your branch. The text below it is the incoming branch. You must edit the file so that only the final code remains and all three marker lines are gone.
Resolving in the workspace
- Open the Git dialog for the Git folder and pull or merge. If the operation reports a conflict, note the files it lists.
- Open each conflicted notebook. Decide which version to keep, or combine both.
- Delete the lines that start with
<<<<<<<,=======, and>>>>>>>. - Run the notebook cells you changed to confirm the code still works.
- Commit the resolved files in the Git dialog, then push.
Resolving outside the workspace
If the conflict is large, resolve it on your computer with the same Git commands you use for any repository. Then push the result and pull it into the Git folder.
git fetch origin
git merge origin/main
# edit the conflicted files and remove the markers
git add -A
git commit -m "Resolve merge conflict in clean orders notebook"
git pushReducing conflicts
- Pull from the main branch often, so your branch does not drift far from it.
- Keep notebooks small and move shared logic into Python modules, so fewer people edit the same file.
- Agree on who owns each notebook while a feature is in progress.
- Do not edit the same Git folder branch at the same time as a teammate. Give each person their own Git folder and branch.
Exercise
You pulled main into your feature branch and Databricks reports a conflict in a notebook. You open the file and see <<<<<<< HEAD, =======, and >>>>>>> origin/main around two versions of one cell. What do you do before you commit?
Show answer
Choose or combine the two versions of the code, delete all three marker lines, run the changed cells to check they work, and then commit and push the resolved notebook.