Understanding Git Architecture
Git operates as a distributed version control system, where each developer maintains a complete local repository containing all branches and commit history. This differs from centralized systems that rely on a single server.
Core Git concepts include:
- Distributed Model: Every contributor possesses a full repository copy
- Data Integrity: SHA-1 hashing ensures content protection
- Efficient Networking: Only differential data transfers during operations
- Branch Management: Lightweight branching enables isolated development
- Staging Area: Selective commit preparation through indexed changes
Local Repository Operations
Initial Configuration
Before using Git, configure user identity:
git config --global user.name "developer-name"
git config --global user.email "developer@domain.com"
Verify configuration:
git config --list
Repository Initialization
Create a working directory and initialize Git tracking:
mkdir project-folder
cd project-folder
git init
This creates a hidden .git directory containing repository metadata.
Version Creation Workflow
Modify files in the working directory, then stage and commit changes:
# Stage all modifications
git add .
# Stage specific file types
git add *.js
# Stage individual files
git add script.js
# Commit staged changes with message
git commit -m "Initial feature implementation"
Version Navigation
View commit history:
git log --oneline
Navigate between versions:
# Return to previous commit
git reset --hard HEAD^
# Go back two commits
git reset --hard HEAD^^
# Navigate 5 commits back
git reset --hard HEAD~5
# Switch to specific commit using hash
git reset --hard a1b2c3d
File Recovery Scenarios
Scenario 1: Unstaged modifications
git checkout -- filename.txt
Scenario 2: Staged but uncommitted changes
git reset HEAD filename.txt
git checkout -- filename.txt
Sceanrio 3: Committed changes - use version navigation
Scenario 4: File deletion
# Restore deleted file
git checkout -- deleted-file.txt
# Permanent removal from repository
git rm permanent-delete.txt
git commit -m "Remove obsolete file"
Branch Management
Branch Operations
Create and manage development branches:
# List existing branches
git branch -a
# Create new branch
git branch feature-branch
# Switch to branch
git checkout feature-branch
# Create and switch simultaneously
git checkout -b new-feature
# Merge branch into current
git merge source-branch
# Delete merged branch
git branch -d completed-branch
# Force delete unmerged branch
git branch -D abandoned-branch
Merge Conflict Resolution
When branches contain conflicting changes:
- Git halts merge operation
- Conflict markers appear in affected files
- Manually resolve conflicts by editing files
- Stage resolved files and complete merge:
git add resolved-file.js
git commit -m "Merge conflict resolution"
Remote Repository Integration
SSH Authentication Setup
Generate SSH keys for secure remote access:
ssh-keygen -t rsa -C "developer@company.com"
Copy public key to remote service:
cat ~/.ssh/id_rsa.pub
Remote Reposiotry Connection
Link local repository to remote:
git remote add origin git@host.com:user/repository.git
Synchronize local repository with remote:
git pull origin main --allow-unrelated-histories
Push local commits to remote:
git push origin main
Essential Command Reference
git status: Check repository stategit diff: View unstaged changesgit log: Examine commit historygit clone: Copy remote repositorygit fetch: Download remote changesgit stash: Temporarily store modifications