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