xxxxxxxxxx
# Clone the repository to your local machine if you haven't already
git clone <repository_url>
# Change to the repository directory
cd <repository_directory>
# Create a new branch
git branch <branch_name>
# Switch to the newly created branch
git checkout <branch_name>
xxxxxxxxxx
# create new branch
git checkout -b new-branch-name;
# go to existing branch
git checkout existing-branch-name;
# check what branch is currently being used
git branch;
# how to figure out other intersting things ;)
man git;
xxxxxxxxxx
# Clone the repository to your local machine
$ git clone <repository_url>
# Change into the repository directory
$ cd <repository_name>
# Create a new branch using the git branch command
$ git branch <new_branch_name>
# Switch to the newly created branch using the git checkout command
$ git checkout <new_branch_name>
# Push the new branch to the remote repository on GitHub
$ git push origin <new_branch_name>
xxxxxxxxxx
import requests
# Define the repository details
username = "<your_username>"
repository = "<your_repository>"
branch_name = "<new_branch_name>"
base_branch = "master"
# Set the necessary headers for authentication
headers = {
"Authorization": "Bearer <access_token>",
"Accept": "application/vnd.github.v3+json"
}
# Create the branch
url = f"https://api.github.com/repos/{username}/{repository}/git/refs"
data = {
"ref": f"refs/heads/{branch_name}",
"sha": f"refs/heads/{base_branch}"
}
response = requests.post(url, json=data, headers=headers)
if response.status_code == 201:
print("New branch created successfully!")
else:
print("Failed to create new branch.")
print(response.json())
xxxxxxxxxx
import requests
def create_branch(repo_owner, repo_name, branch_name):
base_url = 'https://api.github.com/repos'
api_url = f'{base_url}/{repo_owner}/{repo_name}/git/refs'
auth = ('username', 'access_token') # Replace with your own username and access token
# Get the latest commit SHA of the branch you want to branch off from
response = requests.get(f'{base_url}/{repo_owner}/{repo_name}/git/refs/heads/main')
latest_commit_sha = response.json()["object"]["sha"]
# Create a new branch
payload = {
"ref": f'refs/heads/{branch_name}',
"sha": latest_commit_sha
}
response = requests.post(api_url, json=payload, auth=auth)
if response.status_code == 201:
print(f'New branch "{branch_name}" created successfully!')
else:
print(f'Failed to create branch. Error: {response.json()}')
# Usage example
create_branch('your_username', 'your_repository', 'new-branch-name')