Git and GitHub CLI: A Complete Workflow Guide
Master Branching, Commits, Pull Requests, and More
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
Open your repository in GitHub.
Click Code > Codespaces.
Click Continue Working in GitHub Codespace.
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: