Files
mines/rin/miner/setup-git-credentials.ps1
Dobromir Popov b5bec14311 git cred
2025-09-08 09:12:55 +03:00

254 lines
10 KiB
PowerShell

# Git Credential Setup for Windows 11
# Comprehensive setup script for managing Git credentials
Write-Host ""
Write-Host "🔐 Git Credential Setup for Windows 11" -ForegroundColor Cyan
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
# Check if Git is installed
try {
$gitVersion = git --version 2>$null
Write-Host "✅ Git found: $gitVersion" -ForegroundColor Green
} catch {
Write-Host "❌ Error: Git is not installed" -ForegroundColor Red
Write-Host ""
Write-Host "Please install Git for Windows:" -ForegroundColor Yellow
Write-Host "Download: https://gitforwindows.org/" -ForegroundColor White
Write-Host "Or via winget: winget install --id Git.Git -e --source winget" -ForegroundColor White
Read-Host "Press Enter to exit"
exit 1
}
# Check current Git config
Write-Host "📋 Current Git Configuration:" -ForegroundColor Magenta
Write-Host "User Name: $(git config --global user.name)" -ForegroundColor White
Write-Host "User Email: $(git config --global user.email)" -ForegroundColor White
Write-Host "Credential Helper: $(git config --global credential.helper)" -ForegroundColor White
Write-Host ""
# Check if we're in a Git repository
if (Test-Path ".git") {
Write-Host "📁 Current Repository:" -ForegroundColor Magenta
$remoteUrl = git remote get-url origin 2>$null
Write-Host "Remote URL: $remoteUrl" -ForegroundColor White
Write-Host ""
}
Write-Host "🔧 Available Git Credential Methods:" -ForegroundColor Yellow
Write-Host "1. Git Credential Manager (Recommended)" -ForegroundColor White
Write-Host "2. GitHub CLI (for GitHub/GitLab)" -ForegroundColor White
Write-Host "3. SSH Keys (Most Secure)" -ForegroundColor White
Write-Host "4. Personal Access Token" -ForegroundColor White
Write-Host "5. VS Code Integration" -ForegroundColor White
Write-Host ""
$choice = Read-Host "Choose your preferred method (1-5)"
switch ($choice) {
"1" {
Write-Host ""
Write-Host "🔑 Setting up Git Credential Manager (GCM)..." -ForegroundColor Green
Write-Host ""
# Check if GCM is installed
$gcmInstalled = $false
try {
$gcmVersion = git-credential-manager --version 2>$null
$gcmInstalled = $true
Write-Host "✅ Git Credential Manager found: $gcmVersion" -ForegroundColor Green
} catch {
Write-Host "⚠️ Git Credential Manager not found" -ForegroundColor Yellow
}
if (-not $gcmInstalled) {
Write-Host ""
Write-Host "Installing Git Credential Manager..." -ForegroundColor Yellow
# Try winget first
try {
Write-Host "Trying winget..." -ForegroundColor Gray
winget install --id GitHub.GitHubDesktop -e --source winget --silent
Write-Host "✅ GitHub Desktop (includes GCM) installed" -ForegroundColor Green
} catch {
Write-Host "Winget failed, trying manual download..." -ForegroundColor Yellow
# Manual download
$gcmUrl = "https://github.com/git-ecosystem/git-credential-manager/releases/latest/download/gcm-win-x86-64.exe"
$installerPath = "$env:TEMP\gcm-installer.exe"
Write-Host "Downloading GCM installer..." -ForegroundColor Gray
Invoke-WebRequest -Uri $gcmUrl -OutFile $installerPath
Write-Host "Installing GCM..." -ForegroundColor Gray
Start-Process -FilePath $installerPath -ArgumentList "/VERYSILENT /NORESTART" -Wait
Remove-Item $installerPath -Force
Write-Host "✅ Git Credential Manager installed" -ForegroundColor Green
}
}
# Configure Git to use GCM
Write-Host ""
Write-Host "Configuring Git to use GCM..." -ForegroundColor Yellow
git config --global credential.helper manager
Write-Host "✅ GCM configured as credential helper" -ForegroundColor Green
Write-Host ""
Write-Host "Next time you push/pull, GCM will:" -ForegroundColor Cyan
Write-Host "1. Open a browser for authentication" -ForegroundColor White
Write-Host "2. Store credentials securely" -ForegroundColor White
Write-Host "3. Handle token refresh automatically" -ForegroundColor White
}
"2" {
Write-Host ""
Write-Host "🐙 Setting up GitHub CLI..." -ForegroundColor Green
Write-Host ""
# Check if GitHub CLI is installed
$ghInstalled = $false
try {
$ghVersion = gh --version 2>$null | Select-Object -First 1
$ghInstalled = $true
Write-Host "✅ GitHub CLI found: $ghVersion" -ForegroundColor Green
} catch {
Write-Host "⚠️ GitHub CLI not found" -ForegroundColor Yellow
}
if (-not $ghInstalled) {
Write-Host ""
Write-Host "Installing GitHub CLI..." -ForegroundColor Yellow
try {
winget install --id GitHub.cli -e --source winget
Write-Host "✅ GitHub CLI installed" -ForegroundColor Green
} catch {
Write-Host "Please install GitHub CLI manually:" -ForegroundColor Yellow
Write-Host "Download: https://cli.github.com/" -ForegroundColor White
Read-Host "Press Enter after installation"
}
}
# Authenticate with GitHub
Write-Host ""
Write-Host "Authenticating with GitHub..." -ForegroundColor Yellow
gh auth login
# Configure Git to use GitHub CLI
Write-Host ""
Write-Host "Configuring Git to use GitHub CLI..." -ForegroundColor Yellow
git config --global credential.helper gh
Write-Host "✅ GitHub CLI configured as credential helper" -ForegroundColor Green
}
"3" {
Write-Host ""
Write-Host "🔐 Setting up SSH Keys (Most Secure)..." -ForegroundColor Green
Write-Host ""
$sshKeyPath = "$env:USERPROFILE\.ssh\id_ed25519"
$sshKeyExists = Test-Path $sshKeyPath
if ($sshKeyExists) {
Write-Host "✅ SSH key already exists at: $sshKeyPath" -ForegroundColor Green
} else {
Write-Host "Generating new SSH key..." -ForegroundColor Yellow
ssh-keygen -t ed25519 -C "$(git config --global user.email)" -f $sshKeyPath -N '""'
Write-Host "✅ SSH key generated" -ForegroundColor Green
}
Write-Host ""
Write-Host "📋 SSH Public Key (add this to your Git server):" -ForegroundColor Yellow
Write-Host "----------------------------------------" -ForegroundColor Gray
Get-Content "$sshKeyPath.pub"
Write-Host "----------------------------------------" -ForegroundColor Gray
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Cyan
Write-Host "1. Copy the public key above" -ForegroundColor White
Write-Host "2. Add it to your Git server (git.d-popov.com)" -ForegroundColor White
Write-Host "3. Test SSH connection: ssh -T git@git.d-popov.com" -ForegroundColor White
# Change remote URL to SSH
$currentRemote = git remote get-url origin 2>$null
if ($currentRemote -and $currentRemote.StartsWith("https://")) {
Write-Host ""
$useSSH = Read-Host "Change remote URL to SSH? (y/n)"
if ($useSSH -eq "y") {
$sshUrl = $currentRemote -replace "https://git\.d-popov\.com/", "git@git.d-popov.com:"
$sshUrl = $sshUrl -replace "\.git$", ".git"
git remote set-url origin $sshUrl
Write-Host "✅ Remote URL changed to SSH: $sshUrl" -ForegroundColor Green
}
}
}
"4" {
Write-Host ""
Write-Host "🔑 Setting up Personal Access Token..." -ForegroundColor Green
Write-Host ""
Write-Host "For git.d-popov.com, you'll need to:" -ForegroundColor Yellow
Write-Host "1. Go to your Git server web interface" -ForegroundColor White
Write-Host "2. Generate a Personal Access Token" -ForegroundColor White
Write-Host "3. Use your username + token as password" -ForegroundColor White
Write-Host ""
# Configure Git to store credentials
git config --global credential.helper store
Write-Host "✅ Git configured to store credentials" -ForegroundColor Green
Write-Host ""
Write-Host "Next time you push/pull:" -ForegroundColor Cyan
Write-Host "- Username: $(git config --global user.name)" -ForegroundColor White
Write-Host "- Password: [your personal access token]" -ForegroundColor White
Write-Host "- Git will remember these credentials" -ForegroundColor White
}
"5" {
Write-Host ""
Write-Host "💻 Setting up VS Code Git Integration..." -ForegroundColor Green
Write-Host ""
Write-Host "VS Code has built-in Git credential management:" -ForegroundColor Yellow
Write-Host "1. VS Code stores credentials securely" -ForegroundColor White
Write-Host "2. No additional setup needed" -ForegroundColor White
Write-Host "3. Credentials are managed per-repository" -ForegroundColor White
Write-Host ""
# Configure VS Code as credential helper
git config --global credential.helper vscode
Write-Host "✅ VS Code configured as credential helper" -ForegroundColor Green
Write-Host ""
Write-Host "Next time you use Git in VS Code:" -ForegroundColor Cyan
Write-Host "- VS Code will prompt for credentials" -ForegroundColor White
Write-Host "- Choose 'Save' to store them" -ForegroundColor White
Write-Host "- Credentials are stored securely" -ForegroundColor White
}
default {
Write-Host "❌ Invalid choice. Please run the script again." -ForegroundColor Red
exit 1
}
}
Write-Host ""
Write-Host "🎉 Git credential setup complete!" -ForegroundColor Green
Write-Host ""
Write-Host "📖 Additional Resources:" -ForegroundColor Magenta
Write-Host "- Git Documentation: https://git-scm.com/doc" -ForegroundColor White
Write-Host "- GCM Documentation: https://aka.ms/gcm" -ForegroundColor White
Write-Host "- SSH Key Guide: https://docs.github.com/en/authentication/connecting-to-github-with-ssh" -ForegroundColor White
Write-Host ""
Write-Host "🧪 Test your setup:" -ForegroundColor Yellow
Write-Host "git fetch" -ForegroundColor White
Write-Host "git pull" -ForegroundColor White
Write-Host "git push" -ForegroundColor White
Write-Host ""
Read-Host "Press Enter to continue"