GitHub’s command-line interface (CLI) lets you control repositories, manage files, and interact with GitHub’s API directly from Linux—faster than web browsing and far more powerful than the browser’s Git UI. It replaces local Git workflows with direct server commands, supports API integrations, and handles authentication securely without passwords. With the right setup, you’ll push/pull files instantly, automate workflows, and even deploy code directly to GitHub-hosted services. Here’s how to do it properly.
What You'll Need
- Linux system (Ubuntu, Debian, Fedora, Arch, or any distro with Bash and Git installed). Windows/macOS users can’t use this CLI natively.
- A GitHub account (free or paid) with repository access.
- Administrator privileges (some install steps require sudo).
- A stable internet connection for downloading the CLI.
- Optional: SSH keys for secure authentication (recommended).
Step-by-Step
- Install GitHub CLI
- Download the latest version from GitHub’s official release page:
https://github.com/cli/cli/releases(Look for the latest "GitHub CLI" release under "Assets" and download the appropriate version for your Linux distro.) - Transfer the file to your home directory and make it executable:
curl -sSfL https://github.com/cli/cli/releases/latest/download/github-cli-linux-amd64.tar.gz | tar -xz -C ~/.local && mv ~/.local/src/github /usr/local/bin/github && rm ~/.local/src/github-cli-linux-amd64.tar.gz(Replace the URL with the direct download link from the release page.) - Verify installation:
github --versionShould return the installed version (e.g.,github-cli/2.35.0).
- Download the latest version from GitHub’s official release page:
- Authenticate with GitHub
- Run the authentication command:
github auth loginFollow the prompts to log in via browser (GitHub will open a login page). For security, accept the defaults for token expiration. - If you prefer SSH keys (recommended for automation), generate a new SSH key pair:
ssh-keygen -t ed25519 -C "your_email@example.com"Copy the public key to GitHub:cat ~/.ssh/id_ed25519.pub | pbcopy(Mac/Linux) or manually copy the output. Then go to GitHub > Settings > SSH and GPG keys > New SSH key and paste it.
- Run the authentication command:
- Test Basic Commands
- List your repositories:
github repo listShows your private and public repos with brief details. - Clone a repository (as if you were using Git CLI):
github repo clone username/repo-nameCreates a local folder matching the repo name. - Push changes to a repo:
cd /path/to/local/repo && git add . && git commit -m "Your message" && github repo push -m "Your message"(This combines Git and CLI steps—GitHub CLI handles the remote push.)
- List your repositories:
- Work with Files Directly
- Create/update a file in a repo:
echo "Hello, GitHub CLI!" > README.md && github repo pushAdds the file and updates the repo. - View file contents:
github repo view --path README.md - Search repo files:
github repo search --query "hello" --path "src/*"
- Create/update a file in a repo:
- Advanced: API Calls
- Query repo data via API:
github repo get username/repo-nameReturns JSON with repo details (requires authentication). - Create a new issue:
github repo create-issue username/repo-name -t "Bug: Feature X fails" -b "Describe the issue here."
- Query repo data via API:
If That Did Not Work
- **Authentication failed?** Re-rungithub auth login or ensure your SSH key is added to GitHub. If using a token, check its validity with github auth status.
- **Commands list missing repos?**
Run github repo list --all to force a sync. Clear the cache with rm -rf ~/.cache/github and retry.
- **Permissions denied?**
Verify your GitHub account has access to the repo. For SSH, ensure the key is added to ssh-agent:
eval "$(ssh-agent -s)" && ssh-add ~/.ssh/id_ed25519
Common Mistakes
- **Using the wrong token/password:** Never hardcode credentials in scripts. GitHub CLI never prompts for a password—use SSH or tokens instead. - **Forgetting `gh` alias:** After installation, typegh (without quotes) to check if it works. Some distros require adding it to PATH:
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc
- **Case-sensitive paths:** Linux is case-sensitive. Ensure repo names match exactly (e.g., RepoName, not repoName).
- **Outdated CLI:** Pin to the latest version:
github version update
Tips
- **Automate workflows:** Use shell scripts to trigger GitHub CLI commands. Example:
#!/bin/bash
github repo create-issue owner/repo -t "Auto: Fix bug" -b "This was triggered by $USER at $(date)."
- **Debug mode:** Run CLI with flags to see errors:
github repo clone --verbose username/repo
- **Backup tokens:** If you lose access, generate a new token via github auth app create (but note the old token’s permissions).
- **Portability:** The CLI works on any Linux distro, but some features (like browser-based auth) depend on modern browsers.
Troubleshooting
- **No internet connection?** Restart the CLI:pkill -f github && git clone https://github.com/username/repo.git (temporarily fall back to Git).
- **Permission denied on push?**
Check remote URL:
git remote -v
Ensure it points to HTTPS or SSH (but not a local file:// path). Fix with:
git remote set-url origin git@github.com:username/repo.git
- **CLI crashes:** Reinstall with:
sudo rm -rf /usr/local/bin/github && curl -sSfL https://github.com/cli/cli/releases/latest/download/github-cli-linux-amd64.tar.gz | tar -xz -C ~/.local && mv ~/.local/src/github /usr/local/bin/github
- **SSH keys not working?**
Add the key to GitHub’s SSH keys list (Settings > SSH and GPG keys) and test locally:
ssh -T git@github.com
Should return: Hi username! You've successfully authenticated...
Frequently Asked Questions
1. Can I use GitHub CLI on Windows or macOS?
GitHub CLI is designed for Linux. On Windows/macOS, use the [GitHub Desktop app](https://desktop.github.com/) or browser-based Git tools.2. What if I don’t want to use SSH keys?
Use tokens generated viagithub auth app create (scoped to specific repos). Tokens are safer than passwords for CLI use.
3. How do I switch between accounts?
Log out and in:github auth logout && github auth login
This resets the CLI to your current account.
4. Can I integrate GitHub CLI with CI/CD pipelines?
Yes! Use CLI commands in GitHub Actions workflows or external scripts. Example:
steps:
- name: Commit to repo
run: |
echo "New task added" > task.md
github repo push --path task.md --message "Automated task"
5. Does GitHub CLI support private repos without SSH?
Yes, but only if you’ve enabled "Personal Access Tokens" with repo permissions. SSH is the most secure method.6. Why does my token keep expiring?
Tokens expire after 1 hour by default. Extend them via:github auth app create --scope repo,admin:public_key
(Replace scopes with your needs; admin:public_key extends expiration to 30 days.)