Writing code, modifying documents, and working on projects is just like exploring an open-world game. The role of Git is not to write code for you, but to help you create savepoints at crucial moments. This allows you to look back at history, undo mistakes, switch between different development paths, and even let multiple people collaborate across different "worldlines", eventually merging the results back into the main storyline.
In a nutshell: Git is a tool used to manage the history of a project. Every commit is a savepoint, and every branch is a worldline.
1. What Problem Does Git Actually Solve?
Before Git, you might have saved files like this:
project-final.zipproject-final-v2.zipproject-final-v2-real-final.zipproject-final-v3-boss-approved.zip
This quickly turns into a disaster. Git helps you solve these issues by:
- Recording the changes made at each stage of the project.
- Allowing you to roll back to previous versions.
- Clearly showing who modified what.
- Enabling simultaneous development of multiple features without interference.
- Synchronizing code during multiplayer collaboration.
- Safely undoing things when problems arise.
For better understanding, we can directly compare Git concepts with single-player games:
| Git Concept | English Term | Game Analogy |
|---|---|---|
| 仓库 | Repository | The entire game save system |
| 工作区 | Working Directory | The current world you are playing in |
| 暂存区 | Staging Area | The list of items prepared to be written into the next save |
| 提交 | Commit | An official savepoint |
| 分支 | Branch | A parallel worldline |
| 合并 | Merge | Merging the achievements of one worldline into another |
| 远程仓库 | Remote | The cloud save server |
| 克隆 | Clone | Downloading a copy of the game world from the cloud |
| 推送 | Push | Uploading a local save to the cloud |
| 拉取 | Pull | Synchronizing the latest save from the cloud |
2. Getting Started: Setting Identity and Creating a Repository
1. Set Your Identity
Git needs to know "who created this savepoint". This doesn't register an account; it simply signs your commit records.
1 | git config --global user.name "Your Name" |
2. Create Your First Git Repository
Suppose you have a project folder, tell Git to start managing this folder:
1 | mkdir my-project |
This step is equivalent to installing a "save system" into this game world. After execution, a hidden folder .git/ will appear in your project. This is Git's core database, containing history records and branch information. Do not modify it manually under normal circumstances.
3. Git's Three Areas: Current World, Staging Area, Official Save
The most confusing part of Git for beginners is that it doesn't "automatically save after modifying a file". It has three important areas: Working Directory → Staging Area → Local Repository.
1. Working Directory: The current world you are playing in
When you modify files in your editor, these changes occur in the working directory. At this point, Git sees that you changed something, but it hasn't put it into the save yet.
1 | echo "Hello Git" > README.md |
It will prompt Untracked files, meaning Git found a new file, but it hasn't been included in the save system.
2. Staging Area: The list of items prepared for the save
Adding a file to the staging area is equivalent to deciding to put this change into the next savepoint:
1 | git add README.md |
3. Local Repository: Creating the official savepoint
Submitting is the real "saving" process:
1 | git commit -m "Create README file" |
Once successfully submitted, Git will generate a commit with a unique ID, such as a1b2c3d Create README file. This is a complete, official savepoint.
Note
Beginners are advised to first use git status to confirm the state before running git commit. Although git add . can stage all changes in the current directory at once, you must be clear about what you are adding to avoid saving redundant files by mistake.
4. Viewing History and Changes
1. View Commit History (Git Log)
This is like bringing up the save list in a game, with newer saves at the top:
1 | git log |
2. View File Changes (Git Diff)
Before creating a savepoint, you need to confirm exactly what you've changed:
What changed in my current world compared to the last save?
1 | git diff |
What are the contents that I've added to the staging area, ready to be written to the next savepoint?
1 | git diff --staged |
5. Branch and Merge: Multiverses
One of Git's most powerful features is branching.
1. Open a New Worldline
Suppose your main storyline is called main. You want to develop a new feature but aren't sure if it will succeed. To avoid contaminating the main line, you can create a new worldline:
Create and switch to the new branch in one step:
1 | git switch -c new-feature |
Committing code in this new branch will not affect the main storyline. You are free to experiment.
2. Switch Worldlines
You can switch between different branches at any time, and the file contents will change accordingly because different branches represent different historical states.
1 | # View all branches; the current branch is marked with an * |
3. Merge Branches and Resolve Conflicts
When the side quest is completed and you've acquired top-tier equipment, you want to bring the achievements back to the main line:
1 | git switch main |
If two branches modified the same line of code, it will prompt a Conflict when merging. Marks like <<<<<<< HEAD will appear in the file. A conflict is not scary; it's just Git telling you: "These two worldlines have undergone different changes at the same location, and I need you to decide the final plot." Manually modify and keep the code you want, then re-run git add and git commit.
6. Remote Repository: Cloud Synchronized Saves
The local repository only exists on your computer. For backups or multiplayer collaboration, we typically use remote servers like GitHub or GitLab as cloud save servers.
1 | # Clone repository: Download a complete game world from the cloud server |
Golden Rule of Collaboration: Before starting your daily work or committing code, it is strongly recommended to first run git pull to synchronize the latest state from the server. This can significantly reduce the probability of merge conflicts.
7. Undo and Temporary Pocket (Stash)
1. Safe Undo and Rollback
If you break your code or commit the wrong content, you can safely "load a save" through the following commands:
Discard current un-added changes, returning to the state of the last official savepoint:
1 | git restore README.md |
Regret running git add; take the file out of the staging area (does not delete actual modifications):
1 | git restore --staged README.md |
Create a new "reverse commit" to undo the effects of a specified commit. This is the safest practice in collaboration!
1 | git revert a1b2c3d |
Dangerous! Forcibly move the timeline back to an old savepoint, discarding all subsequent commits:
1 | git reset --hard a1b2c3d |
2. Stash: The Temporary Pocket Save
Sometimes you are half-way through developing a feature but need to switch branches temporarily to fix a Bug, yet the code isn't ready for a commit. You can use the temporary pocket:
1 | # Stuff current unfinished modifications into the temporary pocket |
Conclusion: The Mental Model Beginners Should Master Most
The core of Git is not rote memorization of commands, but understanding the following mental models:
commitis a savepoint: Not everyCtrl+Sfile save will be recorded; only runningcommitformally generates a save.branchis a worldline: A branch is not a clunky copy of a folder, but a lightweight pointer on the historical timeline, allowing for stress-free switching.HEADis your eyes: It represents the specific worldline position you are currently observing and occupying.push/pullis cloud synchronization: A local commit does not mean it's uploaded.
Remember this phrase: You are not managing a bunch of file copies; you are managing a historical universe of a project constantly splitting, evolving, and converging. When in doubt, type git status at any time to take a look at the map, and then let loose to explore the coding world!
Comments