GitHub Copilot: Revolutionizing Code Generation with AI Assistance

Introduction

GitHub Copilot is an innovative code generation tool powered by artificial intelligence (AI) that is transforming the way developers write code. Developed by GitHub in collaboration with OpenAI, GitHub Copilot aims to enhance developer productivity by providing intelligent code suggestions and completing entire lines of code based on context and existing patterns.

In this article, we will explore the capabilities of GitHub Copilot and its potential impact on the software development landscape.

How Does GitHub Copilot Work?

GitHub Copilot leverages OpenAI's GPT (Generative Pre-trained Transformer) language model, specifically fine-tuned on a vast amount of publicly available code from GitHub repositories. With this extensive code knowledge, Copilot understands context and can suggest relevant code snippets, function calls, or even entire functions assist developers as they write code.

How to Register?

To register for GitHub Copilot, follow these steps:

  1. Visit the GitHub Copilot website: Go to the GitHub Copilot website at https://copilot.github.com/.

  2. Start my free trial and update the card information needed for the payment purpose from the next month's cycle.

  3. Sign in to your GitHub account: Click on the "Sign in with GitHub" button to sign in to your GitHub account. If you don't have a GitHub account, you must create one by clicking on the "Sign up" link and following the account creation process.

  4. Provide your email address: You may be prompted to enter your email address to receive updates and notifications regarding your GitHub Copilot access.

  5. Receive access invitation: When you receive an invitation, you will be notified via email. The email will contain instructions on how to access and enable GitHub Copilot in your development environment.

  6. Set up GitHub Copilot: Follow the instructions provided in the invitation email to set up GitHub Copilot in your preferred development environment. GitHub Copilot integrates with popular code editors such as Visual Studio Code.

  7. Start using GitHub Copilot: Once you have completed the setup process, you can start using GitHub Copilot to assist you with code generation and suggestions as you write code.

Pricing

  • Individuals: $10 per month or $100 per year.

  • Business: $19 per user per month.

GitHub Copilot for Individuals is available to all developers, including students and maintainers of popular open-source projects. GitHub Copilot for Business is available to organizations with 20 or more users.

GitHub Copilot

How to install GitHub Copilot

In this, I will show you how I used it in Visual Studio Code editor, but you can use it with others too.

  1. Install Visual Studio Code: If you haven't already, download and install Visual Studio Code from the official website (https://code.visualstudio.com/) and follow the installation instructions for your operating system.

  2. Install the GitHub Copilot extension: Open Visual Studio Code and navigate to the Extensions view by clicking on the square icon on the left sidebar or by pressing Ctrl+Shift+X (or Cmd+Shift+X on macOS). Search for "GitHub Copilot" in the extensions marketplace, and click on the "Install" button next to the GitHub Copilot extension. Wait for the installation process to complete.

    GitHub Copilot

  3. Sign in to GitHub: After installing the extension, you must sign in to your GitHub account within Visual Studio Code. Click on the GitHub icon on the left sidebar to open the GitHub panel. If you don't see the panel, you can open it by clicking on the square icon at the bottom left corner of the window. Follow the prompts to sign in to your GitHub account.

  4. Enable GitHub Copilot: Once you're signed in, you need to enable GitHub Copilot. Open the command palette by pressing Ctrl+Shift+P (or Cmd+Shift+P on macOS) and type "GitHub Copilot: Enable" to enable the extension. Select the option to enable GitHub Copilot when it appears in the dropdown menu.

  5. Start using GitHub Copilot: With GitHub Copilot enabled, you can now start using it in your coding workflow. As you write in Visual Studio Code, GitHub Copilot will analyze the context and provide intelligent code suggestions and completions. These suggestions will appear in the IntelliSense dropdown and can be inserted by pressing Enter or selecting the desired suggestion.

Usage of GitHub Copilot with typescript

Problem Statement

 

To create a function that will get the data from fetch api and convert it into json format.

Steps

  1. Create an index.ts file in the visual studio code and open it.

  2. Add the following syntax on the file for which you want to generate the code,\; In this case, create a function that will get the data from fetch api and convert it into json format.

    GitHub Copilot

  3. Then After you clear enter, it will provide you the code highlighted in grey, which you can select by pressing the tab.

  4. Again press enter to complete the whole function creation.

    GitHub Copilot

  5. You can then select the whole code with a tab and use it.

In the same way, we can write and generate code based on the comments we type.

Usage of GitHub Copilot with SPFx

Problem Statement

 

We have an SPFx project set up, and we want to create a SharePoint service class that can create the CRUD methods for the list.

Steps

  1. create a file under the service folder in the SPFx framework and write down the comment as  "//create a SharePoint service class to get the list items from the SharePoint list".

    GitHub Copilot

  2. GitHub Copilot will suggest you the class service to be created below.

    GitHub Copilot

  3. Then you can press the tab to auto-complete it and press enter to complete the definition of the service. As in below, it will create the constructor, which will take the web part context and utilize it to call the methods.

    GitHub Copilot

  4. Then we will press the tab to accept the suggestion and press enter to go to the next line. Then it will create the getItems function by providing code suggestions.

    GitHub Copilot

Similarly, as above, it can create all the methods for crud operation. We can also use the PNP library to achieve the same functionality or any other code restructuring to be done in SPFx.

Bonus Tip

You can install the GitHub Copilot Labs extension from the Visual Studio code to check the experimental features of the Copilot.

GitHub Copilot

  1. Explain: In this, you can select your code and ask AI to explain what the code is doing.
  2. Language Translation: GitHub Copilot can assist with translating code snippets or comments from one programming language to another, making it easier to work with multilingual codebases.
  3. Brushes: Brushes in GitHub Copilot provide interactive code editing experiences for specific tasks, such as creating HTML templates, generating database queries, building regular expressions, and streamlining development workflows.
  4. Test Generation: GitHub Copilot can help generate test cases and test code, aiding developers in automating testing processes and improving code quality by identifying potential edge cases and scenarios to consider.

The Future of GitHub Copilot

GitHub Copilot represents a significant step towards integrating AI into software development workflows. As the underlying AI models continue to improve, Copilot has the potential to become an invaluable assistant to developers, enabling them to focus on higher-level tasks and reducing the cognitive load associated with writing repetitive or boilerplate code.

As developers provide feedback and the AI models learn from their coding styles and preferences, Copilot will become more adept at aligning with individual developer needs.