Why Version Control?¶
Imagine you’re writing a paper: you change the title today, add a data section tomorrow, and then decide the original structure was better the day after. Without tracking these changes, you might have to recreate from scratch or accidentally overwrite important content. That’s exactly what version control solves: safely preserving code history so you can always go back, collaborate, and experiment.
Version control is even more critical in team collaboration. For example, if you and a colleague modify the same file simultaneously, without version control, the code becomes a mess. With version control, each person works on their own “copy” and merges results later, avoiding conflicts.
What is Git?¶
Git is a distributed version control system. The key difference from traditional centralized tools (like SVN) is: every developer has a complete local copy of the code history, so you don’t need constant internet access. This means you can work offline and sync data once connectivity returns, significantly improving development flexibility.
Git’s Core: Snapshots¶
Git’s most unique design is “Snapshots”. Unlike traditional tools (e.g., SVN) that track “differences” between versions, Git saves complete copies of the current code state with each commit.
For example:
- Traditional tools (e.g., SVN) are like “journals,” recording “what changed from A to B.”
- Git is like “photographs,” saving “a full picture of the current state” with each commit—regardless of how many changes were made.
This design simplifies version rollbacks: just select the corresponding “photo” instead of calculating complex differences.
Version Evolution: Branches & Pointers¶
Git’s version evolution is managed through branches. A branch is essentially a “pointer” to a specific snapshot (commit). Think of it as:
- The main branch (e.g., main or master) is the project’s “main line,” pointing to stable versions.
- Other branches (e.g., feature/login) are parallel “side roads” for independent development, with no interference between branches.
Example workflow:
- You develop core features on the main branch while working on payment functionality in feature/payment.
- Each branch evolves independently until merged back to the main branch, completing the project iteration.
Working Directory, Staging Area & Local Repository¶
Git has three core areas. Understanding their relationship is fundamental to mastering Git operations:
- Working Directory: Where you write code—the actual directory with visible and editable files.
- Staging Area: Temporarily stores changes “ready for commit.” The
git addcommand moves modifications from the working directory to the staging area. - Local Repository: A database storing all snapshots (commits). The
git commitcommand “snapshots” staged changes into the local repository.
Operation flow:
Write code (Working Directory) → git add (Staging Area) → git commit (Local Repository)
Basic Operations & Version History¶
1. Initialize a Repository¶
git init # Creates an empty Git repository in the current directory
2. Check Status¶
git status # Shows the status of the working directory and staging area
3. Commit Changes¶
git add filename.txt # Adds a specific file to the staging area
git add . # Adds all modified files to the staging area (`.` = current directory)
git commit -m "Add user login functionality" # Commits staged changes to the local repository
4. View Version History¶
git log # Shows all commit records, including hashes, author, timestamp, and message
5. Branch Operations¶
git branch feature/new-feature # Creates a new branch
git checkout feature/new-feature # Switches to the new branch (Git 2.23+ recommends `git switch`)
git merge feature/new-feature # Merges the feature branch into the current branch
Version Rollbacks & Distributed Collaboration¶
Version Rollbacks¶
If a recent commit has issues, use git reset to revert to a historical version:
git reset --hard HEAD~1 # Rolls back to the previous version (~1 = last, ~2 = second last)
Distributed Collaboration¶
To share code with your team, use remote repositories (e.g., GitHub, GitLab) to sync changes:
git remote add origin https://github.com/yourname/yourrepo.git # Links to a remote repository
git push origin main # Pushes the local main branch to the remote repository
git pull origin main # Pulls remote updates to the local repository
Summary¶
Git’s essence is “Snapshots + Branches”:
- Snapshots: Fully record each code state, enabling traceability and rollbacks.
- Branches: Parallelize development directions through pointers, supporting team collaboration.
By understanding the relationship between the working directory, staging area, and local repository, and mastering basic operations like add, commit, and branch, you can easily manage Git to keep code evolution clear and controlled.
Git’s strength lies in the combination of simple concepts and flexible tools. Even beginners can quickly get started through daily operations and gradually deepen their understanding of its underlying logic.