import requests
# Define the repository details
owner = "your-username"
repo = "your-repo"
old_default = "master" # replace with the current default branch name
new_default = "main" # replace with the desired new default branch name
# Authenticate to access the GitHub API (replace with your personal access token)
headers = {
"Authorization": "Bearer YOUR_PERSONAL_ACCESS_TOKEN"
}
# Get the current repository details
url = f"https://api.github.com/repos/{owner}/{repo}"
response = requests.get(url, headers=headers)
repo_details = response.json()
# Update the default branch setting
repo_details["default_branch"] = new_default
# Update the repository using the GitHub API
response = requests.patch(url, json=repo_details, headers=headers)
# Check the response status code to verify if the default branch was changed successfully
if response.status_code == 200:
print("Default branch updated successfully.")
else:
print("Failed to update the default branch.")