# Git and GitHub CLI: A Complete Workflow Guide

## 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

```bash
gh auth logout -h github.com
```

This logs you out of GitHub via GitHub CLI.

### 🔍 Remove Git Credentials and Configurations

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

### 🔐 Clear SSH Keys (Optional)

```bash
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

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

### ❌ Reject Any Remaining Credentials

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

If that doesn't work:

```bash
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

```bash
gh auth login
```

Once configured, you can see your Git global settings:

```bash
git config --list --global
```

---

## 2\. Initializing a Git Repository

### ⚖️ Git Initialization (`git init`)

```bash
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:

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

### ✅ Staging (`git add <file>`)

```bash
git add file.txt
```

Git updates:

```bash
.git/index  # The Staging Area
```

List staged files:

```bash
git ls-files --stage
```

### 📝 Committing (`git commit -m "message"`)

```bash
git commit -m "Initial commit"
```

Git writes data into:

```bash
.git/objects/
├── info/
├── pack/
├── 9f/2345abc...  # Blob: actual file content
├── 7a/6bcd123...  # Tree: directory structure
├── fc/67de89...   # Commit object with metadata
```

Inspect commit object:

```bash
git cat-file -p <commit-hash>
```

### 🔗 Connecting Remote (`git remote add origin <url>`)

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

This updates:

```bash
.git/config  # Stores remote origin URL
```

### ↗️ Pushing Changes (`git push`)

```bash
git push origin main
```

Push sends commits to the remote repository and updates:

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

If you're interested in understanding Git internals, refer to the [Git Internals Documentation page](https://git-scm.com/book/en/v2/Git-Internals-Plumbing-and-Porcelain).

### ⏪ Undoing Commits

Undo the last commit (keep changes unstaged):

```bash
git reset --soft HEAD~1
```

Undo the last commit (discard changes):

```bash
git reset --hard HEAD~1
```

### 🧩 Resolving Merge Conflicts

```bash
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

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

### 🛡️ Storing Credentials

* **macOS**:
    

```bash
git config --global credential.helper osxkeychain
```

* **Linux**:
    

```bash
git config --global credential.helper cache
```

### 📝 Changing Default Editor

* Use Nano:
    

```bash
git config --global core.editor nano
```

* Use VS Code:
    

```bash
git config --global core.editor "code --wait"
```

---

## 4\. Working in GitHub Codespaces

1. Open your repository in GitHub.
    
2. Click **Code &gt; 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

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

### 🔗 Add SSH Key to GitHub

```bash
cat ~/.ssh/id_ed25519.pub
```

Then add it to GitHub: **Settings &gt; SSH and GPG keys**.

### ✅ Test SSH Connection

```bash
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:

```bash
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

```bash
echo $SHELL
```

* If it contains `zsh`: edit `~/.zshrc`
    
* If it contains `bash`: edit `~/.bashrc`
    

### 📌 Step 2: Add the Function

**For macOS (uses** `open`)

```bash
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`)

```bash
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`)

```bash
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

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

### 🚀 Try It Out!

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

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

---

🌍 Resources:

* [Git Documentation](https://git-scm.com/docs)
    
* [GitHub Repository](https://github.com/techstackspace/developers-setting/tree/01-homebrew-installation-and-usage-guide?tab=readme-ov-file)
    
* [Shared Arc Folders](https://arc.net/folder/FBC776BF-BC29-48F4-B019-E3B969C326A3)
