Tutorial

Git Beginner's Guide: Understanding Version Control via "Savepoints" and "Multiverses"

2026-06-30 #Git#Tools

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.zip
  • project-final-v2.zip
  • project-final-v2-real-final.zip
  • project-final-v3-boss-approved.zip

This quickly turns into a disaster. Git helps you solve these issues by:

  1. Recording the changes made at each stage of the project.
  2. Allowing you to roll back to previous versions.
  3. Clearly showing who modified what.
  4. Enabling simultaneous development of multiple features without interference.
  5. Synchronizing code during multiplayer collaboration.
  6. 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
2
3
4
5
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

# Check configuration
git config --list

2. Create Your First Git Repository

Suppose you have a project folder, tell Git to start managing this folder:

1
2
3
mkdir my-project
cd my-project
git init

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
2
echo "Hello Git" > README.md
git status

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
2
3
git log
# Or use the cleaner single-line output
git log --oneline

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

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
2
3
4
5
# View all branches; the current branch is marked with an *
git branch

# Switch back to the main world
git switch main

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
2
3
git switch main
# Merge the achievements of new-feature into the main line
git merge new-feature

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
2
3
4
5
6
7
8
9
10
11
# Clone repository: Download a complete game world from the cloud server
git clone https://github.com/example/my-project.git

# View the currently configured remote address (usually called origin)
git remote -v

# Push: Upload the local main line to the origin cloud
git push -u origin main

# Pull: Synchronize the latest world state from the cloud
git pull

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

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
2
3
4
5
6
# Stuff current unfinished modifications into the temporary pocket
git stash

# Now the working directory is clean, you can switch branches to do your task...
# After finishing the task and switching back, take out the temporary modifications:
git stash pop

Conclusion: The Mental Model Beginners Should Master Most

The core of Git is not rote memorization of commands, but understanding the following mental models:

  1. commit is a savepoint: Not every Ctrl+S file save will be recorded; only running commit formally generates a save.
  2. branch is 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.
  3. HEAD is your eyes: It represents the specific worldline position you are currently observing and occupying.
  4. push / pull is 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
Share

Comments