How To Use GitHub Copilot - Prompts, Tips, and Use Cases
How To Use GitHub Copilot - Prompts, Tips, and Use Cases
Generative AI coding tools are transforming the way developers approach daily coding tasks. From documenting our codebases to generating unit tests, these
tools are helping to accelerate our workflows. However, just like with any emerging tech, there’s always a learning curve. As a result, developers—beginners and
experienced alike— sometimes feel frustrated when AI-powered coding assistants don’t generate the output they want. (Feel familiar?)
For example, when asking GitHub Copilot to draw an ice cream cone using p5.js, a JavaScript library for creative coding, we kept receiving irrelevant
suggestions—or sometimes no suggestions at all. But when we learned more about the way that GitHub Copilot processes information, we realized that we had
to adjust the way we communicated with it.
When we adjusted our prompt, we were able to generate more accurate results:
We’re both developers and AI enthusiasts ourselves. I, Rizel, have used GitHub Copilot to build a browser extension, rock, paper, scissors game, and to send a
Tweet. And I, Michelle, launched an AI company in 2016. We’re both developer advocates at GitHub and love to share our top tips for working with GitHub
Copilot.
In the context of generative AI coding tools, a prompt can mean different things, depending on whether you’re asking machine learning (ML) researchers
who are building and fine-tuning these tools, or developers who are using them in their IDEs.
For this guide, we’ll define the terms from the point of view of a developer who’s using a generative AI coding tool in the IDE. But to give you the full
picture, we also added the ML researcher definitions below in our chart.
Developer Code blocks, individual lines of code, or natural Providing instructions or Details that are provided by a developer to
language comments a developer writes to comments in the IDE to generate specify the desired output from a generative AI
generate a specific suggestion from GitHub Copilot specific coding suggestions coding tool
ML Compilation of IDE code and relevant context Creating algorithms that will Details (like data from your open files and code
researcher (IDE comments, code in open files, etc.) that is generate prompts (compilations of you’ve written before and after the cursor) that
continuously generated by algorithms and sent to IDE code and context) for a large algorithms send to a large language model
the model of a generative AI coding tool language model (LLM) as additional information about the code
When prompting GitHub Copilot, think of the process as having a conversation with someone: How should I break down the problem so we can solve it
together? How would I approach pair programming with this person?
read://https_github.blog/?url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fgithub.blog%2F2023-06-20-how-to-write-better-prompts-for-github-copilot%2F 1/4
1/24/24, 7:24 PM How to use GitHub Copilot: Prompts, tips, and use cases
For example, when building a markdown editor in Next.jst, we could write a comment like this
/*
Create a basic markdown editor in Next.js with the following features:
- Use react hooks
- Create state for markdown with default text "type markdown here"
- A text area where users can write markdown
- Show a live preview of the markdown text as I type
- Support for basic markdown syntax like headers, bold, italics
- Use React markdown npm package
- The markdown text and resulting HTML should be saved in the component's state and updated in real time
*/
This will prompt GitHub Copilot to generate the following code and produce a very simple, unstyled but functional markdown editor in less than 30 seconds. We
can use the remaining time to style the component:
Note: this level of detail helps you to create a more desired output, but the results may still be non-deterministic. For example, in the comment, we prompted
GitHub Copilot to create default text that says “type markdown here” but instead it generated “markdown preview” as the default words.
2. Make your ask simple and specific. Aim to receive a short output from GitHub Copilot.
Once you communicate your main goal to the AI pair programmer, articulate the logic and steps it needs to follow for achieving that goal. GitHub Copilot
better understands your goal when you break things down. (Imagine you’re writing a recipe. You’d break the cooking process down into discrete steps–not write
a paragraph describing the dish you want to make.)
Let GitHub Copilot generate the code after each step, rather than asking it to generate a bunch of code all at once.
Here’s an example of us providing GitHub Copilot with step-by-step instructions for reversing a function:
console.log(mappedData);
console.log(mappedData);
console.log(mappedData);
read://https_github.blog/?url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fgithub.blog%2F2023-06-20-how-to-write-better-prompts-for-github-copilot%2F 2/4
1/24/24, 7:24 PM How to use GitHub Copilot: Prompts, tips, and use cases
console.log(mappedData);
// Results: ['John', 'Jane', 'Bob']
Read more about common approaches to AI training, such as zero-shot, one-shot, and few-shot learning.
For example, the prompt below is vague. It doesn’t provide any context or boundaries for GitHub Copilot to generate relevant suggestions.
# Write some code for grades.py
We iterated on the prompt to be more specific, but we still didn’t get the exact result we were looking for. This is a good reminder that adding specificity to your
prompt is harder than it sounds. It’s difficult to know, from the start, which details you should include about your goal to generate the most useful suggestions
from GitHub Copilot. That’s why we encourage experimentation.
The version of the prompt below is more specific than the one above, but it doesn’t clearly define the input and output requirements.
# Implement a function in grades.py to calculate the average grade
We experimented with the prompt once more by setting boundaries and outlining what we wanted the function to do. We also rephrased the comment so the
function was more clear (giving GitHub Copilot a clear intention to verify against).
GitHub Copilot uses a technique called neighboring tabs that allows the AI pair programmer to contextualize your code by processing all of the files open in
your IDE instead of just the single file you’re working on. However, it’s not guaranteed that GItHub Copilot will deem all open files as necessary context for
your code.
For example, here we used a descriptive function name and followed the codebase’s patterns of leveraging snake case.
def authenticate_user(username, password):
Compare this to the example below, where we introduced an inconsistent coding style and poorly named our function.
def rndpwd(l):
Instead of suggesting code, GitHub Copilot generated a comment that said, “Code goes here.”
def rndpwd(l):
# Code goes here
Stay smart
The LLMs behind generative AI coding tools are designed to find and extrapolate patterns from their training data, apply those patterns to existing language, and
then produce code that follows those patterns. Given the sheer scale of these models, they might generate a code sequence that doesn’t even exist yet. Just as
you would review a colleague’s code, you should always assess, analyze, and validate AI-generated code.
A practice example
Try your hand at prompting GitHub Copilot to build a browser extension.
To get started, you’ll need to have GitHub Copilot installed and open in your IDE. We also have access to an early preview of GitHub Copilot chat, which is
what we’ve been using when we have questions about our code. If you don’t have GitHub Copilot chat, sign up for the waitlist. Until then you can pair GitHub
Copilot with ChatGPT.
read://https_github.blog/?url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fgithub.blog%2F2023-06-20-how-to-write-better-prompts-for-github-copilot%2F 3/4
1/24/24, 7:24 PM How to use GitHub Copilot: Prompts, tips, and use cases
read://https_github.blog/?url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fgithub.blog%2F2023-06-20-how-to-write-better-prompts-for-github-copilot%2F 4/4