GitHub Copilot in VS Code With GitLens Integration: Practical Patterns
🔍 WiseChecker

GitHub Copilot in VS Code With GitLens Integration: Practical Patterns

GitHub Copilot generates code suggestions directly in Visual Studio Code. GitLens adds deep Git history, blame annotations, and code lens information to the editor. When you combine both tools, Copilot can use Git context from GitLens to produce more relevant code. This article explains how the integration works, shows step-by-step setup, and covers the most useful patterns for daily development work.

Key Takeaways: GitHub Copilot and GitLens Integration in VS Code

  • GitLens CodeLens annotations: Copilot reads commit messages and blame data to generate context-aware code suggestions that match your project’s history.
  • GitLens inline blame: When Copilot suggests a function, it can reference the last author and commit from GitLens, improving code review accuracy.
  • Copilot Chat with Git context: Type @gitlens /explain in Copilot Chat to get explanations of Git history for a selected code block.

ADVERTISEMENT

How GitHub Copilot and GitLens Work Together in VS Code

GitHub Copilot is an AI pair programmer that suggests code as you type. It uses the open file, surrounding code, and comments to generate completions. GitLens supercharges the Git capabilities of VS Code. It shows inline blame annotations, CodeLens with commit details, a powerful GitLens explorer, and interactive rebase tools.

The integration is not a single toggle. Instead, GitLens exposes Git metadata—author names, commit messages, file change history, and branch information—that Copilot can use indirectly. When you select a block of code and use Copilot Chat with the @gitlens participant, Copilot can explain why a change was made based on Git history. In the editor, GitLens annotations provide context that helps you evaluate Copilot’s suggestions faster.

Prerequisites

You need the following installed in VS Code:

  • GitHub Copilot extension (version 1.100 or later)
  • GitLens extension (version 14.0 or later)
  • VS Code 1.85 or later
  • A GitHub Copilot subscription (individual, business, or enterprise)
  • A Git repository with at least one commit

Setting Up the Integration

The integration requires no special configuration file. Both extensions work side by side once installed. Follow these steps to verify and optimize the setup.

  1. Install both extensions from the VS Code marketplace
    Open the Extensions view by pressing Ctrl+Shift+X. Search for GitHub Copilot and GitLens. Install each one. Restart VS Code after installation.
  2. Sign in to GitHub Copilot
    Open the Command Palette with Ctrl+Shift+P. Type Copilot and select GitHub Copilot: Sign In. Complete the browser-based authentication. A green checkmark in the status bar confirms activation.
  3. Enable GitLens CodeLens
    Open Settings with Ctrl+, and search for GitLens CodeLens. Make sure GitLens: CodeLens is set to All or Blame. This shows commit author and date above each function.
  4. Enable GitLens inline blame
    In the same settings, search for GitLens Inline Blame. Set GitLens: Inline Blame Enabled to true. This shows the last commit and author on the current line.
  5. Test the integration
    Open a file in a Git repository. Select a function. Open Copilot Chat with Ctrl+Shift+I. Type @gitlens /explain. Copilot should return a Git history explanation for that code block.

ADVERTISEMENT

Practical Patterns for Daily Use

The real value appears when you apply both tools to specific development tasks. Below are the most effective patterns.

Pattern 1: Code Review With Blame Context

When reviewing a pull request, you often need to know who wrote a block of code and why. GitLens inline blame shows the author and commit message on each line. Copilot can then suggest improvements that respect the original intent.

Select a function with high blame activity. Open Copilot Chat and ask: “Suggest a refactor for this function that preserves its current behavior.” Copilot uses the function body and any comments as context. GitLens annotations tell you whether the code was written recently or is legacy, which helps you decide how aggressively to refactor.

Pattern 2: Generating Tests for Changed Code

After making changes in a feature branch, you want unit tests that cover the new logic. GitLens shows which lines differ from the base branch. Use the GitLens explorer to select the changed files. Then ask Copilot: “Generate unit tests for the changed functions in this file.”

Copilot reads the current file content and can reference the diff context if you have the VS Code diff editor open. GitLens highlights the exact lines that need test coverage. This pattern reduces the time spent writing boilerplate test code.

Pattern 3: Understanding Legacy Code With /explain

Legacy code often lacks documentation. Git commit messages are the only written history. Select a confusing block of code. Open Copilot Chat and type @gitlens /explain. Copilot returns a summary of the Git history for that selection, including the commit messages and authors involved.

This pattern works because GitLens provides the Git context to Copilot through the chat participant API. You do not need to open the Git log manually. The explanation appears inline in the chat panel.

Pattern 4: Auto-Completing Repetitive Git Operations

When you type a Git command in the terminal, Copilot can suggest the full command based on your recent history. GitLens enhances this by showing the last few commands in the GitLens explorer. For example, type git commit -m and Copilot suggests a message based on the diff and recent commit patterns.

This pattern works best when GitLens is set to show recent commits in the status bar. The combination reduces typing errors and keeps commit messages consistent across the team.

Common Issues and Limitations

Copilot Chat Does Not Recognize @gitlens

If Copilot Chat ignores the @gitlens participant, update both extensions to the latest version. Open the Extensions view, right-click GitHub Copilot and GitLens, and select Update. The participant was added in GitLens 14.0 and requires Copilot Chat 1.0 or later. If updates do not help, reload the VS Code window with Ctrl+Shift+P and Developer: Reload Window.

Copilot Suggestions Ignore Git History

Copilot does not read GitLens data directly in the editor. It only uses the file content, comments, and the open file path. To make Copilot aware of Git context, you must use Copilot Chat with the @gitlens participant. For inline completions, write a comment that references the change, such as // refactored in commit a1b2c3. Copilot can then use that comment to generate relevant code.

GitLens Annotations Overlap With Copilot Suggestions

When inline blame is enabled, the line may show both the GitLens annotation and a Copilot ghost text suggestion. This can be visually confusing. To reduce overlap, set GitLens: Inline Blame to After Line instead of Over Line. This places the blame text to the right of the line, leaving the left side clear for Copilot suggestions.

GitHub Copilot Alone vs GitHub Copilot With GitLens: Key Differences

Item GitHub Copilot Alone GitHub Copilot With GitLens
Code completion context Open file, comments, surrounding code Same plus inline blame and commit messages visible in the editor
Git history in chat No Git-specific participant available @gitlens /explain provides commit history for selected code
Code review support Manual look up of Git blame Inline blame shows author and date automatically
Test generation accuracy Based only on current file content Can reference diff context from GitLens explorer
Learning curve Low Medium — requires knowing the @gitlens participant

The main advantage of adding GitLens is the ability to query Git history without leaving the editor. Copilot alone cannot answer questions about why a change was made. GitLens fills that gap with the @gitlens chat participant.

Use the combination when you work on codebases with frequent commits, multiple authors, or long-lived feature branches. For solo projects with simple history, Copilot alone may be sufficient.

To get the most out of the integration, learn the @gitlens chat commands. Type @gitlens /help in Copilot Chat to see the full list. The most useful commands are /explain, /blame, and /log. Each one returns Git context that helps you evaluate Copilot’s suggestions.

ADVERTISEMENT