Working with GitHub Repositories (with examples)
Introduction
In this article, we will explore different use cases of the gh repo
command, which is part of the GitHub CLI (Command Line Interface). This command allows us to work with GitHub repositories right from the terminal. We will cover various operations such as creating a new repository, cloning, forking, viewing repositories, and listing repositories. Let’s get started!
1: Creating a new repository
To create a new repository using the gh repo
command, we can use the following code:
gh repo create name
Motivation: This command is useful when initializing a new project and wanting to quickly create a repository on GitHub with the same name as the current directory. It simplifies the process of setting up a repository without switching to the GitHub website.
Explanation: The create
subcommand is used to create a new repository. The name
argument represents the name of the repository you want to create. If the repository name is not specified, it defaults to the name of the current directory.
Example Output: When running gh repo create my-repo
, a new repository named “my-repo” will be created on GitHub under your account.
2: Cloning a repository
To clone a repository using the gh repo
command, we can use the following code:
gh repo clone owner/repository
Motivation: This command is useful when you want to quickly clone an existing repository from GitHub to your local machine. It provides a convenient way of cloning repositories without having to manually copy the repository URL.
Explanation: The clone
subcommand is used to clone a repository. The owner/repository
argument represents the owner (user or organization) and the name of the repository you want to clone. This command fetches the repository from GitHub and sets it up locally.
Example Output: When running gh repo clone octocat/Spoon-Knife
, the repository named “Spoon-Knife” owned by the user “octocat” will be cloned to your local machine.
3: Forking and cloning a repository
To fork and clone a repository using the gh repo
command, we can use the following code:
gh repo fork owner/repository --clone
Motivation: This command is useful when you want to contribute to an existing repository by creating a fork of it under your account, and then clone it to your local machine for development.
Explanation: The fork
subcommand is used to fork a repository. The owner/repository
argument represents the owner (user or organization) and the name of the repository you want to fork. The --clone
flag is used to clone the forked repository after it has been created.
Example Output: When running gh repo fork octocat/Spoon-Knife --clone
, the repository named “Spoon-Knife” owned by the user “octocat” will be forked to your account and then cloned to your local machine.
4: Viewing a repository in the default web browser
To view a repository in the default web browser using the gh repo
command, we can use the following code:
gh repo view repository --web
Motivation: This command is useful when you want to quickly open a repository in the default web browser to view its details, branches, issues, or pull requests. It provides a convenient way of navigating to the repository without manually typing the URL.
Explanation: The view
subcommand is used to view a repository. The repository
argument represents the name of the repository you want to view. The --web
flag is used to open the repository in the default web browser.
Example Output: When running gh repo view my-repo --web
, the repository named “my-repo” will be opened in the default web browser, allowing you to explore its content and associated information.
5: Listing repositories owned by a specific user or organization
To list repositories owned by a specific user or organization using the gh repo
command, we can use the following code:
gh repo list owner
Motivation: This command is useful when you want to retrieve a list of repositories owned by a particular user or organization. It helps in obtaining an overview of their projects and accessing repositories of interest.
Explanation: The list
subcommand is used to list repositories. The owner
argument represents the owner (user or organization) for which you want to fetch the repositories. If the owner is not specified, it defaults to the currently logged-in user.
Example Output: When running gh repo list octocat
, a list of repositories owned by the user “octocat” will be displayed, including their names, descriptions, and other relevant information.
6: Listing only non-fork repositories and limiting the number of repositories to list
To list only non-fork repositories and limit the number of repositories to list using the gh repo
command, we can use the following code:
gh repo list owner --source -L limit
Motivation: This command is useful when you want to filter the repositories list to only display non-fork repositories. Additionally, you can limit the number of repositories shown to focus on the most relevant ones.
Explanation: The --source
flag is used to display only non-fork repositories. The owner
argument represents the owner (user or organization) for which you want to fetch the repositories. If the owner is not specified, it defaults to the currently logged-in user. The -L limit
flag is used to specify the maximum number of repositories to list, with the default value being 30.
Example Output: When running gh repo list octocat --source -L 10
, a list of the 10 non-fork repositories owned by the user “octocat” will be displayed, including their names, descriptions, and other relevant information.
7: Listing repositories with a specific primary coding language
To list repositories with a specific primary coding language using the gh repo
command, we can use the following code:
gh repo list owner --language language_name
Motivation: This command is useful when you want to filter repositories by their primary coding language. It allows you to find repositories written in a specific language, making it easier to discover projects of interest or contribute to repositories using a particular language.
Explanation: The owner
argument represents the owner (user or organization) for which you want to fetch the repositories. If the owner is not specified, it defaults to the currently logged-in user. The --language
flag is used to specify the primary coding language of the repositories you want to list.
Example Output: When running gh repo list octocat --language python
, a list of repositories owned by the user “octocat” and written in Python will be displayed, including their names, descriptions, and other relevant information.
Conclusion
In this article, we have covered various use cases of the gh repo
command, which allows us to work with GitHub repositories directly from the command line. We have explored creating new repositories, cloning, forking and cloning, viewing repositories in the web browser, and listing repositories based on owner, non-fork status, and primary coding language. Armed with this knowledge, you can now leverage the power of the GitHub CLI to streamline your repository operations efficiently.