How to use the command 'gh pr create' (with examples)
The gh pr create
command is used to manage GitHub pull requests. It allows users to create pull requests either interactively or by specifying the necessary information.
Use case 1: Interactively create a pull request
Code:
gh pr create
Motivation: This use case is useful when you want to create a pull request and interactively provide the required information. It allows you to input the title, body, base branch, and any other necessary details through the command-line interface.
Explanation: When you run the command gh pr create
without any flags, the CLI prompts you to provide the required information for creating a pull request. You can enter the title, body, assignees, labels, and reviewers directly in the terminal.
Example output:
? Title: Add new feature
? Body: This pull request adds a new feature that improves performance.
? Base branch: main
? Head branch: new-feature
? Assignees: octocat
? Labels:
✓ Created pull request #123 (URL: https://github.com/octocat/repo/pull/123)
Use case 2: Create a pull request, determining the title and description from the commit messages of the current branch
Code:
gh pr create --fill
Motivation: This use case is useful when you want to automatically generate the title and description of a pull request from the commit messages of the current branch. It avoids the need to manually enter the pull request details.
Explanation: By using the --fill
flag, the command extracts the commit messages from the current branch and uses them to populate the title and description of the pull request. This is particularly helpful when you follow a well-defined commit message convention that conveys the purpose of each commit.
Example output:
✓ Created pull request #124 (URL: https://github.com/octocat/repo/pull/124)
Use case 3: Create a draft pull request
Code:
gh pr create --draft
Motivation: This use case is useful when you want to create a pull request as a draft. Draft pull requests are not fully ready for review and can be useful for getting early feedback or collaboration.
Explanation: The --draft
flag marks the pull request as a draft when it is created. Draft pull requests have a special status on GitHub and are treated differently from regular pull requests. They are not ready for merging and are usually used to request early feedback or collaboration on a work-in-progress.
Example output:
✓ Created draft pull request #125 (URL: https://github.com/octocat/repo/pull/125)
Use case 4: Create a pull request specifying the base branch, title, and description
Code:
gh pr create --base base_branch --title "title" --body "body"
Motivation: This use case is useful when you want to directly specify the base branch, title, and description of the pull request without being prompted interactively. It allows for a more streamlined and automated creation of pull requests.
Explanation: In this example, the --base
flag is used to specify the base branch for the pull request. The --title
flag is used to provide the desired title, and the --body
flag is used to specify the description. This allows you to create a pull request with the specified details directly.
Example output:
✓ Created pull request #126 (URL: https://github.com/octocat/repo/pull/126)
Use case 5: Start opening a pull request in the default web browser
Code:
gh pr create --web
Motivation: This use case is useful when you prefer to create pull requests using the GitHub web interface instead of the command-line interface. It allows you to quickly open the pull request page in your default web browser for further editing and customization.
Explanation: The --web
flag opens the pull request page in the default web browser instead of creating the pull request directly from the command line. This enables you to use the rich user interface provided by GitHub for creating and editing pull requests.
Example output:
Opening pull request in the default browser...
Conclusion
The gh pr create
command provides a convenient and powerful way to manage GitHub pull requests from the command line. It offers various options for creating pull requests, allowing users to interactively provide information or specify it directly through flags. Whether you prefer an interactive approach or an automated workflow, this command has you covered. By leveraging its versatility, you can streamline your pull request process and collaborate effectively on GitHub.