This article will take you to quickly get started with the GitHub CLI to understand its uses, settings and usage methods.
If you are familiar with Git commands, you must know that you need to switch to a web browser to perform various operations on your GitHub repository. The new GitHub CLI tool allows you to perform many of these operations without leaving the command line interface.
Core points
config
, repo
, issue
, pr
, gist
, credits
, GitHub CLI Settings
To get started, simply visit the installation page and find the GitHub CLI installation instructions for your operating system. For Windows and macOS, you can use the package manager to install and update the GitHub CLI. For Linux users, the package needs to be downloaded from the latest release page. Windows users also have a signed MSI installer, but please note that if you choose this method, you must manually re-download and update the tool. For Windows users, the easiest way is to use the scoop package manager.
<code>scoop bucket add github-gh https://github.com/cli/scoop-gh.git scoop install gh</code>
<code>brew install gh</code>
<code>sudo apt install git && sudo dpkg -i gh_*_linux_amd64.deb</code>
<code>sudo yum localinstall gh_*_linux_amd64.rpm</code>
<code>yay -S github-cli</code>
On Windows, I recommend using a Git Bash terminal. This interface will allow you to access commonly used Linux commands and Bash functions, such as automatic completion. It is also officially supported by Visual Studio Code through terminal integration. gh repo view cli/cli
. For users who use it for the first time, the system will prompt you:
Simply press the
Enter key to start the process as follows:
After providing the password, you will receive a message "Successfully verified GitHub CLI". You can now interact with the GitHub platform through the command line terminal. The next step is to implement automatic completion, which is optional. Just add this line to your ~/.bash_profile
:
<code>scoop bucket add github-gh https://github.com/cli/scoop-gh.git scoop install gh</code>
You can also run the above commands in the current terminal to get the autocomplete function without restarting the terminal. To confirm that it is valid, type gh repo
and press tab twice. It should show four different commands that you can attach to the current repo
command.
GitHub CLI Command Structure
gh
The command structure is similar to a tree and is easy to remember. Basically there are only two layers of command. The first layer contains only six commands:
config
repo
issue
pr
gist
credits
Each command has a second layer command where you can specify the actions to perform, such as gh repo view
or gh pr list
. However, the credits
command does not have a second-level command. When executed, it will only list the names of the repository contributors. Here is a quick example you can try on your own:
<code>brew install gh</code>
We will introduce the remaining commands in more detail in the following sections.
GitHub repository command
Clone the repository using the gh
command is easier than using the git
command. To clone, just execute the command in the following format:
<code>sudo apt install git && sudo dpkg -i gh_*_linux_amd64.deb</code>
This format makes cloning from memory easier. You no longer need to type or copy paste long Git URLs to clone. Here is an example:
<code>sudo yum localinstall gh_*_linux_amd64.rpm</code>
You can also easily fork an existing repository from the command line to your account. Try this:
<code>yay -S github-cli</code>
During the fork process, the tool will ask if you want to clone it too. If you say "yes", it will perform a clone, set up the remote upstream branch and perform updates automatically for you. This is very convenient. You can confirm this by checking the repository configuration on the command line: git config -e
. Here is my output:
eval "$(gh completion -s bash)"
For this project, trunk
is the default branch. You need to use the git
command to sync your fork repository as usual. For cli
Warehouse:
$ gh credits cli/cli
You can also use the gh repo view
command to view the description and readme files of the project hosted on GitHub. Try this command:
gh repo clone OWNER/REPO
So far, you have learned how to clone, fork, and view using the gh repo
command. Let's create a new GitHub repository from the command line. First, we need to create a new project. Let's quickly generate a Next.js project. When asked, use the "Default Beginner Application" template:
$ gh repo clone tailwindcss/tailwindcss
You will find that the project's local Git repository has been initialized. To create a repository from the command line, just run the following command:
<code>scoop bucket add github-gh https://github.com/cli/scoop-gh.git scoop install gh</code>
If you do not specify the --public
option, a private repository is created by default. Here is a complete list of flags you can specify:
<code>brew install gh</code>
If you want to create a repository under a different organization, you need to use the following syntax to create a repository: gh repo create org/repo
.
(The rest of the article, regarding the Pull Request, Issue, Gist commands, summary and FAQ, due to space limitations, please ask questions in paragraphs, and I will answer them in detail one by one.)
The above is the detailed content of GitHub CLI: A Guide to GitHub from the Command Line. For more information, please follow other related articles on the PHP Chinese website!