DEV Community

Cover image for Getting Started with GitHub CLI: A Quick Guide to Installation and Usage
LewisMK for Swahilipot Developers

Posted on

Getting Started with GitHub CLI: A Quick Guide to Installation and Usage

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
Enter fullscreen mode Exit fullscreen mode

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

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

$ sudo apt install git-all
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

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.






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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode
  1. 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
Enter fullscreen mode Exit fullscreen mode

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.


  3. Click on Generate new token.

  4. Enter a descriptive note for the token

  5. Select the desired scopes.

  6. Click on Generate token.

  7. Copy the generated token. Note: This token will be displayed only once. Ensure you securely store it.

Using the Token with the gh CLI

  1. Open your terminal or command prompt.

  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"
Enter fullscreen mode Exit fullscreen mode

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.



  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
  • 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".
Enter fullscreen mode Exit fullscreen mode
  1. Using Command Line:
- Ensure GitHub CLI is installed.
- Authenticate using
gh auth login
- Create sample files.
- Run
gh gist create
Enter fullscreen mode Exit fullscreen mode

Sample Gist

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

Top comments (0)