4. Version Control with Git#
4.1. Introduction#
4.1.1. What is Git?#
4.1.2. What is GitHub?#
4.2. Learning Objectives#
4.3. Setting Up GitHub Account#
4.3.1. Creating Your GitHub Account#
4.4. Installing Git#
4.4.1. Installation Instructions#
4.4.2. Verifying Installation#
# Check Git version
$ git --version
# Check installation location
$ which git # macOS/Linux
$ where git # Windows
4.5. Configuring Git#
4.5.1. Essential Configuration#
# Replace with your actual name and GitHub email
$ git config --global user.name "Your Full Name"
$ git config --global user.email "your.github@email.com"
4.5.2. Verification#
# View all global configuration
$ git config --global --list
# Check specific values
$ git config --global user.name
$ git config --global user.email
4.6. Understanding Git Concepts#
4.6.1. Core Git Concepts#
4.6.2. The Git Workflow#
4.7. Essential Git Commands#
4.7.1. Starting a New Project#
# Navigate to your project folder
$ cd ~/my-geo-project
# Initialize Git repository
$ git init
# Check repository status
$ git status
# Clone a repository from GitHub
$ git clone https://github.com/<your-username>/intro-gispro.git
4.7.2. Tracking Changes#
# Add specific files
$ git add analysis.py
# Add all files in current directory
$ git add .
# See status of all files
$ git status
# See detailed changes
$ git diff
# Commit with inline message
$ git commit -m "Add rainfall analysis function"
# Commit all tracked files (skip staging)
$ git commit -am "Update visualization parameters"
4.7.3. Writing Good Commit Messages#
$ git commit -m "Add NDVI calculation for Landsat 8 imagery"
$ git commit -m "Fix coordinate transformation bug in UTM conversion"
$ git commit -m "Update flood mapping algorithm to handle edge cases"
4.7.4. Working with GitHub#
# Add remote repository
$ git remote add origin https://github.com/<your-username>/<repo-name>.git
# Push to GitHub (first time)
$ git push -u origin main
# Push subsequent changes
$ git push
# Pull changes from GitHub
$ git pull
4.7.5. Basic Branching#
# Create new branch for experimental feature
$ git checkout -b satellite-analysis
# List all branches
$ git branch
# Switch back to main branch
$ git checkout main
# Merge feature branch into main
$ git merge satellite-analysis
# Delete merged branch
$ git branch -d satellite-analysis
4.7.6. Viewing Project History#
# View commit history
$ git log
# Compact one-line format
$ git log --oneline
# View specific file history
$ git log -- data_processing.py
4.7.7. Undoing Changes#
# Undo last commit but keep changes
$ git reset --soft HEAD~1
4.8. Using GitHub#
4.8.1. Creating Repositories on GitHub#
4.8.2. Basic Collaboration#
4.8.3. Repository Management#
4.9. Integration with VS Code#
4.10. Best Practices for Geospatial Projects#
4.10.1. Repository Structure#
4.10.2. What to Track vs. Ignore#
4.10.3. Commit Message Conventions#
# Format: <type>: <description>
$ git commit -m "feat: add NDVI calculation for Sentinel-2"
$ git commit -m "fix: handle NoData values in elevation processing"
$ git commit -m "docs: update README with installation instructions"