Lesson 8
22 min
Initialize a repository, understand file statuses and make your first commit.
Definition
Git is a version control software: it records the history of the changes made to a project's files, which lets you go back to an earlier version and work with other people without overwriting their work. A project tracked by Git is called arepository, and each saved state of it is called acommit.
Git is a version control tool: it lets you save the complete history of a project's changes, go back if needed, and collaborate with several people on the same code without overwriting everything. It is not a VS Code tool — it is independent software, very widely used in the industry — but VS Code offers a native integration that is particularly handy for getting started without having to type all the commands by hand.
Imagine you edit your site for an hour and suddenly nothing works anymore. Without a history, you have to track down the mistake by hand, or worse, start over. With Git, you can compare your current code with the last version that worked and go back in a few seconds. It's like a permanent safety net that lets you experiment without fear.
Git is also essential for teamwork: each developer works on their own copy of the project, then the changes are merged cleanly. That is why almost every company and open source project uses it, and why learning its basics now is an excellent investment. Don't worry: you don't need to understand everything at once. A few simple actions are enough to get started, and the rest will come naturally with practice.
Prerequisites
To follow this lesson, Git must be installed on your machine (separately from VS Code). You can check whether it is already there by opening a terminal and typing git --version. If it isn't recognized, download it from git-scm.com.
Before your first commit, Git needs to know who you are, so it can record the author of each change. You only do this setup once per computer, with the following two commands (replace the values with your own):
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"A project tracked by Git is called a repository (or "repo"). To turn your project folder into a Git repository, you have two options: either through the integrated terminal, or directly from the VS Code interface.
git initFrom the interface, open the Source Control tab in the Activity Bar (branch-shaped icon, shortcut Ctrl + Shift + G, also with Ctrl on Mac). If your folder isn't a repository yet, an "Initialize Repository" button is offered right there.
This operation creates a hidden folder named .git at the root of your project. This is where Git stores the whole history. Never edit or delete it by hand: if you delete it, you lose the history and your folder goes back to being an ordinary folder.

Once a repository is initialized, VS Code automatically displays, in the Source Control tab, the list of files changed since the last tracked state. Each file is accompanied by a letter indicating its status:
These same letters also appear in color directly on the files in the Explorer, which lets you spot at a glance what has changed in your project without even opening the Git tab.
Git works in two steps. First, you choose the changes you want to save by placing them in the staging area. Then, you confirm this selection with a commit. This lets you group your changes logically: if you changed three files for three different reasons, you can create three separate commits, each with its own message.
A commit is a "snapshot" of your project's state at a given moment, accompanied by a message describing what changed. It is the basic unit of Git's history. To create a commit from VS Code:
git add command.git commit -m "message".git add .
git commit -m "Add the home page"If you click Commit without having staged anything, VS Code offers to stage all your changes and commit them directly. This is convenient, but be sure to check what you are about to commit.
The commit message is what you (or a colleague) will reread in a few weeks to understand what happened. A few simple tips:
Even before confirming a commit, you can click a changed file in the list to open a "diff" view: it displays, side by side (or inline depending on the setting), the old version in red and the new one in green, line by line. It is extremely useful for checking exactly what you are about to save before confirming your commit.
To view a file's past commits, open the "Timeline" section at the very bottom of the Explorer. It lists each saved version of the active file, with its message and date, and clicking an entry shows the differences introduced by that commit.
Everything you see in the VS Code interface can also be obtained in the terminal. The git status command displays the changed files and the ones ready to be committed, while git log --oneline lists past commits, one per line. Knowing these two commands is useful, because they let you check your project's state from any environment.
git status
git log --onelineIf you changed a file by mistake, the curved arrow icon next to the file in the list of changes lets you discard the changes and get back the last saved version. Be careful: this action is permanent for uncommitted changes, because Git never saved them. VS Code asks for confirmation before erasing them.
Day to day, you will always repeat the same loop, whether you use the interface or the terminal:
Some files shouldn't be tracked by Git: temporary files, dependency folders, passwords... To do this, you create a file named .gitignore at the root of your project and list in it, one per line, the names of the files or folders to ignore (for example node_modules/ or .env). Git will then leave them aside and they will no longer appear in the list of changes.
One caveat: .gitignore only applies to files that Git is not yet tracking. If a file (such as one containing a password) has already been committed, adding it to.gitignore afterwards doesn't remove it from the history. So create your .gitignore before your first commit.
Good to know
This lesson only covers the local basics of Git in VS Code. Concepts such as branches, syncing with a remote repository (GitHub, GitLab...) or resolving conflicts are covered in more depth in a dedicated Git course.
git init.One last piece of advice: make regular commits, at each significant step of your work, rather than one huge commit at the end of the day. The more detailed your history, the easier it is to find the exact moment when a mistake appeared, and to go back to a healthy version without losing the rest of your work. It's a simple habit that will serve you throughout your career.
Practical exercise
my-first-project project, initialize a Git repository from the Source Control tab, then make a first commit containing all your files with the message "First commit".Going further
index.html file, watch the "M" status appear in the Source Control tab, click the file to open the "diff" view, then confirm a second commit with a message describing your change.