Skip to content
Tutorials Center
My learning

Topics

Lesson 29 of 40 ยท Git and Remote Development

Source Control Basics in VS Code

Use the VS Code Source Control view for Git: initialize, stage changes by line, commit, sync with a remote, read gutter indicators and use the Git commands.

VS Code has Git built in. If Git is installed on your computer, the editor can show which files changed, stage and commit them, switch branches and sync with a remote, all without typing a command. You still need to understand what Git does, but the graphical view makes the daily loop faster and shows differences clearly.

This lesson walks through the Source Control view and the everyday workflow. The stand-ins at the end model a status listing and a commit message check; they are JavaScript, not Git. For Git itself, the site's Git tutorial covers the concepts in depth.

Prerequisites

Install Git from the official site or your package manager and check it from a terminal with git --version. VS Code finds it automatically (setting git.path if not). Tell Git who you are once:

Terminal
git config --global user.name "Your Name"
git config --global user.email "[email protected]"

The Source Control view

Open it with the Activity Bar branch icon or Ctrl+Shift+G. If the folder is not a repository, the button Initialize Repository runs git init. If it is, you see:

  • A message box at the top for the commit message, with a Commit button.
  • A Changes list (and Staged Changes once you stage something). Each file shows a letter: M modified, U untracked (new), A added, D deleted, R renamed, C conflict.
  • Section toolbars and the ... menu with Pull, Push, Fetch, Branch, Stash and more.
  • A Graph and Repositories section to see history and several repos at once.

The status bar at the bottom left shows the current branch; click it to switch or create branches. The sync icon beside it shows how many commits you are ahead or behind the remote.

The daily loop

  1. Edit files. Changed files appear in Changes.
  2. Review. Click a file to open a side-by-side diff (more in diffs and merge conflicts).
  3. Stage. The + next to a file stages it; the + on the Changes header stages everything. You can stage part of a file: open the diff, select lines, and use Stage Selected Ranges, or click the stage icon in the diff gutter.
  4. Write a message in the box and press Ctrl+Enter to commit.
  5. Sync. Use Sync Changes or Push to send commits to the remote, and Pull to fetch the others' work.

If nothing is staged, Commit can offer to stage and commit everything. Set git.enableSmartCommit to make that automatic, or leave it off for deliberate commits.

Staging part of a file lets you split a messy afternoon into several clean commits: one for the bug fix, one for the rename.

Gutter and editor indicators

In the editor's left margin (the gutter):

  • A green bar marks added lines.
  • A blue bar marks modified lines.
  • A red triangle marks deleted lines.

Click a bar to see an inline diff with buttons to revert that change or stage it. The Explorer and tab titles also color files by Git status. Turn the decorations off with git.decorations.enabled if they distract you.

Branches, cloning and the Command Palette

Type Git: in the Command Palette (Ctrl+Shift+P) to see the commands: Git: Clone, Git: Checkout to..., Git: Create Branch..., Git: Merge Branch..., Git: Stash, Git: Fetch, Git: Pull, Git: Publish Branch. Git: Clone asks for a repository URL and a folder, then offers to open it.

Branch operations are covered further in branches, history and the timeline.

Useful settings

JSON
{
  "git.autofetch": true,
  "git.confirmSync": true,
  "git.enableSmartCommit": false,
  "git.postCommitCommand": "none",
  "scm.defaultViewMode": "tree"
}

git.autofetch periodically runs git fetch so the ahead/behind numbers stay current. git.postCommitCommand can be "push" or "sync" to push after each commit, which is risky on shared branches; keep it off unless you know why you want it.

Common mistakes

  • Committing secrets such as .env files. Add them to .gitignore first.
  • Committing generated folders such as node_modules/ or dist/.
  • Committing on the wrong branch. Check the branch name in the status bar before you commit.
  • Discarding changes by accident. The discard button throws away uncommitted edits; VS Code asks for confirmation, so read the dialog.
  • Vague messages like "fix". A good message says what and why in a short first line.

Runnable stand-ins

JS Stand-in: file status letters

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: check a commit message

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which shortcut opens the Source Control view?

    Ctrl+Shift+G is Source Control, Ctrl+Shift+F is Search and Ctrl+Shift+D is Run and Debug.
  2. 2. What does the letter U beside a file mean?

    U means untracked. M is modified, A added, D deleted.
  3. 3. How do you commit from the message box using the keyboard?

    Ctrl+Enter (Cmd+Enter on macOS) commits the staged changes with the typed message.
  4. 4. What does a blue bar in the editor gutter mean?

    Green marks added lines, blue marks modified lines and a red triangle marks a deletion.

Summary

  • VS Code uses the Git installed on your computer; open the view with Ctrl+Shift+G.
  • The loop is edit, review, stage, commit, sync.
  • You can stage individual lines or ranges to make clean commits.
  • Gutter bars show added, modified and deleted lines.
  • Ignore secrets and generated folders before the first commit.

Next: Diffs and Merge Conflicts