Getting Started with GitHub CLI: A Quick Guide to Installation and Usage
Tags: webdev, beginners, programming, github
Published: 2024-05-27 17:08:25

**Introduction**

Installing GitHub (gh)  the command-line interface (CLI) is a straightforward process that allows you to interact with GitHub repositories, issues, pull requests, and more directly from your terminal or command prompt. Here's a brief and clear guidance to installing GitHub CLI:

**Installing Git**

Before you start using Git, you have to make it available on your computer. it’s recommended to update to the latest version. You can either install it as a package or via another installer, or download the source code and compile it yourself.

Installing on Linux

To install Git on Linux, use your distribution's package manager. On Fedora, use dnf.

    $ sudo dnf install git-allautotools

For more options, there are instructions for installing on several different Unix distributions on the Git website, at https://git-scm.com/download/linux.


![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533969179_l1.jpg)


If you’re on a Debian-based distribution, such as Ubuntu, try `apt`:


    $ sudo apt install git-all



Installing on mac OS

The easiest way to install Git on mac OS is installing the X Code Command Line Tools. On Mavericks (10.9) or above you can do this simply by trying to run `git` from the Terminal the very first time.


    $ git --version

If you don’t have it installed already, it will prompt you to install it.
If you want a more up to date version, you can also install it via a binary installer. A mac OS Git installer is maintained and available for download at the Git website, at https://git-scm.com/download/mac.

![Git macOS installer](https://git-scm.com/book/en/v2/images/git-osx-installer.png)



Installing on Windows

To install Git on Windows, you can download the official build from the Git website at https://git-scm.com/download/win. This will automatically start the download.

Alternatively, you can use the Git Chocolatey package, which provides an automated installation. Note that the Chocolatey package is community-maintained.

For more information on the Git for Windows project, which is separate from Git itself, you can visit https://gitforwindows.org.

![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533797716_g8.webp)
![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533797788_g74.png)
![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533797833_g62.png)

![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533797859_g53.png)
![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533797941_g47.png)
![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533798013_g35.png)

![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533798077_g26.png)
![](https://paper-attachments.dropboxusercontent.com/s_C29A35DE5FB8FCB32C2C80BC6497DEF758E0F2252E68F39D75BE5C60AE8E9EA9_1716533798113_g1.jpg)

Installing from Source

To install the latest version of Git from source, you'll need to have the following libraries that Git depends on: auto-tools, curl, zlib, openssl, expat, and libiconv.
On a system with `dnf` (e.g., Fedora) or `apt-get` (e.g., Debian-based), you can use the following commands to install the minimal dependencies for compiling and installing the Git binaries:

    # Fedora (dnf)
    sudo dnf install autotools curl-devel zlib-devel openssl-devel expat-devel libiconv-devel
    
    # Debian-based (apt-get) 
    sudo apt-get install autotools-dev libcurl4-gnutls-dev libz-dev libssl-dev libexpat1-dev libiconv-hook-dev

This approach provides you with the most recent version of Git, though the binary installers are generally only a bit behind.


    $ sudo dnf install dh-autoreconf curl-devel expat-devel gettext-devel \
      openssl-devel perl-devel zlib-devel
    $ sudo apt-get install dh-autoreconf libcurl4-gnutls-dev libexpat1-dev \
      gettext libz-dev libssl-dev

In order to be able to add the documentation in various formats (doc, html, info), these additional dependencies are required:


    $ sudo dnf install asciidoc xmlto docbook2X
    $ sudo apt-get install asciidoc xmlto docbook2x

If you’re using a Debian-based distribution (Debian/Ubuntu/Ubuntu-derivatives), you also need the `install-info` package:


    $ sudo apt-get install install-info

If you’re using a RPM-based distribution (Fedora/RHEL/RHEL-derivatives), you also need the get opt package (which is already installed on a Debian-based distro):
$ sudo dnf install getopt

Additionally, if you’re using Fedora/RHEL/RHEL-derivatives, you need to do this due to binary name differences:


    $ sudo ln -s /usr/bin/db2x_docbook2texi /usr/bin/docbook2x-texi

When you have the necessary dependencies, you can obtain the latest tagged Git release from several sources. The tarball is available at the kernel.org site (https://www.kernel.org/pub/software/scm/git) or the GitHub mirror (https://github.com/git/git/tags). The GitHub page typically provides clearer visibility into the latest version, while the kernel.org site offers release signatures for download verification.

After retrieving the tarball, you can proceed with the compilation and installation process.


    $ tar -zxf git-2.8.0.tar.gz
    $ cd git-2.8.0
    $ make configure
    $ ./configure --prefix=/usr
    $ make all doc info
    $ sudo make install install-doc install-html install-info

After this is done, you can also get Git via Git itself for updates:


    $ git clone https://git.kernel.org/pub/scm/git/git.git


**Key GITHUB CLI Commands**

Repository Management


1. Create a repo

A repository (repo) is an online location where software code, configuration files, and documentation are stored (Macgregor, 2023). This centralized location makes it easy to update and manage the software, especially for projects with multiple contributors.


    gh repo create GH_CLI_TEST --add-readme --public


2. Clone a repo

Cloning a repo is the process of creating a local copy of an existing online repo, such as one hosted on GitHub. The user can work on the local copy without affecting the main copy and only merge changes when approved.


    gh repo clone https://github.com/zpqrtbnk/test-repo.git


3. Fork a repo

Forking a repo involves having a copy of another person’s repo in your online storage. Unlike cloning, the fork remains online, but can also be changed without affecting the main copy. 


    gh repo fork https://github.com/zpqrtbnk/test-repo.git


4. View repo details

The user can also view repo details such as the description and the README file.


    gh repo view https://github.com/zpqrtbnk/test-repo.git


5. Delete a repo

Once a repo is no longer needed, it can be deleted using the following command;


    gh repo delete https://github.com/zpqrtbnk/test-repo.git

**Instructions for Authenticating with GIT HUB_TOKEN**

Generating a GitHub Token


1. Visit GitHub and log in to your account.
2. Navigate to Settings > Developer settings > Personal access tokens.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793202735_Screenshot+118.png)
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793278114_Screenshot+119.png)
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793407494_Screenshot+120.png)

3. Click on Generate new token.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793498363_Screenshot+121.png)

4. Enter a descriptive note for the token
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793591234_Screenshot+122.png)

5. Select the desired scopes.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793692000_Screenshot+123.png)

6. Click on Generate token.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793768569_Screenshot+124.png)

7. Copy the generated token. Note: This token will be displayed only once. Ensure you securely store it.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716793857176_Screenshot+125.png)

                                
Using the Token with the gh CLI


1. Open your terminal or command prompt.
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716794193395_Screenshot+126.png)

2. Execute the following command to set the token as the default authentication method:


    ```gh auth login```


3. Paste the generated token when prompted.
4. Follow the prompts to complete the authentication process.
5. Once authenticated, the gh CLI will utilize the GIT HUB_TOKEN for subsequent interactions with GitHub.

 

Security Considerations

Treat your GitHub Token like a password. Do not share it publicly or store it in unsecured locations.
Regularly review and revoke unnecessary tokens from your GitHub account to maintain security.


**GitHub Authentication Using SSH**

To authenticate with GitHub using SSH via the GitHub Command Line Interface (CLI), you'll need to follow these steps:
 In your terminal, perform the following command that will create an ssh key for you.
 

     ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Add SSH Key to GitHub: If in Linux, you can by using cp command copy the contents of the file with the public key (id_rsa. pub) to the clipboard. 

Next to it, go to GitHub, go to settings / SSH & GPG keys and add a new SSH key. To do this, copy the public key you have been provided with; paste this into the relevant field and then click on save.

![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716795445049_Screenshot+118.png)
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716795468020_Screenshot+127.png)
![](https://paper-attachments.dropboxusercontent.com/s_A6075845CF68F47D005822E9A5509C8F280B546F3249114996591BBDFE5D5968_1716795474327_Screenshot+128.png)

- Test SSH Connection: To test that your SSH key is set up correctly, run:

    ```
ssh -T git@metahashofficial
``` 

- You should see a message confirming that you've successfully authenticated.

The authenticity of host 'github.com (IP address)' can't be established. RSA key fingerprint is SHA256:xxxxxxx... (actual fingerprint will be shown) Are you sure you want to continue connecting (yes/no)?

- Configure gh CLI to use SSH: Now that your SSH key is set up, you need to configure the gh CLI to use SSH. Run:

    ```
gh config set git_protocol ssh
```


- Authenticate with GitHub using a personal access token by selecting the token authentication option and entering your token when prompted.

**Creating Pull Requests**

A pull request is a method used in version control systems where a developer proposes changes to a codebase, allowing others to review and discuss the modifications before they are merged.

- Create a Pull Request:

    ```
gh pr create --title "Title of the pull request" --body "Description"
```

- List a pull request:

   ```
gh pr list
```

- Checkout a Pull Request locally:

Checkout is a command in version control systems that allows a user to switch between different branches or versions of the repository, or to retrieve a specific version of a file.

    gh pr checkout PR_NUMBER

- Merge a Pull Request:

Merge is the process of integrating changes from different branches or versions of a codebase into a single unified branch, resolving any conflicts that arise.

    gh pr merge PR_NUMBER

- View pull Request  status:

    ```
gh pr status
```

-  Request reviews:

    ```
gh pr requestreview PR_NUMBER --reviewer username
``` 

- Add a Comment to a Pull Request:

    ```
gh pr comment PR_NUMBER --body "Your comment here"
```

- Close a Pull Request:

    ```
gh pr close PR_NUMBER
```

**Workflow Management**

This process involves effectively using issues and pull requests to coordinate development tasks and streamline the contribution process. Here's how these elements contribute to workflow management;

1. Issue Tracking:
    - Tasks, bugs, and features are organized.
    - Labels, milestones, and assignee's prioritize and track progress.
2. Feature Development:
    - New branches relate to specific issues.
    - Changes are linked to requirements for traceability.
3. Code Reviews:
    - Pull requests enable feedback and approval.
    - Reviewers ensure code quality and alignment.
4. CI/CD Integration:
    - Automated tests validate changes.
    - Approved changes trigger automatic deployment, speeding up delivery.


**Gist Management and Configuration**

Gist management and configuration are the process of working with different snippets of code or pieces of notes, if not any type of textual information, within certain specialized platform like GitHub.


1. Using the Web Interface:


    - Navigate to the GitHub Gist
    - Click "New gist".
    - Provide a filename with the appropriate extension.
    - Enter your content.
    - Set visibility to public or secret.
    - Click "Create public gist" or "Create secret gist".
    
2. Using Command Line:

    - Ensure GitHub CLI is installed.
    - Authenticate using
    gh auth login
    - Create sample files.
    - Run
    gh gist create
![Sample Gist](https://paper-attachments.dropboxusercontent.com/s_C78564E954A9CCA342B99352E9D517412C97DC9E1A235BFE1F1459568AE1F324_1716539325501_Screenshot+2024-05-24+112807.png)

**Conclusion**

GitHub CLI offers streamlined DevOps tasks through its installation, configuration, and core functionalities. Continuously evolving, it provides efficiency and productivity gains for code management via the terminal. We encourage independent exploration and reference to official documentation for further guidance, welcoming feedback to enhance future discussions on this impactful collaboration tool.

**References**


- [Getting started with installing git.](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)
- [Creating gists](https://docs.github.com/en/get-started/writing-on-github/editing-and-sharing-content-with-gists/creating-gists)
- [Managing issues and pull requests](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue)
- [Automatic token authentication](https://docs.github.com/en/actions/security-guides/automatic-token-authentication)
- [GH manual](https://cli.github.com/manual/)