Using Git In Visual Studio Code



Integrated Git support is one of the great features of Visual Studio Code. Git allows you to track your work, move back and forth between versions, and work on different branches at the same time. Visual Studio Code supports Git by default, which means you don't have to install any packages or extensions to use it. The Side Bar contains a separate Git View you can quickly open by clicking on the Git icon in the View Bar.

Initialize Your Repository¶

If your workspace isn't yet tracked by Git, first you need to initialize a new repository. To do so, click on the Initialize Git Repository button in the Git View.

Install

VS Code will create a .git folder inside your workspace (you can't see this from VS Code, as it's a hidden directory, but you can find it in your file manager in the root folder of your project), and it will also add your untracked files to the Side Bar.

Stage Changes¶

After your Git repository is set up, it's time to create your initial commit (a commit is a recorded state of your whole repository you can later move back to). Before committing, you need to add the files you want to track with Git to the staging area, or in other words you need to stage the changes. To stage a file, click the + button that appears when you hover over that file in the Changes section.

  • Git, installed Visual Studio Code, installed A basic understanding of Git concepts and commands, such as working with repositories, forks, clones, and branches, staging and unstaging changes, and pushing commits. You need a GitHub account.
  • Sign in to GitHub by using Visual Studio Code. Use Visual Studio Code to search GitHub for repos. Clone a repo from Visual Studio Code. Publish a local project to GitHub by using Visual Studio Code. View a timeline of activity on a GitHub repo.

After you click on the files you want to stage, Visual Studio Code moves that file from the Changes folder to the Staged Changes folder, that means that now they are in the staging area. If you want to stage all your files at once, click the + button that appears when you hover over the Changes subtitle.

Commit Changes¶

Staged changes are not yet recorded, you also need to commit them, so that later you can have a look at or move back to the current state of your repository. In order to commit the staged changes, you need to add a commit message that describes the changes you made since your previous commit.

Our GitHub workflow takes advantage of Visual Studio Code (VS Code). We will cover (in this post) the basics of how to get a local copy, push code changes to the development branch, and then pull those changes into the master branch. Master Branch and Develop Branch There are often many code changes in between releases when working on a project.

Since in this example this will be the initial commit, let's use the not-so-creative 'Initial Commit' message. Type your commit message into the input bar at the top of the SideBar, and click on the little tick icon. Now you have made your first commit, and the initial state of your repository is recorded.

Your Git working directory is clean, all the changes you made so far are tracked, and your Git View says there are 0 changes (it always compares the current state to your last commit). If you make new changes in your workspace, the new changes will appear again in the SideBar, and you'll need to commit them again once you have made significant changes that you want to record.

Git

In the screenshot below you can see how your code editor looks right after committing.

Review Changes¶

As you proceed with your work, if you are not sure about how the current version compares to your last commit, you can quickly check the differences. In the Git View, you can just double click on the file you want to compare, and VS Code will show the current and previous versions side by side, so you can quickly compare them.

Using Git In Visual Studio Code

Speaking of reviewing changes, it's also important to notice Visual Studio Code's gutter indicators that annotate changes compared to your previous commit, even when the side-by-side view is not open. VS Code uses 3 kinds of gutter indicators:

  1. A red triangle for every line deletion

  2. A green bar for every line addition

  3. A blue bar for every line modification

Create Branches¶

Git allows you to easily create and switch between branches. This is useful for experimenting with new solutions as well as to facilitate code reviews using merge requests. We recommend using short-lived feature branches and merge requests to facilitate code reviews and tracking changes in systems like CMC.

In Visual Studio Code, you can create new branches by clicking on the name of the current branch in the bottom left and selecting + Create New Branch. Or you can open the Command palette by hitting Ctrl+Shift+P (Cmd+Shift+P on Mac), then typing the Git: Create Branch command into the input bar.

We also have a helper command adx git:branch that makes it easier to follow our suggested naming convention.

Git

Connect Git In Visual Studio Code

Checkout Branches¶

To switch between branches, click the name of your current branch in the bottom left and select your new branch from the list shown. Or use the Command palette (Ctrl/Cmd+Shift+P) and select Git: Checkout to ....

Using Git In Visual Studio Code

Use Git Commands from the Command Palette¶

You can access Git commands from the Command Palette (F1 on Windows and Linux, Ctrl/Cmd+Shift+P on Mac) as well. You don't have to do anything else, just type the command Git inside the Command Palette, and you get a dropdown list of the available commands you can quickly choose from.

Using Github In Visual Studio Code

Advanced Git Operations¶

So far, we have looked at how to use basic Git operations in Visual Studio Code to track our work. VS Studio Code has advanced Git features as well, such as managing remote repositories, merging conflicts, and you can even configure VS Code as your external Git editor.

The Visual Studio Code Marketplace also contains Git-related extensions that can make your Git workflow even more professional.

Install Git Visual Code

View a Git Graph of your repository, and easily perform Git actions from the graph. Configurable to look the way you want!

Features

  • Git Graph View:
    • Display:
      • Local & Remote Branches
      • Local Refs: Heads, Tags & Remotes
      • Uncommitted Changes
    • Perform Git Actions (available by right clicking on a commit / branch / tag):
      • Create, Checkout, Delete, Fetch, Merge, Pull, Push, Rebase, Rename & Reset Branches
      • Add, Delete & Push Tags
      • Checkout, Cherry Pick, Drop, Merge & Revert Commits
      • Clean, Reset & Stash Uncommitted Changes
      • Apply, Create Branch From, Drop & Pop Stashes
      • View annotated tag details (name, email, date and message)
      • Copy commit hashes, and branch, stash & tag names to the clipboard
    • View commit details and file changes by clicking on a commit. On the Commit Details View you can:
      • View the Visual Studio Code Diff of any file change by clicking on it.
      • Open the current version of any file that was affected in the commit.
      • Copy the path of any file that was affected in the commit to the clipboard.
      • Click on any HTTP/HTTPS url in the commit body to open it in your default web browser.
    • Compare any two commits by clicking on a commit, and then CTRL/CMD clicking on another commit. On the Commit Comparison View you can:
      • View the Visual Studio Code Diff of any file change between the selected commits by clicking on it.
      • Open the current version of any file that was affected between the selected commits.
      • Copy the path of any file that was affected between the selected commits to the clipboard.
    • Code Review - Keep track of which files you have reviewed in the Commit Details & Comparison Views.
      • Code Review's can be performed on any commit, or between any two commits (not on Uncommitted Changes).
      • When a Code Review is started, all files needing to be reviewed are bolded. When you view the diff / open a file, it will then be un-bolded.
      • Code Reviews persist across Visual Studio Code sessions. They are automatically closed after 90 days of inactivity.
    • View uncommitted changes, and compare the uncommitted changes with any commit.
    • Hover over any commit vertex on the graph to see a tooltip indicating:
      • Whether the commit is included in the HEAD.
      • Which branches, tags and stashes include the commit.
    • Filter the branches shown in Git Graph using the 'Branches' dropdown menu. The options for filtering the branches are:
      • Show All branches
      • Select one or more branches to be viewed
      • Select from a user predefined array of custom glob patterns (by setting git-graph.customBranchGlobPatterns)
    • Fetch from Remote(s) (available on the top control bar)
    • Find Widget allows you to quickly find one or more commits containing a specific phrase (in the commit message / date / author / hash, branch or tag names).
    • Repository Settings Widget:
      • Allows you to view, add, edit, delete, fetch & prune remotes of the repository.
      • Configure 'Issue Linking' - Converts issue numbers in commit messages into hyperlinks, that open the issue in your issue tracking system.
      • Configure 'Pull Request Creation' - Automates the opening and pre-filling of a Pull Request form, directly from a branches context menu.
        • Support for the publicly hosted Bitbucket, GitHub and GitLab Pull Request providers is built-in.
        • Custom Pull Request providers can be configured using the Extension Setting git-graph.customPullRequestProviders (e.g. for use with privately hosted Pull Request providers). Information on how to configure custom providers is available here.
      • Export your Git Graph Repository Configuration to a file that can be committed in the repository. It allows others working in the same repository to automatically use the same Git Graph configuration.
    • Keyboard Shortcuts (available in the Git Graph View):
      • CTRL/CMD + F: Open the Find Widget.
      • CTRL/CMD + H: Scrolls the Git Graph View to be centered on the commit referenced by HEAD.
      • CTRL/CMD + R: Refresh the Git Graph View.
      • CTRL/CMD + S: Scrolls the Git Graph View to the first (or next) stash in the loaded commits.
      • CTRL/CMD + SHIFT + S: Scrolls the Git Graph View to the last (or previous) stash in the loaded commits.
      • When the Commit Details View is open on a commit:
        • Up / Down: The Commit Details View will be opened on the commit directly above or below it on the Git Graph View.
        • CTRL/CMD + Up / CTRL/CMD + Down: The Commit Details View will be opened on its child or parent commit on the same branch.
          • If the Shift Key is also pressed (i.e. CTRL/CMD + SHIFT + Up / CTRL/CMD + SHIFT + Down), when branches or merges are encountered the alternative branch is followed.
      • Enter: If a dialog is open, pressing enter submits the dialog, taking the primary (left) action.
      • Escape: Closes the active dialog, context menu or the Commit Details View.
    • Resize the width of each column, and show/hide the Date, Author & Commit columns.
    • Common Emoji Shortcodes are automatically replaced with the corresponding emoji in commit messages (including all gitmoji). Custom Emoji Shortcode mappings can be defined in git-graph.customEmojiShortcodeMappings.
  • A broad range of configurable settings (e.g. graph style, branch colours, and more...). See the 'Extension Settings' section below for more information.
  • 'Git Graph' launch button in the Status Bar
  • 'Git Graph: View Git Graph' launch command in the Command Palette

Extension Settings

Detailed information of all Git Graph settings is available here, including: descriptions, screenshots, default values and types.

A summary of the Git Graph extension settings are:

  • Commit Details View:
    • Auto Center: Automatically center the Commit Details View when it is opened.
    • File View:
      • File Tree:
        • Compact Folders: Render the File Tree in the Commit Details View in a compacted form, such that folders with a single child folder are compressed into a single combined folder element.
      • Type: Sets the default type of File View used in the Commit Details View.
    • Location: Specifies where the Commit Details View is rendered in the Git Graph View.
  • Context Menu Actions Visibility: Customise which context menu actions are visible. For more information, see the documentation here.
  • Custom Branch Glob Patterns: An array of Custom Glob Patterns to be shown in the 'Branches' dropdown. Example: [{'name':'Feature Requests', 'glob':'heads/feature/*'}]
  • Custom Emoji Shortcode Mappings: An array of custom Emoji Shortcode mappings. Example: [{'shortcode': ':sparkles:', 'emoji':'✨'}]
  • Custom Pull Request Providers: An array of custom Pull Request providers that can be used in the 'Pull Request Creation' Integration. For information on how to configure this setting, see the documentation here.
  • Date:
    • Format: Specifies the date format to be used in the 'Date' column on the Git Graph View.
    • Type: Specifies the date type to be displayed in the 'Date' column on the Git Graph View, either the author or commit date.
  • Default Column Visibility: An object specifying the default visibility of the Date, Author & Commit columns. Example: {'Date': true, 'Author': true, 'Commit': true}
  • Dialog > *: Set the default options on the following dialogs: Add Tag, Apply Stash, Cherry Pick, Create Branch, Delete Branch, Fetch into Local Branch, Fetch Remote, Merge, Pop Stash, Pull Branch, Rebase, Reset, and Stash Uncommitted Changes
  • Enhanced Accessibility: Visual file change A|M|D|R|U indicators in the Commit Details View for users with colour blindness. In the future, this setting will enable any additional accessibility related features of Git Graph that aren't enabled by default.
  • File Encoding: The character set encoding used when retrieving a specific version of repository files (e.g. in the Diff View). A list of all supported encodings can be found here.
  • Graph:
    • Colours: Specifies the colours used on the graph.
    • Style: Specifies the style of the graph.
    • Uncommitted Changes: Specifies how the Uncommitted Changes are displayed on the graph.
  • Integrated Terminal Shell: Specifies the path and filename of the Shell executable to be used by the Visual Studio Code Integrated Terminal, when it is opened by Git Graph.
  • Keyboard Shortcut > *: Configures the keybindings used for all keyboard shortcuts in the Git Graph View.
  • Markdown: Parse and render a frequently used subset of inline Markdown formatting rules in commit messages and tag details (bold, italics, bold & italics, and inline code blocks).
  • Max Depth Of Repo Search: Specifies the maximum depth of subfolders to search when discovering repositories in the workspace.
  • Open New Tab Editor Group: Specifies the Editor Group where Git Graph should open new tabs, when performing the following actions from the Git Graph View: Viewing the Visual Studio Code Diff View, Opening a File, Viewing a File at a Specific Revision.
  • Open to the Repo of the Active Text Editor Document: Open the Git Graph View to the repository containing the active Text Editor document.
  • Reference Labels:
    • Alignment: Specifies how branch and tag reference labels are aligned for each commit.
    • Combine Local and Remote Branch Labels: Combine local and remote branch labels if they refer to the same branch, and are on the same commit.
  • Repository:
    • Commits:
      • Fetch Avatars: Fetch avatars of commit authors and committers.
      • Initial Load: Specifies the number of commits to initially load.
      • Load More: Specifies the number of additional commits to load when the 'Load More Commits' button is pressed, or more commits are automatically loaded.
      • Load More Automatically: When the view has been scrolled to the bottom, automatically load more commits if they exist (instead of having to press the 'Load More Commits' button).
      • Mute:
        • Commits that are not ancestors of HEAD: Display commits that aren't ancestors of the checked-out branch / commit with a muted text color.
        • Merge Commits: Display merge commits with a muted text color.
      • Order: Specifies the order of commits on the Git Graph View. See git log for more information on each order option.
      • Show Signature Status: Show the commit's signature status to the right of the Committer in the Commit Details View (only for signed commits). Hovering over the signature icon displays a tooltip with the signature details.
    • Fetch and Prune: Before fetching from remote(s) using the Fetch button on the Git Graph View Control Bar, remove any remote-tracking references that no longer exist on the remote(s).
    • Fetch And Prune Tags: Before fetching from remote(s) using the Fetch button on the Git Graph View Control Bar, remove any local tags that no longer exist on the remote(s).
    • Include Commits Mentioned By Reflogs: Include commits only mentioned by reflogs in the Git Graph View (only applies when showing all branches).
    • On Load:
      • Scroll To Head: Automatically scroll the Git Graph View to be centered on the commit referenced by HEAD.
      • Show Checked Out Branch: Show the checked out branch when a repository is loaded in the Git Graph View.
      • Show Specific Branches: Show specific branches when a repository is loaded in the Git Graph View.
    • Only Follow First Parent: Only follow the first parent of commits when discovering the commits to load in the Git Graph View. See --first-parent to find out more about this setting.
    • Show Commits Only Referenced By Tags: Show Commits that are only referenced by tags in Git Graph.
    • Show Remote Branches: Show Remote Branches in Git Graph by default.
    • Show Remote Heads: Show Remote HEAD Symbolic References in Git Graph.
    • Show Stashes: Show Stashes in Git Graph by default.
    • Show Tags: Show Tags in Git Graph by default.
    • Show Uncommitted Changes: Show uncommitted changes. If you work on large repositories, disabling this setting can reduce the load time of the Git Graph View.
    • Show Untracked Files: Show untracked files when viewing the uncommitted changes. If you work on large repositories, disabling this setting can reduce the load time of the Git Graph View.
    • Sign:
      • Commits: Enables commit signing with GPG or X.509.
      • Tags: Enables tag signing with GPG or X.509.
    • Use Mailmap: Respect .mailmap files when displaying author & committer names and email addresses.
  • Repository Dropdown Order: Specifies the order that repositories are sorted in the repository dropdown on the Git Graph View (only visible when more than one repository exists in the current Visual Studio Code Workspace).
  • Retain Context When Hidden: Specifies if the Git Graph view Visual Studio Code context is kept when the panel is no longer visible (e.g. moved to background tab). Enabling this setting will make Git Graph load significantly faster when switching back to the Git Graph tab, however has a higher memory overhead.
  • Show Status Bar Item: Show a Status Bar Item that opens the Git Graph View when clicked.
  • Source Code Provider Integration Location: Specifies where the 'View Git Graph' action appears on the title of SCM Providers.
  • Tab Icon Colour Theme: Specifies the colour theme of the icon displayed on the Git Graph tab.

This extension consumes the following settings:

  • git.path: Specifies the path and filename of a portable Git installation.

Extension Commands

This extension contributes the following commands:

  • git-graph.view: Git Graph: View Git Graph
  • git-graph.addGitRepository: Git Graph: Add Git Repository... (used to add sub-repos to Git Graph)
  • git-graph.clearAvatarCache: Git Graph: Clear Avatar Cache
  • git-graph.endAllWorkspaceCodeReviews: Git Graph: End All Code Reviews in Workspace
  • git-graph.endSpecificWorkspaceCodeReview: Git Graph: End a specific Code Review in Workspace... (used to end a specific Code Review without having to first open it in the Git Graph View)
  • git-graph.fetch: Git Graph: Fetch from Remote(s) (used to open the Git Graph View and immediately run 'Fetch from Remote(s)')
  • git-graph.removeGitRepository: Git Graph: Remove Git Repository... (used to remove repositories from Git Graph)
  • git-graph.resumeWorkspaceCodeReview: Git Graph: Resume a specific Code Review in Workspace... (used to open the Git Graph View to a Code Review that is already in progress)
  • git-graph.version: Git Graph: Get Version Information

Release Notes

Detailed Release Notes are available here.

Visual Studio Marketplace

This extension is available on the Visual Studio Marketplace for Visual Studio Code.

Acknowledgements

Thank you to all of the contributors that help with the development of Git Graph!

Some of the icons used in Git Graph are from the following sources, please support them for their excellent work!

  • GitHub Octicons (License)
  • Icons8 (License)