Skip to content

var Git Command Guide

The git var command displays Git logical variables and configuration-dependent values, providing a way to access internal Git state and configuration information that may vary based on the current environment and configuration.

Terminal window
git var [-l | <variable>]
OptionDescription
-l, --listList all available variables
ParameterDescription
<variable>Specific variable to display
Git Variable Categories:
├── GIT_AUTHOR_IDENT: Author identity for commits
├── GIT_COMMITTER_IDENT: Committer identity for commits
├── GIT_EDITOR: Default editor for Git operations
├── GIT_PAGER: Default pager for output
├── GIT_SSH/GIT_SSH_COMMAND: SSH command for Git operations
└── GIT_ASKPASS: Password prompt helper
Identity Resolution Chain:
├── Explicit config: git config user.name/user.email
├── Environment: GIT_AUTHOR_NAME/GIT_AUTHOR_EMAIL
├── System: getpwuid()/gethostname()
├── Fallback: unknown/unknown@unknown
└── Resolution: git var GIT_AUTHOR_IDENT
Configuration Resolution Order:
├── GIT_* environment variables (highest priority)
├── Local repository config (.git/config)
├── Global user config (~/.gitconfig)
├── System config (/etc/gitconfig)
└── Built-in defaults (lowest priority)
Terminal window
# Show author identity for commits
git var GIT_AUTHOR_IDENT
# Show committer identity
git var GIT_COMMITTER_IDENT
# Display both identities
echo "Author: $(git var GIT_AUTHOR_IDENT)"
echo "Committer: $(git var GIT_COMMITTER_IDENT)"
Terminal window
# Show default editor
git var GIT_EDITOR
# Show default pager
git var GIT_PAGER
# Check if editor is configured
if [ "$(git var GIT_EDITOR)" = "none" ]; then
echo "No editor configured"
fi
Terminal window
# Show SSH command
git var GIT_SSH
# Show SSH command with arguments
git var GIT_SSH_COMMAND
# Check SSH configuration
ssh_cmd=$(git var GIT_SSH_COMMAND)
if [ -n "$ssh_cmd" ]; then
echo "Using custom SSH: $ssh_cmd"
else
echo "Using default SSH"
fi
Terminal window
# List all available variables
git var -l
# Filter for specific patterns
git var -l | grep -i author
git var -l | grep -i ssh
Terminal window
# Set identity for specific operations
export GIT_AUTHOR_NAME="John Doe"
export GIT_AUTHOR_EMAIL="john@example.com"
export GIT_COMMITTER_NAME="Jane Smith"
export GIT_COMMITTER_EMAIL="jane@example.com"
# Verify identity settings
git var GIT_AUTHOR_IDENT
git var GIT_COMMITTER_IDENT
# Use in commit operations
git commit -m "Commit with custom identity"
Terminal window
# Configure different identities per environment
setup_identity() {
local environment="$1"
case "$environment" in
"work")
export GIT_AUTHOR_NAME="John Doe"
export GIT_AUTHOR_EMAIL="john.doe@company.com"
export GIT_COMMITTER_NAME="John Doe"
export GIT_COMMITTER_EMAIL="john.doe@company.com"
;;
"personal")
export GIT_AUTHOR_NAME="John Doe"
export GIT_AUTHOR_EMAIL="john.doe@gmail.com"
export GIT_COMMITTER_NAME="John Doe"
export GIT_COMMITTER_EMAIL="john.doe@gmail.com"
;;
"open-source")
export GIT_AUTHOR_NAME="John Doe"
export GIT_AUTHOR_EMAIL="john.doe@opensource.org"
export GIT_COMMITTER_NAME="John Doe"
export GIT_COMMITTER_EMAIL="john.doe@opensource.org"
;;
esac
echo "Identity set for $environment environment"
git var GIT_AUTHOR_IDENT
}
# Usage
setup_identity "work"
Terminal window
# Configure SSH for different remotes
configure_ssh() {
local remote_name="$1"
local ssh_key="$2"
# Set SSH command for specific remote
git config "remote.$remote_name.sshCommand" "ssh -i $ssh_key"
# Verify SSH configuration
echo "SSH command for $remote_name:"
git var GIT_SSH_COMMAND
# Test connection
git ls-remote "$remote_name" >/dev/null 2>&1 && \
echo "SSH connection successful" || \
echo "SSH connection failed"
}
# Configure multiple SSH keys
setup_multiple_ssh() {
# GitHub personal
configure_ssh "origin" "~/.ssh/id_github_personal"
# GitHub work
configure_ssh "work" "~/.ssh/id_github_work"
# GitLab
configure_ssh "gitlab" "~/.ssh/id_gitlab"
}
# Usage
setup_multiple_ssh
Terminal window
# Configure editor with fallbacks
configure_editor() {
# Try preferred editors in order
for editor in "code --wait" "vim" "nano" "emacs"; do
if command -v "${editor%% *}" >/dev/null 2>&1; then
export GIT_EDITOR="$editor"
break
fi
done
echo "Git editor set to: $(git var GIT_EDITOR)"
}
# Configure pager with options
configure_pager() {
# Use less with options if available
if command -v less >/dev/null 2>&1; then
export GIT_PAGER="less -FRSX"
else
export GIT_PAGER="cat"
fi
echo "Git pager set to: $(git var GIT_PAGER)"
}
# Setup development environment
setup_dev_environment() {
configure_editor
configure_pager
# Configure askpass for password prompts
if command -v ssh-askpass >/dev/null 2>&1; then
export GIT_ASKPASS="ssh-askpass"
fi
echo "Development environment configured"
}
# Usage
setup_dev_environment
Terminal window
# Configure user identity globally
git config --global user.name "John Doe"
git config --global user.email "john.doe@example.com"
# Configure identity per repository
git config user.name "John Doe (Work)"
git config user.email "john.doe@company.com"
# Configure SSH command globally
git config --global core.sshCommand "ssh -o BatchMode=yes"
# Configure editor and pager
git config --global core.editor "code --wait"
git config --global core.pager "less -FRSX"
Terminal window
# Check if identity is properly configured
check_identity() {
local author_ident
author_ident=$(git var GIT_AUTHOR_IDENT 2>/dev/null)
if [[ "$author_ident" == *"unknown"* ]]; then
echo "WARNING: Git identity not properly configured"
echo "Run: git config --global user.name 'Your Name'"
echo "Run: git config --global user.email 'your.email@example.com'"
return 1
else
echo "Git identity: $author_ident"
return 0
fi
}
# Validate configuration before operations
validate_git_config() {
echo "Validating Git configuration..."
# Check identity
check_identity || return 1
# Check editor
if [ "$(git var GIT_EDITOR)" = "none" ]; then
echo "WARNING: No editor configured"
fi
# Check SSH
if ! git var GIT_SSH_COMMAND >/dev/null 2>&1; then
echo "INFO: Using default SSH command"
fi
echo "Git configuration validation complete"
}
# Usage
validate_git_config
Terminal window
# Manage environment variables for CI/CD
setup_ci_environment() {
echo "Setting up CI/CD environment variables..."
# Set CI-specific identity
export GIT_AUTHOR_NAME="CI Bot"
export GIT_AUTHOR_EMAIL="ci@company.com"
export GIT_COMMITTER_NAME="CI Bot"
export GIT_COMMITTER_EMAIL="ci@company.com"
# Configure SSH for CI
export GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"
# Disable interactive prompts
export GIT_ASKPASS="echo"
# Verify configuration
echo "CI Author: $(git var GIT_AUTHOR_IDENT)"
echo "CI Committer: $(git var GIT_COMMITTER_IDENT)"
}
# Setup different environments
setup_environment() {
local env_type="$1"
case "$env_type" in
"ci")
setup_ci_environment
;;
"development")
# Development environment
export GIT_EDITOR="code --wait"
export GIT_PAGER="less -FRSX"
;;
"server")
# Server environment (non-interactive)
export GIT_EDITOR="cat"
export GIT_ASKPASS="echo"
;;
esac
echo "Environment '$env_type' configured"
}
# Usage
setup_environment "ci"
#!/bin/bash
# Automated identity management for teams
team_identity_management() {
echo "=== Team Identity Management ==="
# Configure team identities
setup_team_identities() {
local team_members="alice bob charlie dave"
for member in $team_members; do
echo "Configuring identity for $member..."
# Create member-specific config
cat > ".git-team-$member" << EOF
GIT_AUTHOR_NAME=$member Doe
GIT_AUTHOR_EMAIL=$member@company.com
GIT_COMMITTER_NAME=$member Doe
GIT_COMMITTER_EMAIL=$member@company.com
EOF
echo "Identity file created: .git-team-$member"
done
}
# Switch team member identity
switch_team_member() {
local member="$1"
if [ -f ".git-team-$member" ]; then
# Load member identity
source ".git-team-$member"
echo "Switched to $member identity:"
git var GIT_AUTHOR_IDENT
else
echo "Team member '$member' not configured"
return 1
fi
}
# Pair programming setup
setup_pair_programming() {
local driver="$1"
local navigator="$2"
echo "Setting up pair programming: $driver + $navigator"
# Combine identities
export GIT_AUTHOR_NAME="$driver & $navigator"
export GIT_AUTHOR_EMAIL="pair@company.com"
export GIT_COMMITTER_NAME="$driver & $navigator"
export GIT_COMMITTER_EMAIL="pair@company.com"
echo "Pair identity configured:"
git var GIT_AUTHOR_IDENT
}
# Interactive team management
echo "Team Identity Management Options:"
echo "1. Setup team identities"
echo "2. Switch team member"
echo "3. Setup pair programming"
read -p "Select option (1-3): " option
case "$option" in
1) setup_team_identities ;;
2)
read -p "Team member: " member
switch_team_member "$member"
;;
3)
read -p "Driver: " driver
read -p "Navigator: " navigator
setup_pair_programming "$driver" "$navigator"
;;
esac
}
team_identity_management
Terminal window
# Git var in CI/CD pipelines
ci_var_integration() {
echo "=== CI/CD Var Integration ==="
# Setup CI identity
setup_ci_identity() {
echo "Setting up CI identity..."
# Use CI environment variables or defaults
export GIT_AUTHOR_NAME="${CI_COMMIT_AUTHOR_NAME:-CI Bot}"
export GIT_AUTHOR_EMAIL="${CI_COMMIT_AUTHOR_EMAIL:-ci@company.com}"
export GIT_COMMITTER_NAME="${CI_COMMITTER_NAME:-CI Bot}"
export GIT_COMMITTER_EMAIL="${CI_COMMITTER_EMAIL:-ci@company.com}"
echo "CI identity configured:"
git var GIT_AUTHOR_IDENT
}
# Validate CI environment
validate_ci_environment() {
echo "Validating CI environment..."
# Check required variables
local required_vars=("GIT_AUTHOR_NAME" "GIT_AUTHOR_EMAIL")
for var in "${required_vars[@]}"; do
if [ -z "${!var}" ]; then
echo "ERROR: $var not set"
return 1
fi
done
# Verify identity
author_ident=$(git var GIT_AUTHOR_IDENT)
if [[ "$author_ident" == *"unknown"* ]]; then
echo "ERROR: Invalid Git identity"
return 1
fi
echo "CI environment validated"
}
# Automated commit creation
create_ci_commit() {
local commit_message="$1"
echo "Creating CI commit..."
# Setup identity
setup_ci_identity
# Validate environment
validate_ci_environment || return 1
# Make changes
echo "$(date)" > ci-timestamp.txt
git add ci-timestamp.txt
# Create commit
git commit -m "$commit_message
CI Build: ${CI_BUILD_ID:-unknown}
Pipeline: ${CI_PIPELINE_ID:-unknown}
Job: ${CI_JOB_ID:-unknown}"
echo "CI commit created successfully"
}
# Run CI/CD steps
setup_ci_identity
validate_ci_environment
create_ci_commit "Automated CI commit"
}
# Usage in CI
ci_var_integration
Terminal window
# Repository analysis using git var
repository_analysis() {
echo "=== Repository Analysis with Var ==="
# Analyze contributor identities
analyze_contributors() {
echo "Analyzing repository contributors..."
# Get all commits with author info
git log --pretty=format:"%H %ae %an" | head -20 | while read -r commit email name; do
echo "Commit: $commit"
echo "Author: $name <$email>"
# Check if identity matches current config
current_ident=$(git var GIT_AUTHOR_IDENT)
if [[ "$current_ident" == *"$email"* ]]; then
echo " ✓ Matches current identity"
else
echo " ⚠ Different identity"
fi
echo
done
}
# Check identity consistency
check_identity_consistency() {
echo "Checking identity consistency..."
# Get unique authors
git log --pretty=format:"%ae|%an" | sort | uniq | while IFS='|' read -r email name; do
echo "Author: $name <$email>"
# Count commits by this author
commit_count=$(git log --author="$email" --oneline | wc -l)
echo " Commits: $commit_count"
# Check identity format
if [[ "$name" == *"unknown"* ]] || [[ "$email" == *"unknown"* ]]; then
echo " ⚠ Invalid identity detected"
else
echo " ✓ Valid identity"
fi
echo
done
}
# Generate identity report
generate_identity_report() {
echo "Generating identity report..."
cat > identity-report.md << EOF
# Repository Identity Report
Generated: $(date)
Repository: $(basename "$(pwd)")
## Current Identity Configuration
Author: $(git var GIT_AUTHOR_IDENT)
Committer: $(git var GIT_COMMITTER_IDENT)
## Identity Statistics
Total Commits: $(git rev-list --count HEAD)
Unique Authors: $(git log --pretty=format:"%ae" | sort | uniq | wc -l)
## Author Details
$(git log --pretty=format:"%ae|%an" | sort | uniq -c | sort -nr | while IFS='|' read -r count email name; do
echo "- $name <$email>: $count commits"
done)
## Recommendations
$(if [ "$(git var GIT_AUTHOR_IDENT)" = "$(git var GIT_COMMITTER_IDENT)" ]; then
echo "- ✓ Author and committer identities match"
else
echo "- ⚠ Author and committer identities differ"
fi)
$(unknown_count=$(git log --pretty=format:"%ae %an" | grep -c "unknown"); if [ "$unknown_count" -gt 0 ]; then
echo "- ⚠ $unknown_count commits have unknown author identity"
fi)
EOF
echo "Identity report generated: identity-report.md"
}
# Interactive analysis
echo "Repository Analysis Options:"
echo "1. Analyze contributors"
echo "2. Check identity consistency"
echo "3. Generate identity report"
read -p "Select option (1-3): " option
case "$option" in
1) analyze_contributors ;;
2) check_identity_consistency ;;
3) generate_identity_report ;;
esac
}
repository_analysis
Terminal window
# Diagnose identity problems
diagnose_identity_issues() {
echo "Diagnosing identity configuration issues..."
# Check current identity
author_ident=$(git var GIT_AUTHOR_IDENT 2>&1)
if [[ "$author_ident" == *"fatal"* ]]; then
echo "ERROR: Git repository not initialized"
return 1
fi
echo "Current author identity: $author_ident"
# Check for unknown values
if [[ "$author_ident" == *"unknown"* ]]; then
echo "WARNING: Identity contains unknown values"
# Suggest fixes
echo "To fix, run:"
echo " git config --global user.name 'Your Name'"
echo " git config --global user.email 'your.email@example.com'"
fi
# Check environment variables
echo "Environment variables:"
env | grep -E "^GIT_" | while read -r var; do
echo " $var"
done
# Check configuration
echo "Git configuration:"
git config --list | grep -E "^user\." | while read -r config; do
echo " $config"
done
}
# Fix identity issues
fix_identity_issues() {
echo "Attempting to fix identity issues..."
# Interactive identity setup
read -p "Enter your name: " user_name
read -p "Enter your email: " user_email
# Set global configuration
git config --global user.name "$user_name"
git config --global user.email "$user_email"
# Verify fix
echo "New identity: $(git var GIT_AUTHOR_IDENT)"
# Clear environment variables if they conflict
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
}
# Usage
diagnose_identity_issues
fix_identity_issues
Terminal window
# Resolve environment variable conflicts
resolve_env_conflicts() {
echo "Resolving environment variable conflicts..."
# Show current environment
echo "Current Git environment variables:"
env | grep -E "^GIT_" | sort
# Show configuration
echo -e "\nCurrent Git configuration:"
git config --list | grep -E "^user\." | sort
# Identify conflicts
echo -e "\nPotential conflicts:"
# Check for conflicts between env and config
config_name=$(git config user.name 2>/dev/null || echo "")
config_email=$(git config user.email 2>/dev/null || echo "")
env_name="${GIT_AUTHOR_NAME:-${GIT_COMMITTER_NAME}}"
env_email="${GIT_AUTHOR_EMAIL:-${GIT_COMMITTER_EMAIL}}"
if [ -n "$config_name" ] && [ -n "$env_name" ] && [ "$config_name" != "$env_name" ]; then
echo "⚠ Name conflict: config='$config_name' env='$env_name'"
fi
if [ -n "$config_email" ] && [ -n "$env_email" ] && [ "$config_email" != "$env_email" ]; then
echo "⚠ Email conflict: config='$config_email' env='$env_email'"
fi
# Suggest resolution
echo -e "\nResolution options:"
echo "1. Use environment variables (temporary)"
echo "2. Use Git configuration (persistent)"
echo "3. Clear environment variables"
read -p "Select resolution (1-3): " choice
case "$choice" in
1) echo "Using environment variables (current behavior)" ;;
2)
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
echo "Environment variables cleared, using Git config"
;;
3)
unset GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL
unset GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
echo "Environment variables cleared"
;;
esac
echo "Final identity: $(git var GIT_AUTHOR_IDENT)"
}
# Usage
resolve_env_conflicts
Terminal window
# Troubleshoot SSH configuration
troubleshoot_ssh_config() {
echo "Troubleshooting SSH configuration..."
# Check SSH command
ssh_cmd=$(git var GIT_SSH_COMMAND 2>/dev/null || echo "default")
echo "SSH command: $ssh_cmd"
# Test SSH connection
echo "Testing SSH connection..."
if git ls-remote origin >/dev/null 2>&1; then
echo "✓ SSH connection successful"
else
echo "✗ SSH connection failed"
# Suggest fixes
echo "Possible fixes:"
echo "1. Check SSH key: ssh -T git@github.com"
echo "2. Configure SSH key: git config core.sshCommand 'ssh -i ~/.ssh/id_rsa'"
echo "3. Check remote URL: git remote -v"
fi
}
# Troubleshoot editor configuration
troubleshoot_editor_config() {
echo "Troubleshooting editor configuration..."
# Check editor
editor=$(git var GIT_EDITOR)
echo "Configured editor: $editor"
# Test editor
if [ "$editor" != "none" ]; then
if command -v "${editor%% *}" >/dev/null 2>&1; then
echo "✓ Editor command available"
else
echo "✗ Editor command not found"
# Suggest alternatives
echo "Available editors:"
for alt_editor in vim nano emacs code; do
if command -v "$alt_editor" >/dev/null 2>&1; then
echo " - $alt_editor"
fi
done
echo "To set editor: git config --global core.editor 'vim'"
fi
else
echo "No editor configured"
echo "To set editor: git config --global core.editor 'vim'"
fi
}
# Usage
troubleshoot_ssh_config
troubleshoot_editor_config
#!/bin/bash
# Enterprise identity management system
enterprise_identity_system() {
echo "=== Enterprise Identity Management ==="
# Setup corporate identity
setup_corporate_identity() {
local employee_id="$1"
local department="$2"
echo "Setting up corporate identity for employee $employee_id..."
# Query employee database (simplified)
employee_name="John Doe"
employee_email="john.doe@company.com"
# Set corporate identity
export GIT_AUTHOR_NAME="$employee_name"
export GIT_AUTHOR_EMAIL="$employee_email"
export GIT_COMMITTER_NAME="$employee_name"
export GIT_COMMITTER_EMAIL="$employee_email"
# Set additional metadata
export GIT_AUTHOR_DEPARTMENT="$department"
export GIT_AUTHOR_EMPLOYEE_ID="$employee_id"
echo "Corporate identity configured:"
git var GIT_AUTHOR_IDENT
echo "Department: $department"
echo "Employee ID: $employee_id"
}
# Multi-project identity handling
setup_project_identity() {
local project="$1"
local role="$2"
echo "Setting up project-specific identity..."
# Base corporate identity
base_name=$(git config user.name)
base_email=$(git config user.email)
# Modify for project/role
export GIT_AUTHOR_NAME="$base_name ($project - $role)"
export GIT_AUTHOR_EMAIL="$base_email"
echo "Project identity configured:"
git var GIT_AUTHOR_IDENT
}
# Compliance and audit setup
setup_compliance_identity() {
echo "Setting up compliance identity tracking..."
# Enable detailed identity logging
export GIT_IDENTITY_LOG="/var/log/git-identity.log"
# Log all identity operations
log_identity() {
echo "$(date): $(git var GIT_AUTHOR_IDENT) - $*" >> "$GIT_IDENTITY_LOG"
}
# Enhanced identity with compliance info
export GIT_AUTHOR_COMPLIANCE_APPROVED="true"
export GIT_AUTHOR_AUDIT_TIMESTAMP="$(date +%s)"
echo "Compliance identity configured"
log_identity "Identity configured for compliance"
}
# Automated identity rotation
setup_identity_rotation() {
echo "Setting up automated identity rotation..."
# Create rotation script
cat > rotate-identity.sh << 'EOF'
#!/bin/bash
# Rotate Git identity based on time/project
CURRENT_HOUR=$(date +%H)
if [ "$CURRENT_HOUR" -ge 9 ] && [ "$CURRENT_HOUR" -lt 17 ]; then
# Work hours - use work identity
export GIT_AUTHOR_NAME="John Doe (Work)"
export GIT_AUTHOR_EMAIL="john.doe@company.com"
else
# Personal hours - use personal identity
export GIT_AUTHOR_NAME="John Doe"
export GIT_AUTHOR_EMAIL="john.doe@gmail.com"
fi
echo "Identity rotated: $(git var GIT_AUTHOR_IDENT)"
EOF
chmod +x rotate-identity.sh
# Setup cron for rotation
(crontab -l ; echo "*/30 * * * * $(pwd)/rotate-identity.sh") | crontab -
echo "Automated identity rotation configured"
}
# Interactive enterprise setup
echo "Enterprise Identity Management Options:"
echo "1. Setup corporate identity"
echo "2. Setup project identity"
echo "3. Setup compliance identity"
echo "4. Setup identity rotation"
read -p "Select option (1-4): " option
case "$option" in
1)
read -p "Employee ID: " emp_id
read -p "Department: " dept
setup_corporate_identity "$emp_id" "$dept"
;;
2)
read -p "Project: " project
read -p "Role: " role
setup_project_identity "$project" "$role"
;;
3) setup_compliance_identity ;;
4) setup_identity_rotation ;;
esac
}
enterprise_identity_system
Terminal window
# Development environment identity setup
dev_environment_setup() {
echo "=== Development Environment Identity Setup ==="
# Setup development identity
setup_dev_identity() {
echo "Setting up development identity..."
# Use hostname in identity for development
hostname=$(hostname)
export GIT_AUTHOR_NAME="Dev User"
export GIT_AUTHOR_EMAIL="dev@$hostname.local"
# Configure development tools
export GIT_EDITOR="${GIT_EDITOR:-vim}"
export GIT_PAGER="${GIT_PAGER:-less}"
# Setup SSH for development
if [ -f ~/.ssh/id_dev ]; then
export GIT_SSH_COMMAND="ssh -i ~/.ssh/id_dev"
fi
echo "Development identity configured:"
git var GIT_AUTHOR_IDENT
echo "Editor: $(git var GIT_EDITOR)"
echo "SSH: $(git var GIT_SSH_COMMAND 2>/dev/null || echo 'default')"
}
# Setup multiple development environments
setup_multi_env() {
echo "Setting up multiple development environments..."
environments=("local" "staging" "production")
for env in "${environments[@]}"; do
echo "Configuring $env environment..."
# Create environment-specific config
cat > ".git-env-$env" << EOF
# $env environment configuration
export GIT_AUTHOR_NAME="Dev User ($env)"
export GIT_AUTHOR_EMAIL="dev@$env.company.com"
export GIT_EDITOR="code --wait"
export GIT_PAGER="less -FRSX"
EOF
if [ "$env" = "production" ]; then
echo 'export GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=yes"' >> ".git-env-$env"
fi
echo "Environment config created: .git-env-$env"
done
}
# Switch development environment
switch_dev_env() {
local env="$1"
if [ -f ".git-env-$env" ]; then
source ".git-env-$env"
echo "Switched to $env environment:"
git var GIT_AUTHOR_IDENT
else
echo "Environment '$env' not configured"
return 1
fi
}
# Development workflow setup
setup_dev_workflow() {
echo "Setting up development workflow..."
# Configure git for development
git config core.editor "${GIT_EDITOR:-code --wait}"
git config core.pager "${GIT_PAGER:-less -FRSX}"
git config core.whitespace "trailing-space,space-before-tab"
git config core.autocrlf false
# Setup development hooks
cat > .git/hooks/pre-commit << 'EOF'
#!/bin/bash
# Development pre-commit checks
echo "Running development checks..."
# Check identity
author_ident=$(git var GIT_AUTHOR_IDENT)
if [[ "$author_ident" == *"unknown"* ]]; then
echo "ERROR: Git identity not configured"
exit 1
fi
echo "✓ Identity check passed: $author_ident"
EOF
chmod +x .git/hooks/pre-commit
echo "Development workflow configured"
}
# Interactive setup
echo "Development Environment Setup Options:"
echo "1. Setup development identity"
echo "2. Setup multiple environments"
echo "3. Switch environment"
echo "4. Setup development workflow"
read -p "Select option (1-4): " option
case "$option" in
1) setup_dev_identity ;;
2) setup_multi_env ;;
3)
read -p "Environment: " env
switch_dev_env "$env"
;;
4) setup_dev_workflow ;;
esac
}
dev_environment_setup
Terminal window
# CI/CD identity management with git var
ci_identity_management() {
echo "=== CI/CD Identity Management ==="
# Setup CI identity with metadata
setup_ci_identity() {
echo "Setting up CI identity with metadata..."
# Base CI identity
export GIT_AUTHOR_NAME="${CI_COMMIT_AUTHOR_NAME:-CI Bot}"
export GIT_AUTHOR_EMAIL="${CI_COMMIT_AUTHOR_EMAIL:-ci@company.com}"
export GIT_COMMITTER_NAME="${GIT_COMMITTER_NAME:-CI Bot}"
export GIT_COMMITTER_EMAIL="${GIT_COMMITTER_EMAIL:-ci@company.com}"
# Add CI metadata
export GIT_AUTHOR_CI_SYSTEM="${CI_SYSTEM:-unknown}"
export GIT_AUTHOR_CI_BUILD_ID="${CI_BUILD_ID:-unknown}"
export GIT_AUTHOR_CI_JOB_ID="${CI_JOB_ID:-unknown}"
export GIT_AUTHOR_CI_PIPELINE_ID="${CI_PIPELINE_ID:-unknown}"
echo "CI identity configured:"
git var GIT_AUTHOR_IDENT
echo "CI System: $GIT_AUTHOR_CI_SYSTEM"
echo "Build ID: $GIT_AUTHOR_CI_BUILD_ID"
}
# Validate CI identity
validate_ci_identity() {
echo "Validating CI identity..."
# Check required identity components
author_ident=$(git var GIT_AUTHOR_IDENT)
if [[ "$author_ident" == *"unknown"* ]]; then
echo "ERROR: CI identity contains unknown values"
return 1
fi
# Verify CI metadata
if [ -z "$GIT_AUTHOR_CI_BUILD_ID" ] || [ "$GIT_AUTHOR_CI_BUILD_ID" = "unknown" ]; then
echo "WARNING: CI build ID not set"
fi
echo "✓ CI identity validated: $author_ident"
}
# Create CI commit with metadata
create_ci_commit() {
local commit_message="$1"
echo "Creating CI commit with metadata..."
# Setup and validate identity
setup_ci_identity
validate_ci_identity || return 1
# Create commit with CI metadata
git commit -m "$commit_message
CI Metadata:
- System: $GIT_AUTHOR_CI_SYSTEM
- Build: $GIT_AUTHOR_CI_BUILD_ID
- Job: $GIT_AUTHOR_CI_JOB_ID
- Pipeline: $GIT_AUTHOR_CI_PIPELINE_ID
- Timestamp: $(date -u +%Y-%m-%dT%H:%M:%SZ)" 2>/dev/null || {
echo "No changes to commit"
return 0
}
echo "CI commit created successfully"
}
# Setup CI environment variables
setup_ci_environment() {
echo "Setting up CI environment variables..."
# Common CI systems
if [ -n "$GITHUB_ACTIONS" ]; then
export CI_SYSTEM="GitHub Actions"
export CI_BUILD_ID="$GITHUB_RUN_ID"
export CI_JOB_ID="$GITHUB_JOB"
export CI_PIPELINE_ID="$GITHUB_WORKFLOW"
elif [ -n "$GITLAB_CI" ]; then
export CI_SYSTEM="GitLab CI"
export CI_BUILD_ID="$CI_JOB_ID"
export CI_JOB_ID="$CI_JOB_ID"
export CI_PIPELINE_ID="$CI_PIPELINE_ID"
elif [ -n "$JENKINS_HOME" ]; then
export CI_SYSTEM="Jenkins"
export CI_BUILD_ID="$BUILD_NUMBER"
export CI_JOB_ID="$JOB_NAME"
export CI_PIPELINE_ID="$JOB_NAME"
else
export CI_SYSTEM="Unknown CI"
export CI_BUILD_ID="${CI_BUILD_ID:-unknown}"
export CI_JOB_ID="${CI_JOB_ID:-unknown}"
export CI_PIPELINE_ID="${CI_PIPELINE_ID:-unknown}"
fi
echo "CI environment configured for: $CI_SYSTEM"
}
# Run CI/CD identity setup
setup_ci_environment
setup_ci_identity
validate_ci_identity
create_ci_commit "Automated CI commit"
}
# Usage in CI
ci_identity_management