How to Initialize a Git Repository in Visual Studio Code

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


✅ Option 1: Initialize Git Using the VS Code Interface

Steps:

  1. Open your project folder in VS Code.
  2. Click on the Source Control icon (left sidebar, or Ctrl+Shift+G / Cmd+Shift+G).
  3. You’ll see a message: “This folder does not contain a Git repository.”
  4. Click the “Initialize Repository” button. Initialize Git in VS Code
  5. Git is now initialized. You’ll see a list of files ready to be staged under “Changes”.
  6. To commit your code:
    • Stage files with the + icon
    • Type a commit message
    • Click the checkmark (✔️) to commit

✅ Option 2: Initialize Git Using the Terminal

Alternatively, use the built-in terminal in VS Code:

  1. Open the terminal:
    • `Ctrl+“ (backtick) on Windows/Linux
    • `Cmd+“ on macOS
  2. Run: git init
  3. Git will initialize a new repository in your project folder.
  4. 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

TaskMethod
Initialize repo (GUI)Source Control → “Initialize”
Initialize repo (CLI)git init in terminal
Connect to remotegit remote add origin <URL>
Commit changesStage → 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.

Sharing Is Caring:

Leave a Comment