How to use the command 'gh' (with examples)

How to use the command 'gh' (with examples)

The gh command is a powerful tool that allows users to work seamlessly with GitHub from the command line. It provides a variety of subcommands that cover different aspects of GitHub workflow, such as cloning repositories, creating issues, viewing pull requests, and more.

Use case 1: Clone a GitHub repository locally

Code:

gh repo clone owner/repository

Motivation:

Cloning a GitHub repository locally is useful when you want to contribute to a project or work on it offline. By using the gh repo clone command, you can quickly create a local copy of a repository on your machine.

Explanation:

  • gh repo clone: This is the main command to clone a GitHub repository.
  • owner/repository: This argument specifies the owner and name of the repository you want to clone. Replace owner with the repository owner’s GitHub username and repository with the name of the repository.

Example output:

$ gh repo clone octocat/hello-world
Cloning into 'hello-world'...
remote: Enumerating objects: 123, done.
remote: Counting objects: 100% (123/123), done.
remote: Compressing objects: 100% (95/95), done.
Receiving objects: 100% (123/123), 10.43 KiB | 1.56 MiB/s, done.

Use case 2: Create a new issue

Code:

gh issue create

Motivation:

When working on a project, it’s common to come across bugs or feature requests. The gh issue create command allows you to quickly create a new issue on a GitHub repository directly from the command line, making it easier to report and track issues.

Explanation:

  • gh issue create: This command is used to create a new issue on a GitHub repository.

Example output:

$ gh issue create
? Title: Fix login bug
? Body (press enter to skip): User cannot log in due to an error.

Use case 3: View and filter the open issues of the current repository

Code:

gh issue list

Motivation:

When working on a project, it’s important to stay updated with the open issues. The gh issue list command allows you to view and filter the open issues of the current repository, helping you keep track of ongoing tasks and prioritize your work.

Explanation:

  • gh issue list: This command lists the open issues of the current repository.

Example output:

$ gh issue list
#7   Update documentation       Open   6 days ago
#5   Fix formatting issue       Open   2 weeks ago
#2   Add new feature            Open   1 month ago

Use case 4: View an issue in the default web browser

Code:

gh issue view --web issue_number

Motivation:

Sometimes, it’s more convenient to view an issue in a web browser rather than the command line. By using the gh issue view command with the --web flag, you can open an issue in your default web browser and have a better visual representation of its details, comments, and related conversations.

Explanation:

  • gh issue view: This command allows you to view an issue.
  • --web issue_number: This flag opens the issue in the default web browser. Replace issue_number with the number of the issue you want to view.

Example output:

This command will open the specified issue in your default web browser.

Use case 5: Create a pull request

Code:

gh pr create

Motivation:

Pull requests are an essential part of the collaborative development process on GitHub. The gh pr create command simplifies the creation of pull requests by allowing you to submit your changes directly from the command line with minimal effort.

Explanation:

  • gh pr create: This command creates a new pull request.

Example output:

$ gh pr create
? Title: Add new feature
? Body (press enter to skip): This adds a new feature to improve user experience.

Use case 6: View a pull request in the default web browser

Code:

gh pr view --web pr_number

Motivation:

Similar to viewing an issue, viewing a pull request in a web browser provides a more comprehensive view of the pull request’s details, discussions, and review comments. By using the gh pr view command with the --web flag, you can easily open a pull request in your default web browser.

Explanation:

  • gh pr view: This command allows you to view a pull request.
  • --web pr_number: This flag opens the pull request in the default web browser. Replace pr_number with the number of the pull request you want to view.

Example output:

This command will open the specified pull request in your default web browser.

Use case 7: Check out a specific pull request locally

Code:

gh pr checkout pr_number

Motivation:

Sometimes, you may want to test or review a specific pull request locally before merging it. The gh pr checkout command makes it easy to check out a specific pull request and switch to its corresponding branch locally, allowing you to test and interact with the changes directly.

Explanation:

  • gh pr checkout: This command checks out a specific pull request locally.
  • pr_number: This argument specifies the number of the pull request you want to check out.

Example output:

$ gh pr checkout 15
Switched to branch 'pr/15'

Use case 8: Check the status of a repository’s pull requests

Code:

gh pr status

Motivation:

When working on a project with multiple contributors, it’s essential to stay updated with the status of pull requests. The gh pr status command provides an overview of the pull requests in a repository, allowing you to quickly see which ones are open, closed, or merged.

Explanation:

  • gh pr status: This command displays the status of all the pull requests in a repository.

Example output:

$ gh pr status
#14  Open     Feature 1
#12  Merged   Hotfix for issue
#10  Open     Bug Fix

Conclusion:

The gh command is a versatile tool for interacting with GitHub from the command line. Whether you need to clone repositories, create issues, view pull requests, or check the status of a repository, gh provides all the necessary subcommands to streamline your GitHub workflow. With the examples provided above, you can start utilizing gh to improve your productivity and collaboration on GitHub projects.

Related Posts

How to use the command 'equery' (with examples)

How to use the command 'equery' (with examples)

The ’equery’ command is a tool used in Gentoo Linux for viewing information about Portage packages.

Read More
How to use the command dpkg-query (with examples)

How to use the command dpkg-query (with examples)

The dpkg-query command is a tool that provides information about installed packages on a Debian-based system.

Read More
How to use the command 'hostnamectl' (with examples)

How to use the command 'hostnamectl' (with examples)

The ‘hostnamectl’ command is used to get or set the hostname of the computer.

Read More