Version Control Introduction: Essential Skills for Managing Code Changes â
What is Version Control? â
Version Control is a system that records file change history, capable of:
- Tracking every modification
- Recording who made modifications
- Explaining why modifications were made
- Restoring to previous versions when needed
Understanding Version Control Through a Life Analogy â
Imagine you're writing an important paper:
Without version control:
paper_draft.docx
paper_revised.docx
paper_revised2.docx
paper_revised_final.docx
paper_revised_final_really_final.docx
paper_revised_final_advisor_edits.docxProblems with this approach:
- Chaotic file names, unsure which is latest version
- Cannot compare differences between versions
- Don't know what was changed each time
- More and more files, taking up lots of space
- Easy conflicts when collaborating
With version control:
paper.docxJust one file, but version control system will:
- Record every modification
- Label each version (v1, v2, v3...)
- Record modification time and modifier
- Add modification descriptions
- Restore to any historical version anytime
Why Version Control is Needed? â
1. Tracking Change History â
Scenario: Code worked fine last week, but a bug appeared today.
Without version control:
- Don't know what was changed
- Cannot restore to previous version
- Can only rewrite code
With version control:
- View recent modification records
- Find specific change that introduced bug
- Restore to working version with one click
- Compare code differences before and after
2. Team Collaboration â
Scenario: Three developers working on same project simultaneously.
Without version control:
- Pass code through email or chat tools
- Manually merge each person's modifications
- Easy to overwrite others' code
- Don't know who changed what
With version control:
- Each person develops in their own branch
- Automatic code merging
- Prompts when conflicts occur
- Clear record of each person's contributions
3. Backup and Recovery â
Scenario: Computer hard drive fails, all code lost.
Without version control:
- Code permanently lost
- Need to start from scratch
With version control:
- Code stored on remote server
- Can recover from any device
- All historical versions completely preserved
4. Experimenting with New Features â
Scenario: Want to try a new idea, but unsure if it's feasible.
Without version control:
- Afraid of breaking existing code
- Need to backup entire project
- Rolling back is troublesome
With version control:
- Create experimental branch
- Modify and test freely
- Merge if successful, delete if failed
- Doesn't affect main code
5. Code Review â
Scenario: Team needs to ensure code quality.
With version control:
- View specific changes in each commit
- Comment and discuss code
- Require modifications before merging
- Maintain code quality standards
Types of Version Control Systems â
Local Version Control â
How it works:
- Records versions on local computer
- Through copying folders or using simple database
Advantages:
- Simple to use
- Doesn't need network
Disadvantages:
- Cannot collaborate
- Data easily lost
- Limited functionality
Example: RCS (obsolete, rarely used anymore)
Centralized Version Control â
How it works:
- Central server stores all versions
- Developers get latest code from server
- Submit back to server after modifications
Representatives: SVN (Subversion), CVS
Advantages:
- Simple management
- Easy permission control
- Easy to understand
Disadvantages:
- Depends on central server
- Cannot work if server is down
- Poor network affects efficiency
- Central server failure may lose all data
Distributed Version Control â
How it works:
- Each developer has complete codebase
- Can work offline
- Can commit locally
- Periodically sync with remote
Representatives: Git, Mercurial
Advantages:
- Doesn't depend on central server
- Can work offline
- Fast
- More flexible workflows
- Safer (multiple complete backups)
Disadvantages:
- Steeper learning curve
- Relatively complex concepts
Git is currently the most popular version control system, with platforms like GitHub, GitLab, and Bitbucket all based on Git.
Git Core Concepts â
While we mainly explain concepts rather than operations, understanding Git's core concepts is very important.
Repository â
A repository is where all project files and historical records are stored.
Local Repository:
- On your computer
- Contains complete project history
- Can operate offline
Remote Repository:
- On a server (like GitHub)
- Used for team collaboration
- Code backup
Commit â
A commit is a snapshot of code changes. Each commit contains:
- Changed files: Which files were modified, added, or deleted
- Commit message: Describes what this change did
- Author information: Who made the modification
- Timestamp: When it was modified
- Unique identifier: A hash value (like
a3f2b9c)
Meaning of commits:
- Record every important moment in project
- Can freely switch between commits
- Constitutes complete project history
Good commit habits:
- Each commit only contains related changes
- Write clear commit messages
- Commit frequently (small steps, quick iterations)
- Ensure each commit's code can run
Branch â
A branch is a parallel universe of code, allowing you to develop new features without affecting main code.
Why branches are needed?
Imagine book creation process:
- Main branch (main/master): Officially published version
- Feature branches: Drafts trying new chapters
- Hotfix branches: Correcting discovered errors
Branch advantages:
- Isolated development: Different features don't interfere with each other
- Parallel work: Multiple people can develop different features simultaneously
- Safe experimentation: Can directly delete branch if failed
- Clear workflow: Each branch has clear purpose
Common branch strategies:
Main branch (main):
- Always keep stable and runnable
- Only accept tested code
- Corresponds to production environment
Development branch (develop):
- For daily development
- Contains latest development progress
- Regularly merged to main branch
Feature branches (feature/xxx):
- Develop single feature
- Created from develop branch
- Merged back to develop when complete
Hotfix branches (hotfix/xxx):
- Emergency fixes for production bugs
- Created from main branch
- Merged to both main and develop after fix
Merge â
Merging is integrating one branch's changes into another branch.
Merge process:
Feature branch: Developed new feature A
â
Main branch: Merge feature A
â
Main branch: Now contains new feature AMerge conflicts:
When two branches modify the same code, Git cannot automatically decide which version to keep, resulting in a conflict:
Scenario:
- Developer A modified line 10 of
utils.jsin feature branch - Developer B also modified line 10 of
utils.jsin another branch - Git doesn't know which version to keep during merge
Resolution:
- Manually view conflict file
- Decide which version to keep or integrate both
- Mark conflict as resolved
- Complete merge
Remote and Local Synchronization â
Push:
- Upload local commits to remote repository
- Let team members see your changes
- Backup code
Pull:
- Download latest code from remote repository
- Get team members' changes
- Keep code synchronized
Clone:
- Copy complete project from remote repository
- Used when first obtaining project
- Includes all historical records
Git Hosting Platforms â
While Git is a tool, you need a place to store remote repositories. Here are popular Git hosting platforms:
GitHub â
Features:
- World's largest code hosting platform
- Powerful social features
- Rich open source projects
- Complete CI/CD integration
- Free private repositories
Suitable for:
- Open source projects
- Personal projects
- Learning and sharing
- Job portfolio showcase
GitLab â
Features:
- Complete DevOps platform
- Powerful CI/CD capabilities
- Can self-host (private deployment)
- Enterprise-level features
Suitable for:
- Internal enterprise use
- Need private deployment
- Complete DevOps workflow
Bitbucket â
Features:
- Atlassian product series (integrates with Jira)
- Supports Git and Mercurial
- Free for small teams
Suitable for:
- Teams using Atlassian toolchain
- Small teams
Version Control Workflow â
Basic Personal Workflow â
1. Initialize or clone project
- New project: Initialize Git repository
- Join existing project: Clone remote repository
2. Daily development cycle
Modify code
â
View changes
â
Stage changes
â
Commit changes (with description)
â
Push to remote repository3. Get updates
- Regularly pull latest code from remote
- Avoid code diverging too much
- Discover conflicts early
Team Collaboration Workflow â
Feature development process:
1. Create feature branch
Create new branch from develop: feature/user-login2. Develop feature
Develop on feature branch
Commit frequently, record progress3. Stay synchronized
Regularly pull latest code from develop
Merge into feature branch
Resolve possible conflicts4. Code review
Create Pull Request/Merge Request
Team members review code
Discuss and improve5. Merge to mainline
Merge to develop after review passes
Delete feature branch6. Release
Create release branch from develop
Test
Merge to main
Tag version numberVersion Control Best Practices â
1. Commit Frequently â
Benefits:
- Easy to trace specific changes
- Reduce excessive changes at once
- Easier to resolve conflicts
Recommendations:
- Commit when completing a small feature
- Commit when fixing a bug
- Don't wait until all features complete to commit
2. Write Good Commit Messages â
Bad commit messages:
"Modified code"
"Bug fix"
"Update"
"test"Good commit messages:
"Add user login feature"
"Fix: Registration page email validation failure issue"
"Optimize: Homepage loading speed improved by 30%"
"Refactor: Extract common component UserCard"Recommended commit message format:
<type>: <brief description>
<detailed description (optional)>
<related issue number (optional)>Type examples:
- feat (feature): New feature
- fix: Bug fix
- docs: Documentation update
- style: Code format (doesn't affect functionality)
- refactor: Refactoring
- test: Testing related
- chore: Build, configuration, etc.
3. Keep Branches Clean â
Delete merged branches promptly:
- Avoid too many branches
- Keep repository clean
- Reduce confusion
Regularly merge main branch:
- Keep feature branch synchronized with main branch
- Discover conflicts early
- Reduce final merge difficulty
4. Don't Commit Generated Files â
Files to ignore:
- Dependency files:
node_modules/ - Build artifacts:
dist/,build/ - Temporary files:
.DS_Store,Thumbs.db - Log files:
*.log - Environment configs:
.env(contains secrets) - Editor settings:
.vscode/,.idea/
Use .gitignore file:
# Dependency directory
node_modules/
# Build output
dist/
build/
# Environment configuration
.env
.env.local
# Logs
*.log
# OS files
.DS_Store
Thumbs.db5. Protect Main Branch â
Set branch protection rules:
- Prohibit direct commits to main branch
- Require code review
- Require tests to pass
- Require multiple approvals
Benefits:
- Ensure code quality
- Prevent accidental damage
- Enforce team collaboration workflow
Summary â
Version Control Core Points:
Version control is the foundation of code management: Tracking changes, team collaboration, backup recovery
Git is mainstream choice: Distributed, fast, flexible
Core concepts:
- Repository: Store code and history
- Commit: Save change snapshots
- Branch: Independent development lines
- Merge: Integrate different branches
Team collaboration:
- Use branches to isolate development
- Review code through merge requests
- Keep code synchronized
Best practices:
- Commit frequently with clear descriptions
- Use branches rationally
- Protect main branch
- Ignore unnecessary files
As a beginner:
- Don't be intimidated by Git's complexity
- Master basic operations first (commit, push, pull)
- Gradually understand advanced concepts through practice
- Mistakes aren't scary, Git can almost always recover
Value of version control:
Version control is not just a tool, it's the infrastructure of modern software development:
- Protects your work: Code is never truly lost
- Supports team collaboration: Makes multi-person development possible
- Improves development efficiency: Focus on feature development, not worrying about code management
- Records project history: Clear growth trajectory