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



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:

TEXT
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:

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.

100%
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.

100%
graph TB
    S[Central Server<br/>All Versions]
    S --> A[UserA]
    S --> B[UserB]
    S --> C[UserC]

Representative tools: CVS, Subversion (SVN), Perforce

Advantages:

Disadvantages:


(3) 3.3 Distributed Version Control

Every developer has a complete version control repository (repository) that includes the entire history.

100%
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:

Disadvantages:



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:

Git's Design Goals:

  1. Speed: It must be very fast
  2. Simplicity: The design should be simple and elegant
  3. Support for non-linear development: Allows for thousands of parallel branches
  4. Fully Distributed: Every developer has a complete repository
  5. 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:

TEXT
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:

TEXT
v1: file.txtA complete snapshot of
v2: file.txtA complete snapshot of
v3: file.txtA complete snapshot of

Advantages:

Almost all operations are performed locally

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:

100%
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:

TEXT
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).

100%
graph LR
    C1[SubmitC1] --> C2[SubmitC2] --> C3[SubmitC3]
    main[mainBranch] --> C3
    feature[featureBranch] --> C3


6. Use Cases for Git

(1) 6.1 Personal Projects

(2) 6.2 Teamwork

(3) 6.3 Open-Source Projects

(4) 6.4 Continuous Integration/Continuous Deployment (CI/CD)



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

BASH
# 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:

BASH
git diff               # View unstaged changes
git diff --staged      # View staged changes
git diff HEAD~1 HEAD   # Compare last commit with previous

❓ FAQ

Q What is the relationship between Git and GitHub?
A Git is a version control tool, and GitHub is a code hosting platform based on Git. It’s like Git is a “car” and GitHub is a “parking lot”—you can use Git to manage your code locally, then push it to GitHub for hosting and collaboration.
Q Why should I learn Git?
A Git has become the industry standard. 95% of developers use Git, and virtually all open-source projects are managed with Git. If you don’t learn Git, you won’t be able to participate in team collaboration or contribute to open-source projects.
Q Does distributed version control take up a lot of space?
A Git uses compression and deduplication techniques, making it highly storage-efficient. For example, the Linux kernel has over 1 million commits, but the Git repository is only about 1 GB.
Q Is SVN still in use?
A We're still using it, but its usage has been declining year by year. SVN is suitable for managing large binary files (such as game assets), but we recommend using Git for new projects.

📖 Summary


📝 Exercises

  1. Basic Question: Explain in your own words the difference between "distributed version control" and "centralized version control," listing at least three points.

  2. Advanced Question: Research projects or companies in your area to find out what version control tools they use and why they chose those tools.

  3. 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.

Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏