GitHub Copilot Tutorial

GitHub Copilot Presentation

GitHub Copilot is the world’s first large-scale AI development tool that lets you write code faster and with less effort. It extracts context from comments and code to instantly suggest individual lines and entire functions.

Studies show that when GitHub Copilot enables developers to write code more quickly, they can focus on solving larger problems, stay focused longer, and feel more satisfied in their work.

OpenAI created the generative pre-trained language model behind GitHub Copilot, powered by OpenAI Codex. An extension is available for Visual Studio Code (VS Code), Visual Studio, Neovim, and the JetBrains suite of integrated development environments (IDEs).

Learning Objectives

By the end of this module, you will be able to:

  • Understand how GitHub Copilot helps you code through autocompletion-style suggestions.
  • Understand the various ways to trigger GitHub Copilot.
  • Understand the differences between GitHub Copilot Free, Pro, Business, and Enterprise.
  • Know how to configure GitHub Copilot.
  • Know how to troubleshoot issues related to GitHub Copilot.

Prerequisites

  • GitHub
  • Basic knowledge of GitHub fundamentals

GitHub Copilot, your AI-powered coding partner

It’s no secret that AI is shaking up the tech world. AI is fundamentally transforming how organizations and teams operate, acting as a catalyst that can drastically improve developers’ productivity when used and applied correctly.

By integrating AI capabilities into the development tools you already love, you can collaborate, build, test, and ship your products faster and more efficiently than ever. GitHub Copilot is a service that delivers an AI-based coding partner for developers, supporting virtually every programming language.

Recent research from GitHub and Microsoft shows developers experience significant productivity gains when they use GitHub Copilot on real-world projects and tasks. In fact, during the three years following its launch, developers reported the following benefits:

  • 46% of new code is now written by AI
  • 55% overall productivity improvement for developers
  • 74% of developers feel more focused on fulfilling work

GitHub and Microsoft developed GitHub Copilot in partnership with OpenAI. It’s powered by the OpenAI Codex system, which has extensive knowledge of how programmers write code. OpenAI Codex often outperforms GPT‑3 in code generation, thanks in part to its training on datasets rich in public source code.

GitHub Copilot is available as an extension for Visual Studio Code, Visual Studio, Vim/Neovim, and the JetBrains suite of IDEs.

Here’s a faithful English translation:

GitHub Copilot Features

GitHub Copilot has ushered in a new era of software development as an AI-powered coding partner that helps developers stay in the flow through comment- and code-based autocompletion. Yet AI-driven autocompletion is only the starting point.

Here are some features that truly make GitHub Copilot a development tool of the future. Thanks to these capabilities, GitHub Copilot is much more than just an editor—it becomes an AI Assistant easily accessible throughout the development lifecycle.

Copilot for Conversations

GitHub Copilot provides a ChatGPT-style conversational interface in the editor. This interface focuses on development scenarios and integrates natively with Visual Studio Code and Visual Studio. It’s deeply embedded in the IDE and recognizes code typed by a developer as well as any error messages that appear. A developer can receive in-depth analysis and explanations of what code blocks should do, generate unit tests, and even get bug-fix suggestions.

Copilot for Pull Requests

OpenAI’s GPT-4 adds support in GitHub Copilot for AI-powered labels in pull request descriptions via a GitHub App that organization admins and repo owners can install. GitHub Copilot auto-fills these labels based on the changed code. Developers can then review or modify the suggested descriptions.

Copilot for the CLI

After the editor and pull requests, the terminal is where developers spend the most time. Yet even the most proficient developers must scroll through many pages to remember the exact syntax of countless commands. The GitHub Copilot command-line interface (CLI) can compose commands and loops, as well as surface hidden flags for tools like find to fulfill your request.

Here’s the English translation:

Subscription Plans

The service is available through personal GitHub accounts with GitHub Copilot Free and GitHub Copilot Pro, through organizational accounts with GitHub Copilot Business, or through enterprise accounts with GitHub Copilot Enterprise.

GitHub Copilot Business

GitHub Copilot Business lets you control who has access to Copilot within your company. Once access is granted to an organization, its administrators can then assign it to individuals and teams.

With GitHub Copilot Business, Copilot is available to all developers, teams, organizations, and enterprises.

It focuses on enhancing productivity, security, and user satisfaction for organizations through features such as:

  • AI‑powered code autocompletion
  • Conversational assistance in IDEs and mobile
  • Security vulnerability filtering
  • Code citations
  • Public code filtering
  • Intellectual property indemnification
  • Professional-grade privacy and security

GitHub Copilot Enterprise

GitHub Copilot Enterprise is available to organizations via GitHub Enterprise Cloud. This subscription plan enables your developer teams to:

  • Onboard quickly to your codebase
  • Generate and search documentation
  • Receive suggestions based on internal and private code
  • Review pull requests more effectively

GitHub Copilot Enterprise includes everything in Copilot Business, plus:

  • A conversational interface integrated with GitHub to discuss the codebase
  • Action buttons throughout the platform

Copilot Enterprise can index your organization’s codebase for improved understanding and personalized suggestions. It also provides access to customize Copilot with private models to tailor code autocompletion.

Here’s the English translation:

Interacting with Copilot

This unit explores the various ways to maximize your interaction with GitHub Copilot in your development environment. By understanding the features and capabilities of this service, you’ll learn how to use it effectively.

The following sections describe different ways to invoke and use GitHub Copilot, along with examples and shortcuts to help you get the most out of it.

Inline Suggestions

The most immediate form of assistance in Copilot is inline suggestions. As you type, Copilot analyzes your code and context to offer real-time code completions. This feature predicts the next words you want to write and displays suggestions subtly and unobtrusively.

Suggestions appear as gray text ahead of your cursor.

  • To accept a suggestion, press Tab or the right arrow key (→).
  • To dismiss a suggestion, continue typing or press Esc.

Inline suggestions are especially useful for repetitive tasks or when you need quick generic code.

Here’s an example:

def calculate_average(numbers):
# Start typing here and watch Copilot suggest the function body

Command Palette

The Command Palette lets you quickly access Copilot’s various features, so you can perform complex tasks with just a few keystrokes.

  1. Open the Command Palette by pressing Ctrl + Shift + P (Windows/Linux) or Cmd + Shift + P (Mac).
  2. Type Copilot to view available commands.
  3. Select actions like “Explain this” or “Generate unit tests” to get assistance.

Copilot Chat

Copilot Chat is an interactive feature that allows you to communicate with Copilot using natural language. You can ask questions or request code snippets, and Copilot responds based on your prompts.

  1. Open the Copilot Chat panel in your IDE.
  2. Enter natural-language questions or requests, then review Copilot’s response.
  3. For example, you might ask: “How do I implement binary search in Python?” Copilot Chat is great for exploring new coding concepts or getting help with unfamiliar syntax.

Copilot may respond with:

def binary_search(arr, target):
left, right = 0, len(arr) - 1
while left <= right:
mid = (left + right) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
left = mid + 1
else:
right = mid - 1
return -1

Inline Conversations

Inline conversations allow you to interact with Copilot in-context, directly within your code editor. You can use this feature to request modifications or explanations without losing focus from your current work.

  1. Place your cursor where you need help.
  2. To open an inline conversation, press Ctrl+I (Windows/Linux) or Cmd+I (Mac).
  3. Ask questions or request specific changes for that section of code.

Inline conversations let you concentrate on a particular part of your code and receive targeted guidance.

Code Comments

Copilot uses natural language processing to transform comments into code. Simply describe the functionality you want in a comment. When you press Enter, Copilot generates code based on your description.

Here’s an example:

Function to reverse a string

def reverse_string(s):
# Copilot suggests the function body here
// Function to reverse a string

def reverse_string(s):
return s[::-1]

This approach allows you to write code quickly, especially when the task is simple.

Multiple suggestions
For more complex code snippets, Copilot can propose several alternatives.

  • When Copilot offers a suggestion, look for the lightbulb icon.
  • To cycle through options, click the icon or press Alt + ] (Windows/Linux) or Option + ] (Mac).
  • Multiple suggestions help you explore different coding approaches and choose the most suitable one.

Explanations
Understanding existing code is crucial, especially in large projects. To get explanations for code snippets, use the “Explain this” feature.

  1. Select a block of code.
  2. Right-click the selection and choose Copilot: Explain this from the context menu.
  3. Read the explanation provided by Copilot for the selected code.

This feature is useful for learning and when reviewing others’ code.

Automated test generation
Unit tests are essential for ensuring code quality and reliability. Copilot can save you time and effort by generating unit tests for your functions or classes.

  1. Select a function or class.
  2. Open the command palette and select Copilot: Generate unit tests.
  3. Review the suggested test cases for your code.

Here’s an example:

def add(a, b):
return a + b

Copilot might generate a test like this:

def test_add():
assert add(2, 3) == 5
assert add(-1, 1) == 0
assert add(0, 0) == 0

Automated test generation helps you maintain code integrity and catch bugs early in the development process.

Remember that Copilot learns from context. It provides more accurate and relevant assistance when your code is well-structured and commented. The more you interact with Copilot, the better it understands your coding style and preferences.

Install, configure, and troubleshoot GitHub Copilot

This unit explains how to sign up for GitHub Copilot, how to configure it using VS Code, and how to troubleshoot GitHub Copilot issues in VS Code.

Sign up for GitHub Copilot

Before you can start using GitHub Copilot, you must set up a free trial or subscription for your account.

  1. First, click your GitHub profile picture and select Settings.
  2. Under Code, planning, and automation, you’ll find Copilot.
  3. After signing up, install the appropriate extension for your preferred environment. GitHub Copilot supports GitHub.com (no extension needed), VS Code, Visual Studio, JetBrains IDEs, and Neovim via a lightweight extension.
  4. This module focuses on reviewing the VS Code extensions and settings. The exercise in the next unit uses VS Code.

If you use another editor, you’ll find environment-specific setup links in the References section at the end of this module.

Configure GitHub Copilot in VS Code

Install the VS Code extension

  1. In the Visual Studio Marketplace, visit the GitHub Copilot extension page and click Install.
  2. A prompt will appear asking to open VS Code—click Open.
  3. In VS Code, under the Extensions tab for GitHub Copilot, click Install.
  4. If VS Code hasn’t been authorized in your GitHub account yet, you’ll be prompted to Sign in to GitHub.
  5. Once installed, GitHub Copilot will provide inline code completion. You can enable/disable it or customize advanced settings in VS Code.

Enable or disable GitHub Copilot in VS Code

  • In the status bar at the bottom of the VS Code window, click the Copilot status icon, then select Enable or Disable.
  • If you disable Copilot, VS Code will ask whether to turn off suggestions globally or only for the current language:
    • Select Disable Completions to turn off globally.
    • Select Disable Completions for LANGUAGE to disable it just for the current file’s language.

Enable or disable inline suggestions in VS Code

  1. Go to the File menu → PreferencesSettings.

2. In the left-hand pane of the Settings tab, select Extensions, then GitHub Copilot.

Under Editor: Enable Completions, check or uncheck the box to enable or disable inline suggestions.

You can also choose to enable or disable inline suggestions and specify the languages for which you want GitHub Copilot to be active.

Troubleshooting GitHub Copilot in VS Code
In VS Code, log files are essential for diagnosing connection issues. The GitHub Copilot extension stores its logs in VS Code’s standard extension log location. You can access them by opening the Command Palette and selecting:

  • Developer: Open Log File…
  • or Developer: Open Extension Log Folder

In rare cases, errors may not appear in the usual log locations. If you encounter issues and no logs are found, try viewing the Electron logs of the VS Code process. Access them via:
Help → Toggle Developer Tools in VS Code.

Network restrictions, firewalls, or proxies can also block connection to GitHub Copilot. If a problem arises, follow these steps to gather diagnostic data you can inspect or send to support:

  1. Open the VS Code Command Palette:
    • Mac: Shift + ⌘ + P
    • Windows/Linux: Ctrl + Shift + P
  2. Type Diagnostics, then select GitHub Copilot: Collect Diagnostics from the list.

For troubleshooting guidance in other environments, see the References section at the end of this module.

Exercise: Develop AI-Powered Code Suggestions Using GitHub Copilot and VS Code

This exercise guides you through the following steps:

  • Install GitHub Copilot using GitHub Codespaces
  • Request code suggestions from GitHub Copilot
  • Accept code suggestions provided by GitHub Copilot

Instructions

  1. Click the Start exercise on GitHub button at the bottom of this page. You’ll be taken to a public GitHub starter repository inviting you to complete a series of small challenges.
  2. Click Use this template or Start course in the starter repo. This prompts you to create your own repository. We recommend creating a public repository, as private repos consume GitHub Actions minutes.After generating your repo from the template, wait about 20 seconds, then refresh the page.
  3. Follow the instructions in the repository’s README tab to understand the exercise objectives, learning goals, and how to succeed.
  4. Once you’ve completed the GitHub exercise, return to this module to access:
    • A quick knowledge check
    • A summary of what you’ve learned
    • A badge to signify module completion

Notes

You do not need to modify any files in this workflow to complete the exercise. Changing its contents may disrupt the exercise’s ability to validate your actions, provide feedback, or grade your results.

Summary

GitHub is tailoring GitHub Copilot to every team, project, and repository that uses it. The goal? To enhance the entire software development lifecycle—from writing code to submitting pull requests and beyond.

Leveraging Microsoft’s knowledge model, GitHub taps into an organization’s existing data and insights to strengthen collaboration between all employees and developer teams. These insights help seamlessly transform code ideas into reality. Meanwhile, GitHub continues innovating and updating the core of GitHub Copilot—the AI pair programmer powering this evolution.

GitHub Copilot heralds a new generation of more productive, satisfied, and fulfilled developers who deliver better software for everyone.

Now that you’ve completed this module, you should be able to:

  • Explain GitHub Copilot’s features and subscription plans (Free, Pro, Business, and Enterprise).
  • Get started with and configure GitHub Copilot.
  • Develop using GitHub Copilot and VS Code.