Lesson 8 / 8100%

Lesson 8

Getting Started with Git in VS Code

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 and VS Code: a native integration

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.

Why use Git?

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.

Setting up your identity

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):

terminal
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

Initializing a Git repository

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.

terminal
git init

From 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.

Source Control view in VS Code

Understanding file statuses

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:

  • U (Untracked): a new file that has never been tracked by Git yet.
  • M (Modified): an already tracked file that has been modified since its last save.
  • D (Deleted): a tracked file that has been deleted.
  • A (Added): a new file that has been added to the staging area and will be included in the next commit.

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.

The principle of the staging area

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.

Making your first commit

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:

  • Click the "+" next to each file (or the general "+" that appears when hovering over "Changes") to add it to the staging area — this corresponds to the git add command.
  • Write a clear, short message in the text field at the top, for example "Add the home page".
  • Click the Commit button (checkmark ✓) to confirm — the equivalent of git commit -m "message".
command-line equivalent
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.

Writing good commit messages

The commit message is what you (or a colleague) will reread in a few weeks to understand what happened. A few simple tips:

  • Be precise: "Add the contact form" is much more useful than "updates".
  • Keep it short: a line of about fifty characters is enough to summarize the change.
  • One commit, one idea: avoid mixing several unrelated changes in the same commit.

Viewing the history and the differences

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.

Checking the repository state from the command line

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.

terminal
git status
git log --oneline

Undoing a change

If 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.

The workflow to remember

Day to day, you will always repeat the same loop, whether you use the interface or the terminal:

  • Edit your files in the editing area.
  • Check the changes with the "diff" view.
  • Stage the relevant files with the "+".
  • Commit with a clear message by clicking Commit.

Ignoring certain files

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.

Common mistakes to avoid

  • Forgetting to set up your identity: Git will usually refuse to create a commit, or show an error, until your name and email are set.
  • Initializing a repository in the wrong place: make sure you are in your project folder before running git init.
  • Committing without reviewing: always take a moment to look at the "diff" view before confirming.

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

In your 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

Edit the content of your 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.