#!/bin/bash # # Git Credential Setup for Linux Mint # Interactive setup script for managing Git credentials set -e # Colors for output RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' NC='\033[0m' # No Color # Function to print colored output print_header() { echo -e "${CYAN}================================${NC}" echo -e "${CYAN}$1${NC}" echo -e "${CYAN}================================${NC}" echo "" } print_step() { echo -e "${BLUE}➤ $1${NC}" } print_success() { echo -e "${GREEN}✅ $1${NC}" } print_warning() { echo -e "${YELLOW}⚠️ $1${NC}" } print_error() { echo -e "${RED}❌ $1${NC}" } print_info() { echo -e "${PURPLE}ℹ️ $1${NC}" } # Main script print_header "🔐 Git Credential Setup for Linux Mint" # Check if Git is installed if ! command -v git &> /dev/null; then print_error "Git is not installed" echo "" echo "Installing Git..." sudo apt update && sudo apt install -y git print_success "Git installed successfully" fi print_success "Git found: $(git --version)" # Check current Git config echo "" print_info "Current Git Configuration:" echo "User Name: $(git config --global user.name 2>/dev/null || echo 'Not set')" echo "User Email: $(git config --global user.email 2>/dev/null || echo 'Not set')" echo "Credential Helper: $(git config --global credential.helper 2>/dev/null || echo 'Not set')" echo "" # Check if we're in a Git repository if [ -d ".git" ]; then print_info "Current Repository:" echo "Remote URL: $(git remote get-url origin 2>/dev/null || echo 'No remote')" echo "" fi # Menu options echo "" print_info "Available Git Credential Methods:" echo "1. SSH Keys (Most Secure - Recommended)" echo "2. Git Credential Cache (Temporary)" echo "3. Git Credential Store (Persistent)" echo "4. GNOME Keyring (If available)" echo "5. Personal Access Token" echo "6. VS Code Integration" echo "" read -p "Choose your preferred method (1-6): " choice case $choice in 1) print_step "Setting up SSH Keys..." echo "" # Check if SSH key already exists ssh_key_path="$HOME/.ssh/id_ed25519" if [ -f "$ssh_key_path" ]; then print_success "SSH key already exists at: $ssh_key_path" else print_info "Generating new SSH key..." ssh-keygen -t ed25519 -C "$(git config --global user.email)" -f "$ssh_key_path" -N "" print_success "SSH key generated successfully" fi # Display public key echo "" print_info "SSH Public Key (add this to your Git server):" echo "----------------------------------------" cat "${ssh_key_path}.pub" echo "----------------------------------------" echo "" print_step "Next steps:" echo "1. Copy the public key above" echo "2. Add it to your Git server (git.d-popov.com)" echo "3. Test SSH connection: ssh -T git@git.d-popov.com" echo "" # Ask if user wants to change remote URL to SSH read -p "Change remote URL to SSH? (y/n): " change_remote if [[ $change_remote =~ ^[Yy]$ ]]; then current_url=$(git remote get-url origin) if [[ $current_url == https://* ]]; then ssh_url=$(echo $current_url | sed 's|https://git\.d-popov\.com/|git@git.d-popov.com:|g') git remote set-url origin "$ssh_url" print_success "Remote URL changed to SSH: $ssh_url" else print_warning "Remote URL is already using SSH or non-HTTPS protocol" fi fi ;; 2) print_step "Setting up Git Credential Cache..." echo "" # Set up credential cache git config --global credential.helper cache print_success "Git credential cache configured" echo "" print_info "This will cache credentials for 15 minutes by default" echo "To change timeout: git config --global credential.helper 'cache --timeout=3600'" ;; 3) print_step "Setting up Git Credential Store..." echo "" print_warning "This stores credentials in plain text!" read -p "Continue anyway? (y/n): " confirm if [[ ! $confirm =~ ^[Yy]$ ]]; then print_info "Setup cancelled" exit 0 fi git config --global credential.helper store print_success "Git credential store configured" echo "" print_warning "Credentials will be stored in plain text at: ~/.git-credentials" ;; 4) print_step "Setting up GNOME Keyring..." echo "" # Check if GNOME Keyring is available if command -v gnome-keyring-daemon &> /dev/null; then # Install GNOME Keyring credential helper sudo apt update && sudo apt install -y libsecret-1-0 libsecret-1-dev # Build and install git-credential-libsecret if not available if ! command -v git-credential-libsecret &> /dev/null; then print_info "Building git-credential-libsecret..." # Create temporary directory temp_dir=$(mktemp -d) cd "$temp_dir" # Download and build git clone https://github.com/git-ecosystem/git-credential-libsecret.git cd git-credential-libsecret sudo make install # Clean up cd / rm -rf "$temp_dir" fi git config --global credential.helper libsecret print_success "GNOME Keyring configured as credential helper" else print_error "GNOME Keyring is not available on this system" print_info "Falling back to credential cache..." git config --global credential.helper cache fi ;; 5) print_step "Setting up Personal Access Token..." echo "" print_info "For git.d-popov.com, you'll need to:" echo "1. Go to your Git server web interface" echo "2. Generate a Personal Access Token" echo "3. Use your username + token as password" echo "" git config --global credential.helper store print_success "Git configured to store credentials" echo "" print_info "Next time you push/pull:" echo "- Username: Your Git username" echo "- Password: [your personal access token]" echo "- Git will remember these credentials" ;; 6) print_step "Setting up VS Code Integration..." echo "" # Configure VS Code as credential helper git config --global credential.helper vscode print_success "VS Code configured as credential helper" echo "" print_info "Next time you use Git in VS Code:" echo "- VS Code will prompt for credentials" echo "- Choose 'Save' to store them securely" echo "- Credentials are stored per-repository" ;; *) print_error "Invalid choice. Please run the script again." exit 1 ;; esac echo "" print_success "Git credential setup complete!" echo "" print_info "Test your setup:" echo " git fetch" echo " git pull" echo " git push" echo "" print_info "For troubleshooting, run:" echo " ./test-git-credentials-linux.sh" echo "" # Create test script cat > test-git-credentials-linux.sh << 'EOF' #!/bin/bash # Git Credentials Test Script for Linux echo "🧪 Git Credentials Test" echo "======================" echo "" # Check Git config echo "📋 Git Configuration:" echo "User Name: $(git config --global user.name)" echo "User Email: $(git config --global user.email)" echo "Credential Helper: $(git config --global credential.helper)" echo "" # Test Git operations echo "🔍 Testing Git Operations:" echo -n "git fetch: " if git fetch --quiet 2>/dev/null; then echo "✅ Success" else echo "❌ Failed" fi echo -n "git status: " if git status --porcelain >/dev/null 2>&1; then echo "✅ Success" else echo "❌ Failed" fi echo "" echo "📝 Troubleshooting:" echo "• Clear credentials: git config --global --unset credential.helper" echo "• Test SSH: ssh -T git@git.d-popov.com" echo "• Check Git config: git config --list --show-origin" EOF chmod +x test-git-credentials-linux.sh print_success "Created test script: test-git-credentials-linux.sh"