<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Can we get the branch name from Notebook in Get Started Discussions</title>
    <link>https://community.databricks.com/t5/get-started-discussions/can-we-get-the-branch-name-from-notebook/m-p/114436#M9288</link>
    <description>&lt;P&gt;Yes, you can display the current git branch name from a Databricks notebook in several ways:&lt;/P&gt;
&lt;P&gt;Using the Databricks UI&lt;/P&gt;
&lt;P&gt;The simplest method is to use the Databricks UI, which already shows the current branch name:&lt;BR /&gt;- In a notebook, look for the button next to the notebook name that identifies the current Git branch&lt;BR /&gt;- Click this button to open the Git dialog where you can see your current working branch and perform other Git operations[&lt;/P&gt;
&lt;P&gt;Using Code in a Notebook&lt;/P&gt;
&lt;P&gt;If you need to programmatically get the branch name within your notebook code:&lt;/P&gt;
&lt;P&gt;1. Use the Databricks API approach:&lt;BR /&gt;```python&lt;BR /&gt;import json&lt;BR /&gt;import requests&lt;/P&gt;
&lt;P&gt;def get_current_branch():&lt;BR /&gt;nb_context = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())&lt;BR /&gt;api_url = nb_context['extraContext']['api_url']&lt;BR /&gt;api_token = nb_context['extraContext']['api_token']&lt;BR /&gt;db_repo_data = requests.get(f"{api_url}/api/2.0/repos", &lt;BR /&gt;headers = {"Authorization": f"Bearer {api_token}"}).json()&lt;BR /&gt;# Process the response to extract branch information&lt;BR /&gt;return db_repo_data&lt;BR /&gt;```&lt;/P&gt;
&lt;P&gt;2. If your repository is accessible from the filesystem, you can try:&lt;BR /&gt;```python&lt;BR /&gt;# This might work in some Databricks configurations&lt;BR /&gt;!cat /Workspace/Repos/user@domain/repository/.git/HEAD | sed 's~ref: refs/heads/~~'&lt;BR /&gt;```&lt;/P&gt;
&lt;P&gt;Note that standard Git Python libraries like `pygit2` or `GitPython` don't work well with Databricks Repos as they're configured differently than standard Git repositories. A community user reported that these packages cannot recognize Databricks Repos properly.&lt;/P&gt;
&lt;P&gt;The Databricks UI approach is the most reliable method since it's built into the platform and doesn't require additional code or permissions.&lt;/P&gt;</description>
    <pubDate>Thu, 03 Apr 2025 17:14:04 GMT</pubDate>
    <dc:creator>Louis_Frolio</dc:creator>
    <dc:date>2025-04-03T17:14:04Z</dc:date>
    <item>
      <title>Can we get the branch name from Notebook</title>
      <link>https://community.databricks.com/t5/get-started-discussions/can-we-get-the-branch-name-from-notebook/m-p/114375#M9287</link>
      <description>&lt;P&gt;Hi Folks,&lt;/P&gt;&lt;P&gt;Is there a way to display the current git branch name from Databricks notebook&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;</description>
      <pubDate>Thu, 03 Apr 2025 08:39:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/can-we-get-the-branch-name-from-notebook/m-p/114375#M9287</guid>
      <dc:creator>suryahyd39</dc:creator>
      <dc:date>2025-04-03T08:39:23Z</dc:date>
    </item>
    <item>
      <title>Re: Can we get the branch name from Notebook</title>
      <link>https://community.databricks.com/t5/get-started-discussions/can-we-get-the-branch-name-from-notebook/m-p/114436#M9288</link>
      <description>&lt;P&gt;Yes, you can display the current git branch name from a Databricks notebook in several ways:&lt;/P&gt;
&lt;P&gt;Using the Databricks UI&lt;/P&gt;
&lt;P&gt;The simplest method is to use the Databricks UI, which already shows the current branch name:&lt;BR /&gt;- In a notebook, look for the button next to the notebook name that identifies the current Git branch&lt;BR /&gt;- Click this button to open the Git dialog where you can see your current working branch and perform other Git operations[&lt;/P&gt;
&lt;P&gt;Using Code in a Notebook&lt;/P&gt;
&lt;P&gt;If you need to programmatically get the branch name within your notebook code:&lt;/P&gt;
&lt;P&gt;1. Use the Databricks API approach:&lt;BR /&gt;```python&lt;BR /&gt;import json&lt;BR /&gt;import requests&lt;/P&gt;
&lt;P&gt;def get_current_branch():&lt;BR /&gt;nb_context = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())&lt;BR /&gt;api_url = nb_context['extraContext']['api_url']&lt;BR /&gt;api_token = nb_context['extraContext']['api_token']&lt;BR /&gt;db_repo_data = requests.get(f"{api_url}/api/2.0/repos", &lt;BR /&gt;headers = {"Authorization": f"Bearer {api_token}"}).json()&lt;BR /&gt;# Process the response to extract branch information&lt;BR /&gt;return db_repo_data&lt;BR /&gt;```&lt;/P&gt;
&lt;P&gt;2. If your repository is accessible from the filesystem, you can try:&lt;BR /&gt;```python&lt;BR /&gt;# This might work in some Databricks configurations&lt;BR /&gt;!cat /Workspace/Repos/user@domain/repository/.git/HEAD | sed 's~ref: refs/heads/~~'&lt;BR /&gt;```&lt;/P&gt;
&lt;P&gt;Note that standard Git Python libraries like `pygit2` or `GitPython` don't work well with Databricks Repos as they're configured differently than standard Git repositories. A community user reported that these packages cannot recognize Databricks Repos properly.&lt;/P&gt;
&lt;P&gt;The Databricks UI approach is the most reliable method since it's built into the platform and doesn't require additional code or permissions.&lt;/P&gt;</description>
      <pubDate>Thu, 03 Apr 2025 17:14:04 GMT</pubDate>
      <guid>https://community.databricks.com/t5/get-started-discussions/can-we-get-the-branch-name-from-notebook/m-p/114436#M9288</guid>
      <dc:creator>Louis_Frolio</dc:creator>
      <dc:date>2025-04-03T17:14:04Z</dc:date>
    </item>
  </channel>
</rss>

