Git and GitHub CLI: A Complete Workflow Guide

Git and GitHub CLI: A Complete Workflow Guide

Master Branching, Commits, Pull Requests, and More

Β·

5 min read

Introduction

Learn how to fully clean up Git and GitHub CLI before reinstalling. This complete guide covers credential removal, SSH key management, Git initialization, Codespaces setup, Git configuration, and advanced terminal shortcuts.

Cleaning Up Git and GitHub CLI Before Reinstallation

Before reinstalling Git or GitHub CLI (gh), it’s a good practice to ensure all previous configurations and credentials are fully removed. Here's how to do a complete cleanup:

πŸ“‰ Logout from GitHub CLI

gh auth logout -h github.com

This logs you out of GitHub via GitHub CLI.

πŸ” Remove Git Credentials and Configurations

rm ~/.git-credentials           # Deletes saved HTTPS credentials
rm ~/.gitconfig                # Removes global Git configuration file

πŸ” Clear SSH Keys (Optional)

rm -rf ~/.ssh/                 # Removes all SSH keys and configurations

Note: Only do this if you're sure you want to delete all SSH keys.

πŸ”’ Clear GitHub CLI Config

rm -rf ~/.config/gh            # Deletes GitHub CLI config directory

❌ Reject Any Remaining Credentials

echo "url=https://github.com" | git credential reject

If that doesn't work:

git credential reject https://github.com

1. GitHub CLI (gh) and Authentication

The gh command-line tool allows you to interact with GitHub directly from your terminal. It simplifies repo management, pull requests, issues, and CI/CD workflows.

πŸ” Authenticate GitHub CLI

gh auth login

Once configured, you can see your Git global settings:

git config --list --global

2. Initializing a Git Repository

βš–οΈ Git Initialization (git init)

git init
ls -la            # Lists all files including hidden ones
ls -la .git        # View the .git directory

πŸ—‚οΈ Understanding the .git Folder

  • HEAD: Points to the current branch.

  • config: Stores repository settings.

  • refs/: Contains references to commits (branches and tags).

  • objects/: Stores commit objects.

  • hooks/: Scripts that trigger on Git events.

View contents:

cat .git/HEAD
cat .git/config
ls -la .git/refs/heads/   # See available branches
ls -la .git/objects/

βœ… Staging (git add <file>)

git add file.txt

Git updates:

.git/index  # The Staging Area

List staged files:

git ls-files --stage

πŸ“ Committing (git commit -m "message")

git commit -m "Initial commit"

Git writes data into:

.git/objects/
β”œβ”€β”€ info/
β”œβ”€β”€ pack/
β”œβ”€β”€ 9f/2345abc...  # Blob: actual file content
β”œβ”€β”€ 7a/6bcd123...  # Tree: directory structure
β”œβ”€β”€ fc/67de89...   # Commit object with metadata

Inspect commit object:

git cat-file -p <commit-hash>

πŸ”— Connecting Remote (git remote add origin <url>)

git remote add origin https://github.com/your/repo.git

This updates:

.git/config  # Stores remote origin URL

↗️ Pushing Changes (git push)

git push origin main

Push sends commits to the remote repository and updates:

.git/refs/remotes/  # Tracks state of remotes

If you're interested in understanding Git internals, refer to the Git Internals Documentation page.

βͺ Undoing Commits

Undo the last commit (keep changes unstaged):

git reset --soft HEAD~1

Undo the last commit (discard changes):

git reset --hard HEAD~1

🧩 Resolving Merge Conflicts

git status                  # Check conflicts
nano <conflicted_file>      # Edit the file to resolve conflicts
git add <conflicted_file>
git commit -m "Resolved merge conflict"

3. Git Configuration and Credential Management

πŸ” Checking Git Configuration

git config --list --local     # Show local repository settings
git config --global --list    # Show global settings

πŸ›‘οΈ Storing Credentials

  • macOS:
git config --global credential.helper osxkeychain
  • Linux:
git config --global credential.helper cache

πŸ“ Changing Default Editor

  • Use Nano:
git config --global core.editor nano
  • Use VS Code:
git config --global core.editor "code --wait"

4. Working in GitHub Codespaces

  1. Open your repository in GitHub.

  2. Click Code > Codespaces.

  3. Click Continue Working in GitHub Codespace.

  4. Select an instance type based on your project needs:

    • Basic: Suitable for lightweight projects.

    • Standard: Recommended for most web and mobile development.

    • Performance: For complex builds or heavy computation.


5. GitHub SSH Key Setup

πŸ”‘ Generate a New SSH Key

ssh-keygen -t ed25519 -C "your_email@example.com"

πŸ”— Add SSH Key to GitHub

cat ~/.ssh/id_ed25519.pub

Then add it to GitHub: Settings > SSH and GPG keys.

βœ… Test SSH Connection

ssh -T git@github.com

6. Bonus: Configure Terminal for Quick Search and URL Opening

Wouldn’t it be great if you could open websites or search Google directly from the terminal using a simple command like:

oh "github.com"
# (Opens https://github.com in your browser)

oh "how to install python"
# (Searches Google for "how to install python")

πŸ“Œ Step 1: Check Your Default Shell

echo $SHELL
  • If it contains zsh: edit ~/.zshrc

  • If it contains bash: edit ~/.bashrc

πŸ“Œ Step 2: Add the Function

For macOS (uses open)

oh() {
  local input="$*"
  if [[ "$input" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    open "https://$input"
  else
    open "https://www.google.com/search?q=$(echo "$input" | tr ' ' '+')"
  fi
}

For Linux (uses xdg-open)

oh() {
  local input="$*"
  if [[ "$input" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    xdg-open "https://$input"
  else
    xdg-open "https://www.google.com/search?q=$(echo "$input" | tr ' ' '+')"
  fi
}

For Windows (uses start)

oh() {
  local input="$*"
  if [[ "$input" =~ ^[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$ ]]; then
    powershell -c "Start-Process 'https://$input'"
  else
    powershell -c "Start-Process 'https://www.google.com/search?q=$(echo \"$input\" | tr ' ' '+')'"
  fi
}

βœ… Step 3: Apply the Changes

source ~/.zshrc  # Or ~/.bashrc depending on your shell

πŸš€ Try It Out!

oh github.com
# Opens https://github.com

oh "best terminal commands for Mac"
# Opens Google search

🌍 Resources:

Β