Back to Shared Hosting Documentation

    Git Version Control

    Professional code management and deployment workflows.

    What is Git?

    Git is a distributed version control system that tracks changes in your code over time. It allows you to collaborate with others, revert to previous versions, and maintain different versions of your website simultaneously.

    Benefits of Using Git:

    Version History: Track every change with detailed logs
    Collaboration: Work with team members efficiently
    Branching: Test features without affecting production
    Easy Rollback: Undo changes quickly if something breaks

    Initial Git Configuration

    Check Git Availability (via SSH):

    ssh username@yourserver.com
    git --version
    # Should output: git version 2.x.x

    Set Your Identity:

    # Configure your name
    git config --global user.name "Your Name"
    
    # Configure your email
    git config --global user.email "your.email@example.com"
    
    # Verify configuration
    git config --list

    Optional Settings:

    # Set default branch name to main
    git config --global init.defaultBranch main
    
    # Enable colorful output
    git config --global color.ui auto
    
    # Set default editor
    git config --global core.editor nano

    Initialize Git on Existing Site

    # Navigate to your site
    cd ~/public_html
    
    # Initialize Git
    git init
    
    # Create .gitignore file
    nano .gitignore

    Recommended .gitignore for WordPress:

    # WordPress specific
    wp-config.php
    .htaccess
    
    # Cache
    /wp-content/cache/
    /wp-content/uploads/cache/
    
    # Backups
    *.sql
    *.tar.gz
    *.zip
    
    # Logs
    *.log
    error_log
    debug.log
    
    # OS files
    .DS_Store
    Thumbs.db
    
    # IDE files
    .idea/
    .vscode/
    *.swp
    
    # Node modules (if using)
    node_modules/

    Add and Commit Files:

    # Stage all files
    git add .
    
    # Create initial commit
    git commit -m "Initial commit: existing website files"
    
    # View commit history
    git log

    Clone from Remote Repository

    Set Up SSH Keys (Recommended):

    # Generate SSH key
    ssh-keygen -t ed25519 -C "your.email@example.com"
    
    # Display public key (copy to GitHub/GitLab)
    cat ~/.ssh/id_ed25519.pub

    Clone Repository:

    # Navigate to parent directory
    cd ~/
    
    # Clone repository
    git clone git@github.com:username/repo-name.git public_html

    Basic Git Workflow

    Daily Development Cycle:

    # 1. Check current status
    git status
    
    # 2. Pull latest changes (if working with others)
    git pull origin main
    
    # 3. Make your changes (edit files)
    
    # 4. Check what changed
    git status
    git diff
    
    # 5. Stage changes
    git add filename.php
    # Or stage all changes
    git add .
    
    # 6. Commit changes
    git commit -m "Descriptive message about what changed"
    
    # 7. Push to remote repository
    git push origin main

    Common Git Commands

    Viewing Information

    git status              # Check status
    git log                 # View commit history
    git log --oneline       # Condensed history
    git diff                # View changes not staged
    git diff --cached       # View staged changes

    Making Changes

    git add filename.php    # Stage specific file
    git add .               # Stage all changes
    git add -u              # Stage only modified files
    git reset filename.php  # Unstage file
    git commit -m "Message" # Commit staged changes

    Undoing Changes

    git checkout -- filename.php   # Discard changes
    git reset --hard               # Discard all uncommitted changes
    git reset --soft HEAD~1        # Undo last commit (keep changes)
    git reset --hard HEAD~1        # Undo last commit (discard changes)
    git revert commit-hash         # Revert a specific commit

    Working with Remote

    git remote -v                              # View remote repositories
    git remote add origin git@github.com:...   # Add remote
    git fetch origin                           # Fetch changes
    git pull origin main                       # Pull changes
    git push origin main                       # Push changes

    Branching Strategy

    Branches allow you to test new features without affecting production.

    # List branches
    git branch
    
    # Create new branch
    git branch feature-name
    
    # Switch to branch
    git checkout feature-name
    
    # Create and switch in one command
    git checkout -b feature-name
    
    # Merge branch into current branch
    git merge feature-name
    
    # Delete branch
    git branch -d feature-name

    Common Workflow:

    # Start new feature
    git checkout -b feature/new-contact-form
    
    # Work on feature, commit changes
    git add .
    git commit -m "Add new contact form"
    
    # Push feature branch
    git push origin feature/new-contact-form
    
    # Switch back to main
    git checkout main
    
    # Merge feature (after testing)
    git merge feature/new-contact-form
    
    # Push to production
    git push origin main
    
    # Delete feature branch
    git branch -d feature/new-contact-form

    Deployment Workflows

    Simple Manual Deployment:

    # On your server via SSH
    cd ~/public_html
    
    # Pull latest changes
    git pull origin main

    Automated Deployment Script (deploy.sh):

    #!/bin/bash
    cd ~/public_html
    
    echo "Pulling latest changes..."
    git pull origin main
    
    echo "Setting permissions..."
    find . -type f -exec chmod 644 {} \;
    find . -type d -exec chmod 755 {} \;
    
    echo "Deployment complete!"
    date

    Make Script Executable:

    chmod +x deploy.sh
    ./deploy.sh

    WordPress-Specific Git Setup

    What to Track:

    ✓ Track:

    • • Custom themes
    • • Custom plugins
    • • Must-use plugins (mu-plugins)
    • • Custom code

    ✗ Don't Track:

    • • Core WordPress files
    • • Third-party plugins
    • • Uploads folder
    • • wp-config.php

    Handling Merge Conflicts

    When conflicts occur, Git marks the conflicting sections in your files:

    <<<<<<< HEAD
    Your local changes
    =======
    Remote changes
    >>>>>>> branch-name

    Resolution Steps:

    1. View conflicted files with git status
    2. Open conflicted file in editor
    3. Find and resolve conflict markers
    4. Remove the conflict markers
    5. Save the file
    6. Stage resolved file: git add filename
    7. Complete merge: git commit

    Git Best Practices

    • • Write clear, descriptive commit messages
    • • Commit small, focused changes
    • • Pull before pushing
    • • Never commit sensitive data
    • • Use branches for features
    • • Review changes before committing
    • • Keep .gitignore updated
    • • Backup before major operations