Sub topic 13
Resolving merge conflicts
This chapter assumes you know Git basics (commit, branch, merge, pull). It also assumes you have a Git folder with a cloned repository, as in the earlier chapters. You will learn why conflicts appear in notebooks, how to read them, and how to resolve them from a Git folder or from a local clone.
Why conflicts happen
A merge conflict occurs when Git cannot combine two sets of changes on its own. This happens when two branches change the same lines of a file, or when one branch edits a file that the other deleted.
A notebook is stored in Git as a plain text source file, so conflicts are line-based like any other code. A Python notebook has a header comment, cells separated by # COMMAND ----------, and magic cells written as # MAGIC comments. If you and a teammate both edit the same lines in the same cell, Git cannot tell which version you want.
Conflicts usually appear in one of three operations:
- Pulling from the remote branch into your Git folder.
- Merging another branch, such as
main, into your feature branch. - Rebasing your branch onto another branch.
Edits in different cells usually merge cleanly, because the changes touch different lines. Conflicts get more likely when cells are reordered, a cell is split, or someone reformats a whole file.
Reading conflict markers
When Git cannot merge a file, it writes both versions into the file between marker lines. Here is a notebook cell after a conflicting merge:
# Databricks notebook source
# COMMAND ----------
<<<<<<< HEAD
orders = spark.table("sales.orders").filter("status = 'PAID'")
=======
orders = spark.table("sales.orders").filter("status IN ('PAID', 'SHIPPED')")
>>>>>>> origin/main
display(orders)The markers mean:
| Marker | Meaning |
|---|---|
| `<<<<<<< HEAD` | Start of your current branch's version |
| `=======` | Divider between the two versions |
| `>>>>>>> origin/main` | End of the incoming version, labeled with its source |
Your job is to replace this whole block with the content you want, and to delete all three marker lines. The result can be one side, the other side, or a combination. In this example, the incoming change broadens the filter, so the right answer might be to keep the main version:
# Databricks notebook source
# COMMAND ----------
orders = spark.table("sales.orders").filter("status IN ('PAID', 'SHIPPED')")
display(orders)Do not leave any marker lines behind. A notebook that still contains <<<<<<< has a syntax error in Python, or garbage in SQL cells, and it will fail when run.
Resolving a conflict in a Git folder
The steps are the same whether the conflict came from a pull, a merge, or a rebase:
- Open the Git dialog for the folder and start the pull or merge. If Git cannot finish, the dialog reports a merge conflict and lists the conflicted files.
- Open each conflicted notebook or file in the workspace. The conflict markers are in the content, and you edit them like any other text.
- Edit the file so it holds the final content, with no markers.
- Mark the conflict as resolved in the Git dialog, then continue the operation. For a merge this creates the merge commit, and for a rebase it moves on to the next commit.
- Push the result to your remote branch.
The exact button names can differ between releases of the workspace UI, so follow the prompts in the dialog. The sequence of steps stays the same.
Some operations fail before any conflict exists. If you have uncommitted local edits that overlap with incoming changes, the pull can be refused. You then have two choices: commit your changes first, which turns the problem into a normal merge you can resolve, or discard your local changes if you do not need them.
Resolving in a local clone
Large or messy conflicts are easier to handle in a full editor with a three-way merge view. You can resolve the conflict in a local clone and then pull the result into the Git folder:
git checkout feature/clean-orders
git fetch origin
git merge origin/main
# Git reports: CONFLICT (content): Merge conflict in orders_notebook.py
# Open the file, remove the markers, keep the final content, then:
git add orders_notebook.py
git commit -m "Merge main into feature/clean-orders"
git push origin feature/clean-ordersBack in the workspace, pull the branch in the Git folder. It now fast-forwards to the resolved commit, so no conflict appears there. This also works when the Git folder is the only place you have edited recently: commit and push from the folder first, then resolve locally.
Merge or rebase
Both strategies combine your work with a branch that has moved on. They differ in what the history looks like and when conflicts surface.
| Strategy | History result | Conflicts appear |
|---|---|---|
| Merge | One merge commit joins the branches | Once, all together |
| Rebase | Your commits are replayed on top of the target branch | Possibly at each replayed commit |
Merge is simpler and safer for shared branches. Rebase gives a linear history but rewrites your commits, so a branch you have already pushed needs a force push afterwards. Use rebase only on a branch that you alone work on, and follow your team's convention.
Check the result before committing
A resolved file can be syntactically valid and still wrong. Before you push:
- Search the file for
<<<<<<<,=======, and>>>>>>>. A code search or a quickgrepfinds leftovers. - Check the notebook structure. The first line must still be
# Databricks notebook source, and each cell must stay separated by# COMMAND ----------. If you delete a separator by accident, two cells merge into one. If you delete the header, the file is no longer treated as a notebook. - Run the notebook on compute with a small input, or run the relevant cells, to confirm it still works.
- Look at the diff to confirm that you kept the intended changes from both sides.
Common mistakes
- Leaving markers in a SQL magic cell. A
# MAGIC %sqlcell containing<<<<<<<fails at run time, not at pull time, so the error shows up later. - Keeping both sides blindly. Two versions of the same assignment can silently override each other. Read the surrounding code.
- Resolving a notebook while someone else edits it. The Git folder is a single working copy per user. Use your own folder and your own branch, rather than editing the same folder together, so your resolution is not overwritten.
- Forgetting to push. Resolving and committing only updates your Git folder. The remote branch and any pull request stay unchanged until you push.
Exercise
You merge main into your feature branch from a Git folder and a notebook reports a conflict. You open the file and find <<<<<<< HEAD, =======, and >>>>>>> origin/main around two different versions of one line. List the steps to finish the merge and one check to do before pushing.
Show answer
Edit the file so it contains only the final content you want, deleting all three marker lines. Mark the file as resolved in the Git dialog and continue the merge, which creates the merge commit. Then push the branch. Before pushing, search the file for leftover markers, confirm that the # Databricks notebook source header and the # COMMAND ---------- separators are intact, and run the changed cells to confirm the notebook still works.
Exercise
Two teammates each change a different cell of the same notebook on separate branches. Is a conflict likely when one merges the other's branch, and why?
Show answer
A conflict is unlikely. Git merges per line, and the two changes touch different lines of the source file, so it combines them automatically. A conflict would appear only if both people changed the same lines, or if one of them reordered or reformatted large parts of the file.