Skip to content

Instantly share code, notes, and snippets.

@devinschumacher
Last active October 18, 2024 03:41
Show Gist options
  • Select an option

  • Save devinschumacher/ea416af5542ac7102c8e1ffd0ab38a99 to your computer and use it in GitHub Desktop.

Select an option

Save devinschumacher/ea416af5542ac7102c8e1ffd0ab38a99 to your computer and use it in GitHub Desktop.
How to Create a Branch from an Issue in Github (without the GUI / from the Command Line)
title How to Create a Branch from an Issue in Github (without the GUI / from the Command Line)
tags
git
github
git branch

How to Create a Branch from an Issue in Github (without the GUI / from the Command Line)

GitHub's CLI tool gh provides a convenient way to create branches linked to specific issues without using the GUI.

Prerequisite: Ensure you have the GitHub CLI (gh) installed and authenticated.

1. Display a list of available issues

gh issue list
image

Note: If there's no issue for you to work on, you can create one with gh issue create followed by a few interactive prompts.

2. Create a feature branch from an issue

Syntax

gh issue develop <issue-number> [--base <base-branch>] [--name <branch-name>] [--checkout]
  • gh issue develop: Will create a branch for you to work on that's connected to an issue
  • <issue-number>: The number of the issue you want to link the branch to.
  • --base <base-branch>: (Optional) The branch you want to base your new branch on. Defaults to the repository's default branch if not specified.
  • --name <branch-name>: (Optional) The name for the new branch. If not provided, GitHub CLI generates a name based on the issue title.
  • --checkout: Will switch to the branch after it creates it, saving you from having to run a git switch <branch-name> afterwards

Example

Example: Createing a branch for issue #48, from the dev branch with a custom issue title:

# run command from the main branch
devin:~/repos/projects/serp-ui-nuxt (main)$
$ gh issue develop 32 --name ds/32/company-index --checkout

github.com/serpcompany/serp-ui-nuxt/tree/ds/32/company-index
From github.com:serpcompany/serp-ui-nuxt
 * [new branch]      ds/32/company-index -> origin/ds/32/company-index
 
# automatically switched to the created branch
devin:~/repos/projects/serp-ui-nuxt (ds/32/company-index)$

Some additional gh issue list commands

# List all issues (open and closed)
gh issue list --state all

# List issues assigned to you
gh issue list --assignee @me

# List issues with a specific label
gh issue list --label bug

# List issues in JSON format
gh issue list --json number,title,assignees

# List issues with custom columns
gh issue list --columns number,title,assignees,labels

# List issues and limit the output
gh issue list --limit 10

# List issues and sort them
gh issue list --sort comments

# List issues in web browser
gh issue list --web

Bonus: How to Create a Branch from an Issue in Github (from the github.com Website GUI)

create a branch from an issue in github

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment