VS Code SetupBeginner

How to Initialize a Git Repository in VS Code

Turn a plain project folder into a git repository, make your first commit, and understand the Source Control view, all without typing git commands.

7 minBeginner

Version control is the safety net that lets an AI assistant make bold changes to your code while you keep the power to undo any of them. VS Code has git built into the Source Control view, so you can create a repository and commit changes with buttons instead of memorizing commands. This guide takes a fresh folder from no history to its first commit.

What you need

  • Git installed on your machine (run git --version to check)
  • A project folder open in VS Code
  • Your git name and email configured (covered below)

Step 1: Set your git identity once

If you have never used git on this machine, set the name and email that will be stamped on your commits. Open the integrated terminal and run these two commands once. They apply to every repository.

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

Step 2: Open the Source Control view

Click the Source Control icon in the activity bar on the left (it looks like a branching line), or press Ctrl+Shift+G. Because the folder is not yet a repository, you will see an Initialize Repository button.

VS Code - Source Control
SOURCE CONTROL
------------------------------------
The folder is not a git repository.
[ Initialize Repository ]
[ Publish to GitHub ]
An uninitialized folder offers to create a repository.

Step 3: Initialize the repository

Click Initialize Repository. VS Code runs git init for you and creates a hidden .git folder. The Source Control view now lists every file in your project under Changes, because nothing has been committed yet.

Step 4: Add a .gitignore

Before your first commit, create a .gitignore file so you do not commit junk like node_modules or environment secrets. Make a new file named .gitignore in the project root with these lines.

.gitignore
node_modules/
dist/
.env
.DS_Store

Step 5: Stage and commit

Hover over Changes and click the plus icon to stage all files, or stage individual files. Type a short message in the box at the top, such as Initial commit, then click the checkmark (Commit) button. Your first snapshot is saved.

VS Code - first commit
SOURCE CONTROL v ...
Message: Initial commit
[ Commit ]
------------------------------------
STAGED CHANGES 4
+ package.json
+ src/index.js
+ .gitignore
+ README.md
Files staged with a commit message ready to go.
Commit early and often
Before letting an AI agent refactor anything, commit your working code. If the change goes wrong you can discard everything back to the last commit in one click.

Result

Your folder is now a git repository with a clean first commit and a .gitignore protecting it from clutter. Every future change shows up in Source Control, ready to review and commit.

Watch related tutorials

Tags
#git#source-control#commit#version-control