|
| 1 | +--- |
| 2 | +title: "Installing and Using GitHub CLI" |
| 3 | +sidebar_label: "2. GitHub CLI (gh)" |
| 4 | +sidebar_position: 2 |
| 5 | +description: "Learn how to install and use the GitHub CLI (gh) to manage your repositories, issues, and pull requests directly from the command line. This guide covers installation steps for Windows, macOS, and Linux, as well as essential commands to streamline your workflow." |
| 6 | +tags: ["GitHub CLI", "gh", "Command Line", "GitHub Tools", "Developer Workflow"] |
| 7 | +keywords: ["GitHub CLI", "gh", "Command Line Interface", "GitHub Tools", "Developer Workflow"] |
| 8 | +--- |
| 9 | + |
| 10 | +While the GitHub website is beautiful and easy to use, professional developers often find it faster to perform tasks directly from the command line. The **GitHub CLI (`gh`)** is the official tool that brings GitHub’s issues, pull requests, and other features to your terminal. |
| 11 | + |
| 12 | +At **CodeHarborHub**, we use `gh` to streamline our workflow and keep our focus on the code. |
| 13 | + |
| 14 | +:::tip |
| 15 | +Don't worry if you're new to the command line! The GitHub CLI is designed to be user-friendly, and this guide will help you get started step by step. By the end, you'll be able to manage your GitHub projects without ever leaving your terminal. |
| 16 | +::: |
| 17 | + |
| 18 | +## Why use the CLI? |
| 19 | + |
| 20 | +1. **Context Switching:** You don't have to leave VS Code or your terminal to open a Pull Request. |
| 21 | +2. **Automation:** You can write scripts to create repositories or check status automatically. |
| 22 | +3. **Speed:** Most common actions (like cloning a repo or checking a PR) are 3x faster with a single command. |
| 23 | + |
| 24 | +## Installation |
| 25 | + |
| 26 | +<Tabs> |
| 27 | + <TabItem value="windows" label="Windows" default> |
| 28 | + |
| 29 | +Open PowerShell or Command Prompt and run: |
| 30 | + |
| 31 | +```bash |
| 32 | +winget install --id GitHub.cli |
| 33 | +``` |
| 34 | + |
| 35 | +</TabItem> |
| 36 | +<TabItem value="mac" label="macOS"> |
| 37 | + |
| 38 | +Use Homebrew: |
| 39 | + |
| 40 | +```bash |
| 41 | +brew install gh |
| 42 | +``` |
| 43 | + |
| 44 | +</TabItem> |
| 45 | +<TabItem value="linux" label="Linux"> |
| 46 | + |
| 47 | +For Debian/Ubuntu: |
| 48 | + |
| 49 | +```bash |
| 50 | +type -p curl >/dev/null || (sudo apt update && sudo apt install curl -y) |
| 51 | +curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg |
| 52 | +sudo chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg |
| 53 | +echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null |
| 54 | +sudo apt update |
| 55 | +sudo apt install gh -y |
| 56 | +``` |
| 57 | + |
| 58 | +</TabItem> |
| 59 | +</Tabs> |
| 60 | + |
| 61 | +## Getting Started: Authentication |
| 62 | + |
| 63 | +Once installed, you need to link the CLI to your GitHub account. Run: |
| 64 | + |
| 65 | +```bash |
| 66 | +gh auth login |
| 67 | +``` |
| 68 | + |
| 69 | +Follow the interactive prompts: |
| 70 | + |
| 71 | + * Select **GitHub.com** (unless you're using GitHub Enterprise). |
| 72 | + * Choose **HTTPS** or **SSH**. |
| 73 | + * Select **Login with a web browser** (this will open a page to authorize the app). |
| 74 | + |
| 75 | +## Essential Commands for Daily Work |
| 76 | + |
| 77 | +| Task | Command | Description | |
| 78 | +| :--- | :--- | :--- | |
| 79 | +| **Repo Status** | `gh repo view` | Shows the README and basic info for the current repo. | |
| 80 | +| **List PRs** | `gh pr list` | Lists all open Pull Requests in the repository. | |
| 81 | +| **Check out PR** | `gh pr checkout <number>` | Downloads a teammate's PR to your PC to test it. | |
| 82 | +| **Create PR** | `gh pr create` | Opens a new Pull Request for your current branch. | |
| 83 | +| **View Issues** | `gh issue list` | Shows all open bugs or tasks. | |
| 84 | + |
| 85 | +## Common Workflow Example |
| 86 | + |
| 87 | +Imagine you want to start a new project for **CodeHarborHub**. Instead of clicking through the website: |
| 88 | + |
| 89 | +1. **Create a new repo:** |
| 90 | + ```bash |
| 91 | + gh repo create my-new-project --public --clone |
| 92 | + ``` |
| 93 | +2. **Change into the folder:** |
| 94 | + ```bash |
| 95 | + cd my-new-project |
| 96 | + ``` |
| 97 | +3. **Open in VS Code:** |
| 98 | + ```bash |
| 99 | + code . |
| 100 | + ``` |
| 101 | + |
| 102 | +*You just set up a full project in 5 seconds!* |
| 103 | + |
| 104 | +## The "Status" View |
| 105 | + |
| 106 | +One of the best features of `gh` is the **Dashboard** view. Run this to see everything happening with your work: |
| 107 | + |
| 108 | +```bash |
| 109 | +gh status |
| 110 | +``` |
| 111 | + |
| 112 | +It shows your relevant Pull Requests, their review status, and if your **GitHub Actions** tests passed or failed. |
| 113 | + |
| 114 | +:::tip |
| 115 | +You can use the `--web` flag with almost any command (e.g., `gh pr view --web`) to instantly open that specific page in your browser if you need to see the full UI. |
| 116 | +::: |
0 commit comments