How to Easily Cache Your GitHub Credentials in Git

Harendra BarotHarendra Barot
2 min read

Document Overview

This document provides a step-by-step guide to installing and configuring the GitHub CLI (gh) tool, troubleshooting errors during the authentication process, and verifying successful GitHub repository operations.


1. Installing GitHub CLI

To install the GitHub CLI, follow these commands:

  1. Check if gh is installed:

     gh --version
    

    If gh is not found, proceed with installation.

  2. Install via apt:

     sudo apt install gh
    
    • This will download and set up the GitHub CLI package.

    • If prompted for a password, ensure you enter the correct sudo password.


2. Authenticating GitHub CLI

Once the GitHub CLI is installed, authenticate it with your GitHub account:

  1. Log in to GitHub CLI:

     gh auth login
    
  2. Follow the prompts:

    • Select GitHub.com as the account to log into.

    • Choose HTTPS as the protocol for Git operations.

    • Opt for authentication via GitHub credentials.

    • Select a method for authentication (e.g., Personal Access Token or Web Browser).


3. Common Issues and Fixes

Error: Missing Required Scope

  • When prompted to paste an authentication token, ensure it has the minimum required scopes:

    • repo

    • read:org

    • workflow

  • To generate a token:

    1. Visit GitHub Personal Access Tokens.

    2. Select the scopes listed above.

    3. Generate and copy the token.

Error: Web Browser Fails to Open

  • If the browser fails to launch, the error might mention missing executables (e.g., xdg-open or www-browser).

  • Solution:

    1. Copy the one-time code displayed (e.g., 9531-FCCA).

    2. Open the URL manually in your browser.

    3. Enter the one-time code to complete the authentication.


4. Verifying GitHub CLI Configuration

  • To confirm the configuration, check the authenticated user:

      gh auth status
    

    This should display the logged-in username and the connected protocol.


5. Git Repository Operations

Once authenticated, verify Git commands by performing the following steps:

  1. Add and commit a new file:

     touch demo.txt
     git add demo.txt
     git commit -m "Demo file added"
    
  2. Push changes to the repository:

     git push origin main
    
    • Verify the output for successful push confirmation.

6. Final Output

After following these steps:

  • Your GitHub CLI should be properly authenticated.

  • You should be able to push commits to your repository without errors.

  • Confirm that your repository reflects the pushed changes.


3
Subscribe to my newsletter

Read articles from Harendra Barot directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Harendra Barot
Harendra Barot

I'm an IT professional and business analyst, sharing my day-to-day troubleshooting challenges to help others gain practical experience while exploring the latest technology trends and DevOps practices. My goal is to create a space for exchanging ideas, discussing solutions, and staying updated with evolving tech practices.