Git: Introduction to Git and Version Control Concepts
Git is the world's most popular distributed version control system—it makes team collaboration simple and efficient.
This tutorial is designed for beginners with no prior experience. It starts with the basics of version control and progresses until you are proficient in using Git for team collaboration.
1. What You'll Learn
- Basic Concepts of Version Control
- The History and Design Philosophy of Git
- Distributed vs. Centralized Version Control
- The Core Advantages of Git
- Use Cases for Git
2. What Is Version Control?
Version control is a system that tracks changes to file content so that specific versions can be retrieved in the future.
(1) Why is version control necessary?
Imagine you're writing a paper:
Thesis-v1.doc # First Edition
Thesis-v2.doc # Some content has been revised.
Thesis-Final Version.doc # My advisor said I still need to revise it.
Thesis-Final Version2.doc # Made a few more changes
Thesis-I'll never change the layout.doc # This really is the final version.
Thesis-I'll never change the layout2.doc # All right,Still needs to be revised...
The problem with this approach:
- Confusing file names: Which one is the actual final version?
- Lack of traceability: It's unclear what changes were made each time
- Prone to errors: You may have edited the wrong file version
- Collaboration Challenges: What should you do when multiple people are editing the same file?
Version control systems were created precisely to address these issues.
3. The Evolution of Version Control
(1) 3.1 Local Version Control
The earliest form of version control involved storing all versions of a file locally.
graph TB
A[Local Database<br/>v1, v2, v3] --> B[Workspace]
Representative tools: RCS (Revision Control System)
Disadvantages: No support for collaboration among multiple users
(2) 3.2 Centralized Version Control
All versions of the dataset are stored on a single server. Team members retrieve files from the server, make changes, and then submit them back.
graph TB
S[Central Server<br/>All Versions]
S --> A[UserA]
S --> B[UserB]
S --> C[UserC]
Representative tools: CVS, Subversion (SVN), Perforce
Advantages:
- Easy to manage, with straightforward access control
- Everyone can see what others are doing
Disadvantages:
- Single Point of Failure: If the server goes down, no one can work.
- Internet Dependency: An internet connection is required for this to work
- Slow: All operations require a server request
(3) 3.3 Distributed Version Control
Every developer has a complete version control repository (repository) that includes the entire history.
graph LR
A[UserAWarehouse<br/>Complete History] <--> B[UserBWarehouse<br/>Complete History]
B <--> C[UserCWarehouse<br/>Complete History]
A <--> C
Representative tools: Git, Mercurial, Bazaar
Advantages:
- Work Offline: Submit changes, view history, and create branches without an internet connection
- Fast: Most operations are performed locally
- High Disaster Tolerance: Each user's repository is a full backup
- Flexible Workflows: Supports multiple collaboration modes
Disadvantages:
- The initial clone is slow (requires downloading the entire history)
- The concept is relatively complex
4. The Birth of Git
(1) 4.1 The Origins of Git
In 2005, a major event took place in the Linux kernel development community:
- The Linux kernel project has always used the proprietary BitKeeper version control system
- BitMover, the owner of BitKeeper, has revoked the free usage rights
- Linus Torvalds, the founder of Linux, decided to develop a new version control system on his own
Git's Design Goals:
- Speed: It must be very fast
- Simplicity: The design should be simple and elegant
- Support for non-linear development: Allows for thousands of parallel branches
- Fully Distributed: Every developer has a complete repository
- Efficient handling of large-scale projects: such as the Linux kernel
Linus completed the initial version of Git in April 2005 in about two weeks. Today, Git has become the world's most popular version control system.
(2) 4.2 Git's Design Philosophy
Git's design embodies several core principles:
Snapshot, Not Differences
Other systems (such as SVN) store the differences between files:
v1: file.txt (Original Document)
v2: file.txt + diff1 (Storage Differences)
v3: file.txt + diff1 + diff2 (Cumulative Difference)
Git stores a complete snapshot of each commit:
v1: file.txtA complete snapshot of
v2: file.txtA complete snapshot of
v3: file.txtA complete snapshot of
Advantages:
- Extremely fast version switching (directly reads snapshots)
- Creating a branch is inexpensive (it's just a pointer)
Almost all operations are performed locally
- View History: Local
- Submit changes: Local
- Create a branch: Local
- Switch branch: Local
Only push and pull operations require an internet connection.
5. Core Concepts of Git
(1) 5.1 Repository
A repository is a core concept in Git and includes:
- Working Directory: The files you are actually editing
- Staging Area: Changes ready to be committed
- Repository: The history of all commits
graph TB
subgraph GitWarehouse
R[Repository .git<br/>All Commit History<br/>All Branch Information]
S[Buffer Stage<br/>Changes Ready to Be Submitted]
W[Workspace Work<br/>Files Actually Edited]
end
W -->|git add| S
S -->|git commit| R
(2) 5.2 Commit
A commit is the basic unit of Git and consists of:
- Snapshot: The state of all files at a specific point in time
- Metadata: Author, Date, Commit Message, Parent Commit
- Unique Identifier: 40-character SHA-1 hash value
commit a1b2c3d4e5f6... (40Bit Hash)
Author: Zhang San <zhangsan@example.com>
Date: 2024-01-01 10:00:00
Add User Login Functionality
(3) 5.3 Branch
A branch is a movable pointer to a commit. Creating a branch in Git is extremely inexpensive—it simply involves creating a 41-byte file (a 40-byte hash plus a 1-byte newline character).
graph LR
C1[SubmitC1] --> C2[SubmitC2] --> C3[SubmitC3]
main[mainBranch] --> C3
feature[featureBranch] --> C3
6. Use Cases for Git
(1) 6.1 Personal Projects
- Track Revision History: Roll back to any version at any time
- Try out new features: Create a branch to experiment with them, and delete it if it fails
- Backup code: Push to the remote repository as a backup
(2) 6.2 Teamwork
- Parallel Development: Multiple people working simultaneously on different branches
- Code Review: Pull Request Process
- Conflict Resolution: Automatic merging, manual conflict resolution
(3) 6.3 Open-Source Projects
- Fork Workflow: First fork, then make changes, and finally submit a PR
- Contribute Code: Submit a pull request to the project
- Track Upstream: Stay in sync with the original project
(4) 6.4 Continuous Integration/Continuous Deployment (CI/CD)
- Automated Testing: Tests run automatically after code is submitted
- Automatic Deployment: Deploy automatically after testing is successful
- Version Release: Tag and release a new version
7. Git vs. Other Version Control Systems
| Features | Git | SVN | CVS |
|---|---|---|---|
| Architecture | Distributed | Centralized | Centralized |
| Offline Work | ✅ Fully supported | ❌ Not supported | ❌ Not supported |
| Branch Creation | Very fast (O(1)) | Slow (directory copy) | Not supported |
| Storage Method | Snapshot | Differential | Differential |
| Network Dependency | Push/Pull Only | All Operations | All Operations |
| Disaster Recovery | High (full backup per person) | Low (server-dependent) | Low |
▶ Example: Basic Git Commands
# InitializationGitWarehouse
git init
# View Repository Status
git status
# Add a file to the staging area
git add .
# Submit Changes
git commit -m "Initial Submission"
# View commit history
git log
▶ Example
Viewing differences between commits:
git diff # View unstaged changes
git diff --staged # View staged changes
git diff HEAD~1 HEAD # Compare last commit with previous
❓ FAQ
📖 Summary
- Version control is a system that tracks changes to files, addressing issues such as version confusion and collaboration difficulties.
- Version control has evolved from local to centralized to distributed.
- Git was created by Linus Torvalds in 2005 and was designed with speed, simplicity, and decentralization in mind.
- Git stores snapshots rather than differences, and almost all operations are performed locally
- Core Git Concepts: Repositories, Commits, Branches
- Git is suitable for personal projects, team collaboration, open-source projects, CI/CD, and other scenarios
📝 Exercises
-
Basic Question: Explain in your own words the difference between "distributed version control" and "centralized version control," listing at least three points.
-
Advanced Question: Research projects or companies in your area to find out what version control tools they use and why they chose those tools.
-
Challenge: Read Linus Torvalds’ original email about Git (search for "Linus Torvalds Git mailing list") to learn about the background and design philosophy behind Git’s creation.