Visual Studio Code (VS Code) makes it simple to integrate Git into your development workflow. Whether you’re starting a new project or adding version control to an existing one, initializing a Git repository in VS Code is just a few clicks or commands away.
In this guide, you’ll learn how to initialize a Git repo using the VS Code interface and the built-in terminal.
🧰 Prerequisites
- Visual Studio Code installed
- Git installed on your system
- A folder or project open in VS Code
✅ Option 1: Initialize Git Using the VS Code Interface
Steps:
- Open your project folder in VS Code.
- Click on the Source Control icon (left sidebar, or
Ctrl+Shift+G
/Cmd+Shift+G
). - You’ll see a message: “This folder does not contain a Git repository.”
- Click the “Initialize Repository” button.
- Git is now initialized. You’ll see a list of files ready to be staged under “Changes”.
- To commit your code:
- Stage files with the
+
icon - Type a commit message
- Click the checkmark (✔️) to commit
- Stage files with the
✅ Option 2: Initialize Git Using the Terminal
Alternatively, use the built-in terminal in VS Code:
- Open the terminal:
- `Ctrl+“ (backtick) on Windows/Linux
- `Cmd+“ on macOS
- Run:
git init
- Git will initialize a new repository in your project folder.
- You can now stage, commit, and push as usual.
✅ Optional: Connect to a Remote Repository
To push your local repo to GitHub or any other Git hosting service:
git remote add origin https://github.com/username/repo-name.git
git branch -M main
git push -u origin main
Make sure you’ve created the remote repository on GitHub first.
🔍 Tips for Using Git in VS Code
- .gitignore: Add a
.gitignore
file to exclude files from being tracked. - GitLens Extension: Enhance Git capabilities in VS Code with GitLens (for viewing commit history, blame annotations, etc.).
- Integrated Diff Viewer: Click a file in Source Control to see diffs before staging.
📝 Summary
Task | Method |
---|---|
Initialize repo (GUI) | Source Control → “Initialize” |
Initialize repo (CLI) | git init in terminal |
Connect to remote | git remote add origin <URL> |
Commit changes | Stage → Message → Checkmark (✔️) |
Initializing a Git repository in VS Code is a quick and intuitive process. Whether you’re working solo or collaborating in a team, adding version control is a vital step toward managing and tracking your code efficiently.