Skip to content

Develop with AI-powered code suggestions using GitHub Copilot and VS Code

License

Notifications You must be signed in to change notification settings

skills/copilot-codespaces-vscode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Develop With AI Powered Code Suggestions Using GitHub Copilot and VS Code

GitHub Copilot can help you code by offering autocomplete-style suggestions. You can learn how GitHub Copilot works, and what to consider while using GitHub Copilot. GitHub Copilot analyzes the context in the file you are editing, as well as related files, and offers suggestions from within your text editor. GitHub Copilot is powered by OpenAI Codex, a new AI system created by OpenAI.

  • Who this is for: Developers, DevOps Engineers, Software development managers, Testers
  • What you'll learn: How to install Copilot into a Codespace, accept suggestions from code, accept suggestions from comments
  • What you'll build: Javascript files that will have code generated by Copilot AI for code and comment suggestions
  • Prerequisites: To use GitHub Copilot you must have an active GitHub Copilot subscription. Sign up for 60 days free Copilot
  • Timing: This course is four steps long and can be completed in under an hour

How to start this course

start-course

  1. Right-click Start course and open the link in a new tab.
  2. In the new tab, most of the prompts will automatically fill in for you.
    • For owner, choose your personal account or an organization to host the repository.
    • We recommend creating a public repository, as private repositories will use Actions minutes.
    • Scroll down and click the Create repository button at the bottom of the form.
  3. After your new repository is created, wait about 20 seconds, then refresh the page. Follow the step-by-step instructions in the new repository's README.

Step 1: Leverage Codespaces with VS Code for Copilot

Welcome to " Develop With AI Powered Code Suggestions Using GitHub Copilot and VS Code"! 👋

GitHub Copilot is an AI pair programmer that helps you write code faster and with less work. It draws context from comments and code to suggest individual lines and whole functions instantly. GitHub Copilot is powered by OpenAI Codex, a generative pretrained language model created by OpenAI.

Copilot works with many code editors including VS Code, Visual Studio, JetBrains IDE, and Neovim.

Additionally, GitHub Copilot is trained on all languages that appear in public repositories. For each language, the quality of suggestions you receive may depend on the volume and diversity of training data for that language.

Using Copilot inside a Codespace shows just how easy it is to get up and running with GitHub's suite of Collaborative Coding tools.

Note This skills exercise will focus on leveraging GitHub Codespace. It is recommended that you complete the GitHub skill, Codespaces, before moving forward with this exercise.

⌨️ Activity: Enable Copilot inside a Codespace

We recommend opening another browser tab to work through the following activities so you can keep these instructions open for reference.

Before you open up a codespace on a repository, you can create a development container and define specific extensions or configurations that will be used or installed in your codespace. Let's create this development container and add copilot to the list of extensions.

  1. Navigating back to your Code tab of your repository, click the Add file drop-down button, and then click Create new file.

  2. Type or paste the following in the empty text field prompt to name your file.

    .devcontainer/devcontainer.json
    
  3. In the body of the new .devcontainer/devcontainer.json file, add the following content:

    {
        // Name this configuration
        "name": "Codespace for Skills!",
        "customizations": {
            "vscode": {
                "extensions": [
                    "GitHub.copilot"
                ]
            }
        }
    }
    
  4. Select the option to Commit directly to the main branch, and then click the Commit new file button.

  5. Navigate back to the home page of your repository by clicking the Code tab located at the top left of the screen.

  6. Click the Code button located in the middle of the page.

  7. Click the Codespaces tab on the box that pops up.

  8. Click the Create codespace on main button.

    Wait about 2 minutes for the codespace to spin itself up.

  9. Verify your codespace is running. The browser should contain a VS Code web-based editor and a terminal should be present such as the below: Screen Shot 2023-03-09 at 9 09 07 AM

  10. The copilot extension should show up in the VS Code extension list. Click the extensions sidebar tab. You should see the following: Screen Shot 2023-03-09 at 9 04 13 AM

Wait about 60 seconds then refresh your repository landing page for the next step.

Step 2: Seeing AI code suggestions in a Javascript file!

Nice work! 🎉 You created a Codespace using a devcontainer file that installed Copilot!

GitHub Copilot provides suggestions for numerous languages and a wide variety of frameworks, but works especially well for Python, JavaScript, TypeScript, Ruby, Go, C# and C++. The following samples are in JavaScript, but other languages will work similarly.

Let's try this out utilizing Javascript for Copilot.

⌨️ Activity: Add a Javascript file and start writing code

  1. From inside the codespace in the VS Code explorer window, create a new file.

Note: If you closed the Codespace from above, please open it back up or create a new Codespace.

  1. Name the file skills.js

  2. Verify your new file looks like: Screen Shot 2023-03-09 at 9 21 34 AM

  3. In the skills.js file, type the following function header.

    function calculateNumbers(var1, var2)
    

    GitHub Copilot will automatically suggest an entire function body in grayed text. Below is an example of what you'll most likely see, but the exact suggestion may vary. Screen Shot 2023-04-27 at 10 23 06 AM

  4. Press Tab to accept the suggestion.

⌨️ Activity: Push code to your repository from the codespace

  1. Use the VS Code terminal to add the skills.js file to the repository:

    git add skills.js
    
  2. Next from the VS code terminal stage and commit the changes to the repository:

    git commit -m "Copilot first commit"
    
  3. Finally from the VS code terminal push to code to the repository:

    git push
    

Wait about 60 seconds then refresh your repository landing page for the next step.

Step 3: View the GitHub Copilot tab with multiple suggestions

Nice work! You just used AI code suggestions within a Javascript file by using GitHub Copilot ✨

Keep in mind that as you continue to use copilot, you may not want some of the suggestions GitHub Copilot offers. GitHub Copilot will show you multiple suggestions in a new tab.

⌨️ Activity: Pull the latest code to the Codespace repo.

Note Pull MUST be done prior to the next activity.

  1. Use the VS Code terminal to pull the latest code:

    git pull
    

⌨️ Activity: Add another Javascript method and view all suggestions

  1. From inside the codespace in the VS Code explorer window, create a new file. Note: If you closed the Codespace from above please open it back up or create a new Codespace.
  2. Name the file member.js
  3. In the member.js file, type the following function header.
    function skillsMember()
    
  4. Stop typing and view the Copilot suggestion by hovering over the red squiggly and select the ...
  5. Click Open Completions Panel. Copilot will synthesize around 10 different code suggestions. You should see something like this: Screen Shot 2023-04-27 at 10 06 55 AM
  6. Find a solution you like and click Accept Solution.
  7. Your member.js file will be updated with your solution.

⌨️ Activity: Push code to your repository from the codespace

  1. Use the VS Code terminal to add the member.js file to the repository:

    git add member.js
    
  2. Next from the VS code terminal stage and commit the changes to the repository:

    git commit -m "Copilot second commit"
    
  3. Finally from the VS code terminal push to code to the repository:

    git push
    

Wait about 60 seconds then refresh your repository landing page for the next step.

Step 4: Using comments to generate code with Copilot

Nicely done utilizing the Copilot tab! 🥳

You now have leveraged the Copilot quick tab auto-suggest as well as the Copilot hub to accept AI generated suggestions.

Now lets see how you can leverage comments to generate Copilot suggestions!

⌨️ Activity: Pull the latest code to the Codespace repo.

Note Pull MUST be done prior to the next activity.

  1. Use the VS Code terminal to pull the latest code:

    git pull
    

⌨️ Activity: Generate Copilot suggested code from comments.

  1. From inside the codespace in the VS Code explorer window, create a new file. (If you closed the Codespace from above, please open it back up or create a new Codespace.)

  2. Name the file comments.js.

  3. Type the following comment into the file:

    // Create web server
    
  4. Press enter to go to a new line.

  5. Copilot will suggest a code block.

  6. Hover over the red squggly and select the ...

    Note If you don't see the copilot code block suggestion or the red squiggly and the three dots ..., you can type control + enter to bring up the GitHub Copilot completions panel.

  7. Click Open Completions Panel. Copilot will synthesise around 10 different code suggestions. You should see somethig like this: Screen Shot 2023-04-25 at 3 59 42 PM

  8. Find a solution you like and click Accept Solution.

  9. Your comments.js file will be updated with your solution.

⌨️ Activity: Push code to your repository from the codespace

  1. Use the VS Code terminal to add the comments.js file to the repository:

    git add comments.js
    
  2. Next from the VS code terminal stage and commit the changes to the repository:

    git commit -m "Copilot third commit"
    
  3. Finally from the VS code terminal push to code to the repository:

    git push
    

Wait about 60 seconds then refresh your repository landing page for the next step.

Finish

Congratulations friend, you've completed this course!

celebrate

Here's a recap of all the tasks you completed:

  • Set up Copilot inside a Codespace.
  • Use Copilot to accept suggested code.
  • Use Copilot's hub for alternate suggestions.
  • Leverage comments to have Copilot auto-suggest code.

Additional learning and resources

What's next?


Get help: Post in our discussion boardReview the GitHub status page

© 2022 GitHub • Code of ConductMIT License

About

Develop with AI-powered code suggestions using GitHub Copilot and VS Code

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks