Version Control Fundamentals
Traditional version control often involves manually saving multiple file versions, leading to inefficiencies:
project_draft_v1.txt
project_draft_v2.txt
project_final_v1.txt
project_final_v2.txt
Key liimtations of manual versioning:
- File duplication for each version
- Collaboration challenges
- Risk of permanent data loss
Modern version control systems like Git address these issues through:
- Local client for version tracking
- Optional remote server for backup (e.g., GitHub)
Git Architecture
Git operates as a distributed version control system where:
- Each client maintains complete version history
- Remote servers (like GitHub) provide centralized backup
- Key distinction from centralized systems: full history exists locally
Core Git Workfolw
Initialize a repository:
$ cd /project/directory
$ git init
Basic version control operations:
$ git status # Check current state
$ git add . # Stage all changes
$ git commit -m "Initial version" # Create version snapshot
$ git log # View version history
Working Directory and Repository
Git manages files across three states:
- Working directory: Active development area
- Staging area: Prepared changes
- Repository: Committed versions
Example workflow:
$ touch new_file.py # Create new file
$ git status # Shows untracked file
$ git add new_file.py # Stage file
$ git commit -m "Add new feature" # Commit changes
Branch Management
Effective branching strategy:
$ git branch feature-x # Create new branch
$ git checkout feature-x # Switch to branch
# Develop features...
$ git add .
$ git commit -m "Implement feature X"
$ git checkout main # Return to main branch
$ git merge feature-x # Integrate changes
GitHub Itnegration
Remote repository operations:
$ git remote add origin https://github.com/user/repo.git
$ git push -u origin main # First push
$ git clone https://github.com/user/repo.git # Get existing project
Collaboration workflow:
- Fetch latest changes:
git pull origin main - Resolve any merge conflicts
- Push your changes:
git push origin main
Advanced Techniques
Temporary work storage:
$ git stash # Save uncommitted changes
$ git stash pop # Restore changes
Tagging releases:
$ git tag -a v1.0 -m "Release version"
$ git push origin --tags
Configuration options:
- System-wide:
/etc/gitconfig - User-specific:
~/.gitconfig - Project-specific:
.git/config