Getting Started with Git: Understanding Version Control

Learn what Git is, how it helps during development, and how it differs from traditional backups in this beginner-friendly introduction to version control.

🎯 What will I learn?

With a basic understanding of Git, you can understand how version control works, manage your code's history, and handle conflicts and collaboration. Git is more than a code backup tool: it also helps teams develop software efficiently.


✨ What is Git?

Git is an open-source version control system that helps you manage and back up code. It tracks changes so you can review your editing history and restore earlier versions whenever you need them. Git also supports collaboration and helps resolve conflicts that can arise when several people edit code at the same time. You can keep a complete repository locally and synchronize it with a remote repository to keep your code safe and accessible. Git makes code management, backups, and teamwork easier.

Everyday services such as iCloud, photo synchronization, and phone management apps offer a useful analogy: data is stored in the cloud, with synchronized copies on your devices. Using Git with a remote repository provides a similar way to synchronize code, making collaboration more efficient and reliable.


✏️ How Git helps during development: a simple example

In everyday development, Git primarily helps you manage changes, avoid losing code, and collaborate with others.

Suppose you write a simple web page:

<!DOCTYPE html>
<html>
    <head>
        <title>My First Page</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                background-color: #f0f0f0;
            }
        </style>
    </head>
    <body>
        <h1>Welcome to My Website</h1>
        <p>This is a simple page managed with Git.</p>
    </body>
</html>

When you change the code, such as updating the heading, you can record the change with Git:

git add index.html
git commit -m "Update the heading"

Git saves a snapshot of this commit, allowing you to restore an earlier version whenever you need to.


How does this compare with sharing files manually?

The task: a project contains two files, a.html and b.html. Your manager asks Xiaoming to change the heading in a.html to “xiaoming” and Libai to change the heading in b.html to “libai”. Both changes must be finished within five minutes, so they need to work at the same time.

Updating code by sharing files manually

Typically, the project is stored on Libai's computer. To edit a.html, Xiaoming first asks Libai to send it to him, perhaps through WeChat. After editing it, Xiaoming sends the file back and explains exactly what changed. Libai then manually replaces the file or merges the changes. Passing files back and forth requires extra communication and makes mistakes more likely, such as using different file versions or overwriting someone else's changes.

Updating code with Git

Instead of keeping the project only on one person's computer, the team stores it in an online repository on a platform such as GitHub or Gitee. Both Libai and Xiaoming have local copies of that repository.

After editing a.html, Xiaoming commits his changes locally and pushes them to the online repository. He no longer needs to send files to Libai or explain each change verbally. Libai can then pull the latest code with Git to synchronize his local copy and see Xiaoming's changes.

This keeps versions organized, makes changes traceable, and helps resolve conflicts when people edit code at the same time. Git handles transferring and synchronizing changes without requiring the team to send files through WeChat.


❓ Knowledge check

Q1: What is Git? Git is a version control tool that manages the history of file changes and supports collaboration.

Q2: How does Git differ from traditional backups? Traditional backups copy files. Git tracks changes through versioned snapshots and makes it easy to inspect and switch between earlier versions.

Q3: Why use Git during development? Git helps prevent code loss, makes history easy to review, and supports multiple developers working together.


🎉 You now understand Git's basic concepts, its role in development, and how it differs from traditional backups! Start using Git to manage your code and make development more efficient and reliable.