#GitHub #CLI - Crash Course

#GitHub #CLI - Crash Course

Introduction to GitHub CLI

Overview of the Video

  • The video introduces GitHub CLI, a tool that integrates GitHub functionalities directly into the terminal, enhancing user experience beyond traditional web interfaces.
  • The host mentions that while this channel primarily focuses on Scala, it also covers broader software development topics.

History and Development of GitHub CLI

  • GitHub's co-founder Chris created an earlier tool called Hub in 2009, which utilized GitHub's API but is not an official project.
  • In October 2019, GitHub released its official CLI tool named "gh," opting for a different direction than Hub to avoid disrupting existing users.
  • The new CLI has gained significant traction with over 19,000 stars and contributions from more than 100 developers since its launch.

Installation Process

Setting Up the Environment

  • The host shares their experience as a long-time Windows user who now runs software in virtual machines and uses Windows Subsystem for Linux (WSL).
  • They demonstrate installing the GitHub CLI on an Ubuntu 20.04 instance using custom install scripts hosted on their personal repository.

Installation Steps

  • Users can find installation scripts by visiting install.davinci.com, where they can search for repositories related to installations.
  • The installation process involves copying two lines of code: one to download the script and another to make it executable.

Using GitHub CLI

Initial Setup and Commands

  • After installation, users can check the version of gh installed , confirming successful setup.
  • Basic commands include gh help, which provides information about available commands like issues, pull requests (PR), releases, and repositories.

Features of GitHub CLI

  • The command line interface allows seamless interaction with GitHub features directly from the terminal without needing a browser.
  • Users can generate completion scripts for various shells; however, some may need manual configuration depending on their shell environment.

Exploring Further Capabilities

Configuration and Help Resources

  • Users have access to configuration files for customizing their experience with the CLI tool.
  • Additional resources are available at cli.github.com for further learning about using and configuring the GitHub CLI effectively.

How to Set Up Zsh Completions for GitHub CLI

Setting Up Zsh Completions

  • The speaker discusses how to set up completions for the Zsh shell, emphasizing that different shells handle completions differently. For Zsh, all completions can be placed in a specific file.
  • To generate completions, the command gh completions is used with the specified shell (Zsh), which outputs all available completions for the GitHub CLI tool.
  • The output of the completion command is redirected into a file named _gh, following a common naming convention for such files.
  • A new directory called zsh_completions is created to store the generated completion file, and it is moved there from its original location.

Configuring .zshrc

  • The speaker opens their .zshrc configuration file to add the newly created directory to the $fpath, which allows Zsh to recognize where to find custom completion scripts.
  • After modifying .zshrc, they run compinit to initialize the new completions and then open a new shell session to apply changes.

Testing Completions

  • The speaker demonstrates testing the setup by typing gh followed by pressing tab multiple times, showing that various commands and options are now autocompleted correctly.

Authentication Process

  • They explain that while reading data from GitHub's API does not require authentication, writing actions do. The command gh auth login initiates an interactive authentication process.
  • Users can authenticate via a web browser or by pasting an authentication token. The speaker opts for browser-based login and explains how tokens can grant additional permissions beyond what CLI supports.

Token Management

  • During authentication, users receive a one-time code that must be copied and pasted into their browser after opening GitHub.com.
  • After logging in through the browser, no visible token appears in settings immediately; however, it confirms successful connection without creating a new token entry in user settings.

Finalizing Configuration

  • Users are prompted to choose between HTTPS or SSH as their default git protocol during setup. They select SSH for secure connections.
  • When generating tokens manually on GitHub, it's important to enable necessary permissions like 'repo' access for full functionality when using CLI tools.

GitHub CLI Configuration and Token Management

Overview of GitHub API and CLI

  • The speaker discusses using the GitHub API via a specific URL, which returns a JSON response. They mention that while this method is functional, using curl might be more straightforward.

Token Storage and Security

  • The configuration files for the GitHub CLI are located in a specified directory. The speaker highlights three key files: config, host.yml, and state.
  • The hosts file contains critical information such as the user's authentication token, which should remain confidential to prevent unauthorized access.

Refreshing Tokens

  • A built-in command exists within the GitHub CLI to refresh tokens. The speaker demonstrates this process, emphasizing the importance of keeping tokens secure.
  • After refreshing the token, they note that it’s crucial to regenerate any displayed tokens after recording to maintain security.

Configuration Options

  • Users can synchronize their configurations through various methods. The configuration file allows customization of default protocols and editor settings.
  • Users can manipulate configuration either directly or through commands like gh config. This flexibility mirrors traditional git configurations.

Environment Variables and Command Usage

  • Environment variables can be set for easier command execution without being in a specific repository directory. This feature enhances usability across different environments.
  • Commands related to environment variables include options for setting up tokens for CI servers or specifying default repositories.

Running Commands with GitHub CLI

  • With proper configuration, users can execute commands like listing issues from any directory without needing an active git repository present.
  • The speaker illustrates how to list issues using shorthand commands in the CLI, demonstrating efficiency in command usage.

This structured summary captures essential insights from the transcript regarding configuring and managing tokens with the GitHub CLI while providing clear timestamps for reference.

Understanding GitHub CLI Commands

Fetching Issues with Limits

  • You can fetch a limited number of issues using the -l flag, for example, -l 60 to retrieve 60 issues.

Viewing Specific Issues

  • To view a specific issue, you need to specify its ID (e.g., 2158). The output will be rendered in markdown format.

Opening Issues in Browser

  • Use the command --web or shorthand -w to open an issue directly in your web browser. This functionality is consistent across various commands except for the repo command.

Cloning Repositories

  • When cloning a repository, you can omit specifying the repository name if you're already within its directory. For instance, use gh repo clone cli. The default branch may not always be 'master' or 'main'; it could be named 'trunk'.

Running Commands Without Repository Specification

  • Once inside a cloned repository's directory, commands like gh issue list work without needing to specify the repository again since it recognizes the remote setup with GitHub.

Exploring Pull Requests and Issues

Similarities Between PRs and Issues

  • Pull requests (PRs) and issues share similar APIs; thus, commands for both are often interchangeable. For example, you can list PRs using gh pr list.

Viewing PR Details

  • You can view details of specific pull requests by their ID (e.g., gh pr view 2157). Note that CLI outputs may lack some conversational context compared to web views.

Checking Differences in PR Changes

  • Use the command gh pr diff <PR_ID> to see what changes were made in a pull request. This will display differences through your default pager tool.

Creating and Managing Branches

Transitioning from Viewing to Writing Code

  • The tutorial emphasizes moving beyond just viewing issues and pull requests towards actively creating branches and making contributions as part of project management on GitHub. This includes creating new branches for fixes or features based on existing issues.

Creating Repositories with Git and GitHub

Introduction to Repository Creation

  • The speaker introduces two methods for creating repositories: starting from GitHub or starting from a local Git setup.
  • The first method involves creating a repository directly on GitHub, cloning it, and then working locally.

Creating a Repository on GitHub

  • The speaker demonstrates creating a private repository named "playground" in their account using the command gh repo create.
  • A new directory called "playground" is created, which contains an empty .git directory but no commits yet.

Deleting the Repository

  • The speaker explains that deleting the repository must be done through the settings on GitHub as CLI deletion isn't possible.
  • After deletion, they plan to demonstrate the second method of repository creation.

Using Templates to Create Projects

  • The speaker introduces a tool called "generate" for downloading templates from GitHub to create projects.
  • They create a Scala project using g8, resulting in another "playground" project that is now initialized as a real project.

Initializing Local Git Repository

  • The speaker initializes a local git repository using an alias (gi) that combines git init, adds files, and makes an initial commit.
  • They run commands similar to previous steps but within this newly created directory containing an initialized git repository.

Pushing Changes to Remote Repository

  • After setting up the remote connection, they push their master branch using another alias (gpsup), which simplifies pushing changes.
  • This results in successfully viewing the newly created remote repository with its initial commit visible.

Creating Issues as a Developer

  • The speaker transitions into discussing how developers can create issues when noticing something unusual in their workflow.

Creating Issues and Forking Repositories with GitHub CLI

Creating an Issue

  • The speaker initiates the process of creating an issue without cloning any repository, emphasizing that being in a directory containing a git repository is not necessary.
  • The title for the issue is set as "Hello World is so old school," and the speaker uses their editor (Vim) to add a body to the issue, showcasing GitHub's markdown style.
  • After submitting the issue, it can be viewed using gh issue list, confirming that one issue has been created successfully.

Viewing and Managing Issues

  • The speaker discusses how to view the created issue both in the terminal and browser, highlighting its content which includes a wave emoji.
  • If issues are ignored by maintainers, users typically would fork and clone repositories through web UI; however, this can also be done via GitHub CLI.

Forking and Cloning Repositories

  • The speaker demonstrates forking a repository directly from the command line using repo fork, which creates a private copy of the original repository.
  • Upon forking, there’s an option to clone it immediately. The default account used for cloning is mentioned as "agel steel."

Branch Creation and Development

  • Using an alias (gcb), the speaker creates a new branch named "feature one" within their cloned repository while noting they should create a proper git tutorial in future.
  • Instead of switching to VS Code, they continue working within Vim on their terminal setup while importing builds related to Scala tools.

Editing Files and Running Code

  • The speaker modifies text within files using emojis by accessing an emoji keyboard feature available on Windows during editing sessions in Vim.
  • They run code using an alias (bloop) designed for Scala development, confirming successful execution with output displaying "hi cool kids."

Committing Changes and Creating Pull Requests

  • After making changes, they use another alias (gcam) to commit modifications with reference to fixing issues.
  • Instead of pushing changes through web UI for pull requests (PR), they utilize gh pr create directly from CLI, explaining different types of forks based on project direction disagreements.

Creating and Managing Pull Requests with GitHub CLI

Overview of Pull Requests and Issues

  • The tutorial begins by discussing the creation of a pull request (PR) using the command line interface (CLI), emphasizing ease of use with commands like ghpr list.
  • It is noted that both PRs and issues are stored in the same SQL database, generating unique IDs for each.
  • A distinction is made between PRs and issues; PRs include a patch file, which can be accessed via .patch at the end of the URL.

Reviewing Pull Requests

  • While reviewing PRs can be done through CLI, it’s often more visual on the website. The speaker prefers to review visually but continues with CLI for this tutorial.
  • The command ghpr checkout is used to create a local branch from a PR, allowing users to work on it locally.

Merging Pull Requests

  • The speaker discusses merging a PR using ghpr merge, mentioning an option (-d) that deletes both local and remote branches post-merge.
  • Despite expectations, local branches were not deleted as anticipated after merging, although they were removed from GitHub.

Branch Management

  • After merging, only the master branch remains visible locally. This leads into managing merged branches effectively.
  • An alias (gbda) is introduced for deleting branches that have already been merged into master.

Creating and Managing Aliases

  • Users can create aliases directly in their config file or through commands like gh alias set, enhancing workflow efficiency.
  • Examples are provided on how to update existing aliases or delete them using simple commands.

Advanced Alias Creation

  • More complex aliases can be created that execute shell commands. The speaker shows examples from another virtual machine setup.
  • Instructions are given on how to set up these advanced aliases using gh alias set, demonstrating flexibility in command usage within GitHub CLI.

GitHub CLI Productivity Boost

Creating and Using Aliases in GitHub CLI

  • The speaker discusses the importance of using parameters with GitHub CLI subcommands, emphasizing the need to work with variables like $1 for effective command execution.
  • An example is provided where the basename command is used alongside pwd (print working directory) to simplify directory naming, illustrating how it returns just the last part of a path.
  • The speaker mentions an alias called gh, which simplifies the process of creating repositories by allowing users to execute commands quickly without typing them out fully.

Streamlining Repository Creation

  • A specific alias (ghdi) is introduced that combines multiple Git commands: initializing a repository, committing changes, creating a remote repo, and pushing branches. This streamlines workflow significantly.
  • The speaker demonstrates creating a new project named "playground2" and highlights how using the ghdi alias automates several steps in setting up a new repository.

Conclusion and Call to Action

  • The session wraps up with encouragement for viewers to utilize these aliases for enhanced productivity when using GitHub CLI. The speaker invites feedback through likes and subscriptions while promoting their website.
Video description

In this video we are going to play with the GitHub CLI. The GitHub CLI: https://cli.github.com Hub: https://github.com/github/hub Chris Wanstrath: https://github.com/defunkt The official GitHub CLI (gh) vs hub: https://github.com/cli/cli/blob/trunk/docs/gh-vs-hub.md My .dotfiles: https://github.com/agilesteel/.dotfiles https://github.com/agilesteel/.dotfiles/blob/master/stow/zsh/.zshrc https://github.com/agilesteel/.dotfiles/blob/master/stow/gh/.config/gh/config.yml This video is part of the following playlist: https://www.youtube.com/playlist?list=PLJGDHERh23x9DbNtd7AoGlvuuejBuxrPZ Table of Contents 00:00 intro 02:44 installation 04:00 gh help 05:25 gh completion 08:13 gh auth login 11:33 gh config 14:43 gh environment 16:07 gh issue view 18:36 gh repo clone 20:17 gh pr 21:30 a realistic scenario 22:54 gh repo create 27:07 gh issue create 29:09 gh fork 32:33 gh pr create 34:31 gh pr checkout 33:18 gbda 36:53 gh alias 38:19 gh alias --shell 40:10 ghdi Website: https://devinsideyou.com Patreon: https://patreon.devinsideyou.com Discord: https://discord.devinsideyou.com Telegram: https://telegram.devinsideyou.com Twitter: https://twitter.devinsideyou.com GitHub: https://github.devinsideyou.com LBRY: https://lbry.devinsideyou.com Install scripts: https://install.devinsideyou.com Giter8 templates: https://g8.devinsideyou.com Spotify concentration playlist: https://code.radio.devinsideyou.com Cheers and Happy Coding!