About GitHub CLI (Git & Dev Tools)
Learn About GitHub CLI (Git & Dev Tools) step by step with clear examples and exercises.
Why This Matters
GitHub CLI (Command Line Interface) is an indispensable tool for developers, as it enables direct interaction with GitHub's services from the terminal. By leveraging GitHub CLI, you can optimize your workflow and collaborate more efficiently with other developers. It simplifies managing repositories, issues, pull requests, and more, without requiring multiple applications to be switched between.
Prerequisites
Before delving into GitHub CLI, it's crucial to have a basic understanding of:
- Git: Familiarity with Git commands is essential as GitHub CLI extends the functionality provided by Git. If you're not already comfortable with Git commands like
git init,git add,git commit, andgit push, we recommend brushing up on those first. - Command Line Interface (CLI): A good understanding of your operating system's command line interface is necessary to use GitHub CLI effectively. You should be familiar with navigating directories, running commands, and handling file operations from the terminal.
- GitHub Account: To fully use GitHub CLI, you'll need a GitHub account. If you don't have one yet, create an account at github.com.
Core Concept
Installing GitHub CLI
To install GitHub CLI on your system, follow these steps:
- Linux and macOS: use the package manager for your operating system to install
gh. On Ubuntu or Debian, execute:
sudo apt-get install gh
On macOS with Homebrew, use:
brew install gh
- Windows: Download the MSI package from GitHub and run it to install
gh. You can find the latest release at github.com/cli/cli/releases. - Verify Installation: After installation, verify that
ghis available by running:
gh --version
This should display the version of GitHub CLI installed on your system.
Basic Usage
Once installed, you can use GitHub CLI to interact with various GitHub features directly from your terminal. Here are some basic commands:
- List Repositories:
gh repo list - Create a New Repository:
gh repo create --template=(Optional: Use templates to set up initial files and directories) - Clone a Repository:
gh repo clone /.git - View Issues and Pull Requests:
gh issue listorgh pr list - Create a New Issue:
gh issue create --body "" - Create a New Pull Request:
gh pr create --base --head
Authentication
To authenticate with GitHub CLI, you can use either SSH keys or personal access tokens (PATs). For more information on setting up authentication, refer to the official documentation.
Worked Example
Let's walk through creating a new repository, pushing some files, and opening an issue using GitHub CLI.
- Create a new directory for your project:
mkdir my-project && cd my-project
- Initialize a Git repository:
git init
- Add a file to the repository:
echo "Hello, World!" > README.md
- Initialize GitHub CLI and log in (using SSH keys):
gh auth login --local
Follow the prompts to authenticate with your GitHub account using your SSH key.
- Create a new repository on GitHub and link it to the local repository:
gh repo create my-project --public
- Add the remote origin URL for the GitHub repository:
git remote add origin git@github.com:<username>/my-project.git
- Commit and push the changes to the new repository:
git commit -m "Initial commit"
git push -u origin master
- Open a new issue in the GitHub repository:
gh issue create Bug Report --title "Issue Title" --body "Detailed description of the issue."
Common Mistakes
- Not logging in: Remember to log in with
gh auth login --local(or using PATs) before using GitHub CLI commands that require authentication, such as creating a new repository or opening an issue. - Incorrect syntax: Ensure you're using the correct syntax for each command and that you've provided all necessary arguments. For example, when creating a pull request, don't forget to specify the base and head branches.
- Not pushing changes: After making changes locally, remember to commit them with
git commitand push them to the remote repository withgit push. - Authentication issues: If you encounter authentication errors, double-check that your SSH keys or PATs are correctly set up and that you're using the correct command for logging in (e.g.,
gh auth login --local). - Not linking local repository to GitHub repository: Make sure to add the remote origin URL for the GitHub repository with
git remote add origin. - Incorrect repository name: When creating a new repository, ensure that you're using the correct repository name (
gh repo create) and that it matches the name of your local repository. - Not providing template when creating a new repository: If you want to set up initial files and directories for your new repository, use the
--template=option when creating a new repository withgh repo create. - Not using the correct command for viewing issues or pull requests: Use
gh issue listfor listing issues andgh pr listfor listing pull requests.
Practice Questions
- How can you list all the repositories in your GitHub account using GitHub CLI?
gh repo list
- What command would you use to create a new pull request from a feature branch (
feature/my-feature) to the master branch in a repository namedmy-project?
gh pr create --base master --head feature/my-feature
- How do you view the details of a specific issue in a GitHub repository using GitHub CLI?
gh issue vieworgh issue
- How can you update your personal access token (PAT) for GitHub CLI?
- Visit your GitHub account settings, navigate to Developer Settings > Personal Access Tokens, create a new token with the necessary permissions, and use it to authenticate with
gh auth login --with-token.
- How do you view the details of a specific pull request in a GitHub repository using GitHub CLI?
gh pr vieworgh pr
- How can you create a new repository with a README file and a .gitignore file using a template?
- Use the
--template=option when creating a new repository withgh repo create. For example, to use the "empty" template:
gh repo create my-project --public --template empty
This will create a new repository with an initial README file and .gitignore file.
FAQ
- Do I need to install Git to use GitHub CLI?
Yes, GitHub CLI builds upon the functionality provided by Git, so it's essential to have Git installed on your system before you can use GitHub CLI effectively.
- Can I use GitHub CLI on Windows?
Yes, GitHub CLI is available for Windows as well. You can download the MSI package from GitHub and run it to install gh.
- How do I upgrade my version of GitHub CLI?
To upgrade your version of GitHub CLI, you can use the package manager for your operating system or reinstall the latest release of gh from GitHub. On Ubuntu or Debian, run:
sudo apt-get update && sudo apt-get install gh
On macOS with Homebrew, use:
brew upgrade gh
- How do I set up SSH keys for GitHub CLI authentication?
To set up SSH keys for GitHub CLI authentication, follow the steps outlined in the official documentation.
- What is the difference between GitHub CLI and GitHub Desktop?
GitHub CLI is a command-line tool for interacting with GitHub, while GitHub Desktop is a graphical user interface (GUI) application for managing repositories on your local machine and pushing changes to GitHub. Both tools allow you to collaborate effectively with other developers on GitHub.
- How do I use GitHub CLI to view the status of my pull requests?
To view the status of a specific pull request, use gh pr view or gh pr . This command will display information about the pull request, including its status (e.g., open, merged, closed). If you have multiple pull requests open, you can list them all with gh pr list.
- How do I use GitHub CLI to create a new branch and switch to it?
To create a new branch and switch to it, use the following command:
git checkout -b <new_branch_name>
If you want to create a new branch based on an existing one, use:
git checkout -b <new_branch_name> <existing_branch>
- How do I use GitHub CLI to delete a branch?
To delete a local branch, use the following command:
git branch -d <branch_name>
To delete a remote branch, first switch to the corresponding local branch (if it exists), and then use:
git push origin --delete <branch_name>