├── copilot-cheat-sheet-v2.md ├── img ├── copilot-prompting.webp └── github-copilot.png ├── playground.html ├── playground.js ├── playground.py └── readme.md /copilot-cheat-sheet-v2.md: -------------------------------------------------------------------------------- 1 | # Copilot Labs & Demos 2 | 3 | ![GitHub Copilot images](img/github-copilot.png) 4 | 5 | ## Introduction to GitHub Copilot 6 | 7 | ### Understand Copilot's strengths and weaknesses 8 | 9 | GitHub Copilot is an AI coding assistant that helps you write code faster and with less effort, allowing you to focus more energy on problem solving and collaboration. Before you start working with Copilot, it's important to understand when you should and shouldn't use it. 10 | 11 | Some of the things Copilot does best include: 12 | 13 | - Writing tests and repetitive code 14 | - Debugging and correcting syntax 15 | - Explaining and commenting code 16 | - Generating regular expressions 17 | 18 | Copilot is not designed to: 19 | 20 | - Respond to prompts unrelated to coding and technology 21 | - Replace your expertise and skills. Remember that you are in charge, and Copilot is a powerful tool at your service. 22 | 23 | ### Choose the right Copilot tool for the job 24 | 25 | While Copilot code completions and Copilot Chat share some functionality, the two tools are best used in different circumstances. 26 | 27 | Code completions work best for: 28 | 29 | - Completing code snippets, variable names, and functions as you write them 30 | - Generating repetitive code 31 | - Generating code from inline comments in natural language 32 | - Generating tests for test-driven development 33 | 34 | Alternatively, Copilot Chat is best suited for: 35 | 36 | - Answering questions about code in natural language 37 | - Generating large sections of code, then iterating on that code to meet your needs 38 | - Accomplishing specific tasks with keywords and skills. Copilot Chat has built-in keywords and skills designed to provide important context for prompts and accomplish common tasks quickly. Different types of keywords and skills are available in different Copilot Chat platforms. See "[Asking GitHub Copilot questions in your IDE.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-your-ide#using-keywords-in-your-prompt)" 39 | - Completing a task as a specific persona. For example, you can tell Copilot Chat that it is a Senior C++ Developer who cares greatly about code quality, readability, and efficiency, then ask it to review your code. 40 | 41 | ### Create thoughtful prompts 42 | 43 | Prompt engineering, or structuring your request so Copilot can easily understand and respond to it, plays a critical role in Copilot's ability to generate a valuable response. Here are a few quick tips you should remember while crafting your prompts: 44 | 45 | - Break down complex tasks. 46 | - Be specific about your requirements. 47 | - Provide examples of things like input data, outputs, and implementations. 48 | - Follow good coding practices. 49 | 50 | To learn more, see: 51 | 52 | - [Prompt engineering for GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot) 53 | - [Prompt Engineering Guide](https://www.promptingguide.ai/) 54 | 55 | #### Prerequisities 56 | 57 | - Latest version of Visual Studio Code 58 | - [GitHub Copilot extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) 59 | - [GitHub Copilot Chat extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat) 60 | - GitHub Copilot license 61 | - [Setup](https://docs.github.com/en/copilot/setting-up-github-copilot/setting-up-github-copilot-for-your-organization) 62 | - Other optional extension to support coding in your favourite programming langauge 63 | 64 | ## Copilot features 65 | 66 | - [GitHub Copilot](https://github.com/features/copilot) 67 | - [GitHub Copilot features](https://docs.github.com/en/copilot/about-github-copilot/github-copilot-features) 68 | - [GitHub Copilot Blogs](https://github.blog/tag/github-copilot/) 69 | 70 | ## Using Copilot 71 | 72 | - [Best practices for using GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/best-practices-for-using-github-copilot) 73 | - [How to use GitHub Copilot: What it can do and real-world examples](https://github.blog/developer-skills/github/what-can-github-copilot-do-examples/) 74 | - [Copilot Chat Cookbook](https://docs.github.com/en/copilot/example-prompts-for-github-copilot-chat) 75 | - [GitHub for Beginners: How to get LLMs to do what you want](https://github.blog/ai-and-ml/github-copilot/github-for-beginners-how-to-get-llms-to-do-what-you-want/) 76 | - [GitHub for Beginners: Essential features of GitHub Copilot](https://github.blog/ai-and-ml/github-copilot/github-for-beginners-essential-features-of-github-copilot/) 77 | - [Mastering GitHub Copilot: When to use AI agent mode](https://github.blog/ai-and-ml/github-copilot/mastering-github-copilot-when-to-use-ai-agent-mode/) 78 | 79 | ### Code suggestions 80 | 81 | Creaty empty folder, open it in VS Code, create empty javascript file (e.g. test.js) and just start typing. GitHub Copilot offers coding suggestions as you type. For example, type this function header in a JavaScript file: 82 | 83 | ```javascript 84 | function calculateDaysBetweenDates(begin, end) { 85 | ``` 86 | 87 | You can also describe something you want to do using natural language within a comment, and Copilot will suggest the code to accomplish your goal. For example, type this comment in a JavaScript file: 88 | 89 | ```javascript 90 | // write a function to calculate number of days between provided dates 91 | ``` 92 | Generate lists or dictionaries with sample data: 93 | 94 | ```javascript 95 | // Produce IO exemplars for sentiment analysis. Examples are categorized as either positive or negative. 96 | // Produce 2 negative examples and 8 positive examples. 97 | // Each example should be a string. 98 | ``` 99 | 100 | Work with regular expressions (regex): 101 | 102 | ```javascript 103 | // Create a regex to validate phone number in this format (xxx) xxx-xxxx. 104 | ``` 105 | 106 | Work with external APIs: 107 | 108 | ```javascript 109 | // Write a function to retrieve dog breeds from following API 'https://dog.ceo/api/breed/' and return an array of breed objects 110 | ``` 111 | 112 | Use @file to provide context of specific file: 113 | 114 | ```javascript 115 | // Use sample.json file to generate a strongly typed class or interface. 116 | ``` 117 | 118 | Work with SQL queries. Define data structure (e.g. struct, class) like Product(Id, Name, Price, Description) for best results. 119 | ```javascript 120 | //Generate sql to insert Product object into database. 121 | ``` 122 | 123 | 124 | Unit testing: 125 | ```javascript 126 | // test caclulator function 127 | // create unit test for a calculator function using assert/jest framework 128 | ``` 129 | 130 | #### Code suggestions in IDE - basics 131 | 132 | - Suggestions appear as you type. 133 | - Accept whole suggestion with Tab. 134 | - Accept word by word Ctrl + ->. 135 | - Navigate through multpile solutions (Completion Panel) with Ctrl + Enter. 136 | - Guide Copilot with **instructions within the function** to provide conditions, assertions and so on. 137 | - Inline chat Ctrl + I might be better than "ghost text" in many cases. 138 | - Don't recreate common prompts - use **slash commands**. 139 | - Don't forget about context. 140 | 141 | #### Essential keyboard shortcuts: 142 | 143 | Reference: [GitHub Copilot in VS Code cheat sheet](https://code.visualstudio.com/docs/copilot/copilot-vscode-features) 144 | 145 | Essential keyboard shortcuts: 146 | 147 | - Tab - Accept suggestion 148 | - Alt + ] - Next suggestion 149 | - Alt + [ - Previous suggestion 150 | 151 | More infromation: 152 | - [Getting code suggestions in your IDE with GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/getting-code-suggestions-in-your-ide-with-github-copilot) 153 | 154 | ### Chat in IDE 155 | 156 | Press Ctrl+Alt+I to open the chat window 157 | 158 | Press Ctrl+I to open the inline chat 159 | 160 | #### Chat participants 161 | 162 | Chat participants are AI domain experts that can perform tasks or answer questions in a specific domain. 163 | 164 | Chat participants include: 165 | 166 | - @workspace: Has context about the code in your workspace. Use @workspace when you want Copilot to consider the structure of your project, how different parts of your code interact, or design patterns in your project. 167 | - @vscode: Has context about Visual Studio Code commands and features. Use @vscode when you want help with Visual Studio Code. 168 | - @terminal: Has context about the Visual Studio Code terminal shell and its contents. Use - @terminal when you want help creating or debugging terminal commands. 169 | - @azure: Has context about Azure services and how to use, deploy and manage them. Use @azure when you want help with Azure. The @azure chat participant is currently in public preview and is subject to change. 170 | 171 | #### Slash commands 172 | 173 | Use slash commands to avoid writing complex prompts for common scenarios. To use a slash command, type / in the chat prompt box, followed by a command. Slash commands include: 174 | 175 | - /tests: Generate unit tests for the selected code 176 | - /fix: Propose a fix for problems in the selected code 177 | - /explain: Explain the selected code 178 | - /clear: Start a new chat 179 | 180 | 181 | #### Chat variables 182 | 183 | Use chat variables to include specific context in your prompt. To use a chat variable, type # in the chat prompt box, followed by a chat variable. Chat variables include: 184 | 185 | - #file: Include a specific file as context in the chat. 186 | - #git: Include information about the current Git repository. 187 | - #terminalLastCommand: Include the last run command in the active Visual Studio Code terminal. 188 | 189 | #### GitHub skills for Copilot 190 | 191 | Copilot's GitHub-specific skills expand the type of information Copilot can provide. To access these skills in Copilot Chat in VS Code, include @github in your question. 192 | 193 | You can generate a list of currently available skills by asking Copilot: @github What skills are available? 194 | 195 | | Skill | Description | Enabled by default? | Example question | 196 | |--------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------| 197 | | Commit details | Retrieves a list of commits, or the contents of a specific commit, to provide answers to commit-related questions. | Yes | @github Explain the changes in the code of this commit | 198 | | Discussion details | Retrieves a specific GitHub discussion. This is useful for quickly getting the gist of the conversation in a discussion. | Yes | @github Summarize this discussion LINK-TO-DISCUSSION | 199 | | File details | Retrieves a specific file in the default branch of the Git repository, allowing you to ask questions about the file and the recent changes made to it. This skill is useful when you provide the exact path of a file in the repository. | Yes | @github What logic does user_auth.js encapsulate? @github What is the file history of user_auth.js? | 200 | | GitHub Advanced Security | Retrieves information about security alerts within your organization from GitHub Advanced Security features (code scanning, secret scanning, and Dependabot alerts). | Yes | @github How would I fix this code scanning alert? | 201 | | Issue details | Retrieves a specific GitHub issue, including the issue's title, number, author, status, body, linked pull requests, comments, and timestamps. | Yes | @github List issues assigned to me | 202 | | Lexical code search | Keyword code search in the default branch of the Git repository. This skill is useful when you want to know about specific functions, methods or keywords that exist in the code. This skill leverages most of the functionality available to GitHub Search like symbol and path. | Yes | Find me the tests for the GitService class | 203 | | Path search | Retrieves a specific file in the default branch of the Git repository. This skill is useful when you provide the exact path of a file in the repository. | Yes | @github What logic does user_auth.js encapsulate? | 204 | | Pull request details | Retrieves a specific pull request. This allows you to ask questions about the pull request, including getting a summary of the pull request, its comments, or the code it changes. | Yes | @github List my recent pull requests | 205 | | Release details | Retrieves the latest, or specified, release. This allows you to find out who created a release, when it happened, and information included in the release notes. | Yes | @github When was the latest release? | 206 | | Repository details | Retrieves a specific GitHub repository. This is useful for finding out details such as the repository owner and the main language used. | Yes | @github Tell me about PATH-TO-REPOSITORY | 207 | | Semantic code search | Natural language semantic code search in the default branch of the Git repository. This skill is useful when you want to know where or how certain functionality has been implemented in the code. Note: this requires indexing to be enabled for the repository (see "Indexing repositories for Copilot Chat"). | Yes Available for all repositories with a Copilot Enterprise subscription, and for public repositories with a Copilot Individual or Copilot Business subscription. | How does this repo manage HTTP requests and responses? | 208 | | Support search | Retrieves information from the GitHub Support portal. This skill is useful for asking Copilot Chat about GitHub products and support related questions. | Yes | @github Can I use Copilot knowledge bases with Copilot Individual? | 209 | | Web search | Searches the web using the Bing search engine. This skill is useful for teaching Copilot about recent events, new developments, trends, technologies, or extremely specific, detailed, or niche subjects. | No For Copilot Individual:Enable in your user settings.For Copilot Business:Enable in organization settings. | @github #web What are some recent articles about SAT tokens securing against vulnerabilities in Node? | 210 | 211 | 212 | #### Essential keyboard shortcuts: 213 | - Ctrl+Alt+I - Open the chat view 214 | - Ctrl+I - Open the inline chat 215 | 216 | More infromation: 217 | - [Asking GitHub Copilot questions in your IDE](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-your-ide) 218 | 219 | ### Chat in GitHub 220 | 221 | You can use GitHub Copilot Chat in GitHub to answer general questions about software development, or specific questions about the issues or code in a repository. 222 | 223 | > [!TIP] 224 | > Look for GitHub Copilot icon next to the search bar, in Code window or with code selection. 225 | 226 | On GitHub, you can use Copilot Chat to ask: 227 | 228 | - General software-related questions, without a particular context. For more information, see "[Asking a general question about software development.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-a-general-question-about-software-development)" 229 | 230 | - Examples: 231 | - What are the advantages of the Go programming language? 232 | - What is Agile software development? 233 | - What is the most popular JavaScript framework? 234 | - Give me some examples of regular expressions. 235 | - Write a bash script to output today's date. 236 | 237 | - Exploratory questions asked in the context of a specific repository. For more information, see "[Asking exploratory questions about a repository.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-exploratory-questions-about-a-repository)" 238 | 239 | - Examples: 240 | - When was the most recent release? 241 | - Where is rate limiting implemented in our API? 242 | - How does the WidgetFactory class work? 243 | - Where is the code for updating a phone number? 244 | - Where are SAT tokens generated? 245 | - Show the most recently updated issues assigned to USERNAME 246 | - List open issues about SUBJECT 247 | - What was the last merged PR by USERNAME 248 | - What are the latest commits to the main branch by USERNAME 249 | 250 | - Questions asked in the context of a specific repository, file or symbol. For more information, see "[Asking a question about a specific file or symbol.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-a-question-about-a-specific-file-or-symbol)" 251 | 252 | - Questions about a specific file or specified lines of code within a file. For more information, see "[Asking questions about specific pieces of code.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-questions-about-specific-pieces-of-code)" 253 | 254 | - For example, if you are asking about the entire file, you could enter: 255 | - Explain this file. 256 | - How could I improve this code? 257 | - How can I test this script? 258 | - If you are asking about specific lines, you could enter: 259 | - Explain the function at the selected lines. 260 | - How could I improve this class? 261 | - Add error handling to this code. 262 | - Write a unit test for this method. 263 | 264 | - Questions about a pull request diff. For more information, see "[Finding out about the changes in a pull request.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-questions-about-a-specific-pull-request)" 265 | 266 | - Examples: 267 | - Summarize this PR for me. 268 | - Summarize the comments in this PR. 269 | - Summarize the changes in this PR. 270 | 271 | - Questions about a specific issue. For more information, see "[Asking a question about a specific issue or discussion.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-a-question-about-a-specific-issue-or-discussion)" 272 | 273 | - Examples: 274 | - Explain this issue 275 | - Summarize this discussion 276 | - Recommend next steps for this issue 277 | - What are the acceptance criteria for this issue? 278 | - What are the main points made by PERSON in this discussion? 279 | 280 | - Questions about GitHub Advanced Security alerts. For more information, see "[Asking questions about GitHub Advanced Security alerts.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github#asking-questions-about-github-advanced-security-alerts)" 281 | 282 | - Examples 283 | - How would I fix this alert? 284 | - How many alerts do I have on this pull request? 285 | - Which line of code is this code scanning alert referencing? 286 | - What library is affected by this Dependabot alert? 287 | 288 | More infromation: 289 | - [Asking GitHub Copilot questions in GitHub](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github) 290 | 291 | ### Chat in Mobile 292 | 293 | GitHub Copilot Chat is a chat interface that lets you ask and receive answers to coding-related questions in GitHub Mobile. 294 | Copilot Chat in GitHub Mobile can help you with a variety of coding-related tasks, like offering you code suggestions, providing natural language descriptions of a piece of code's functionality and purpose, generating unit tests for your code, and proposing fixes for bugs in your code. 295 | 296 | In GitHub Mobile, you can use Copilot Chat to ask: 297 | 298 | - General software-related questions, without a particular context. For more information, see "[Asking a general question about software development.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github-mobile#asking-a-general-question-about-software-development)" 299 | 300 | - Some examples of general questions you could ask are: 301 | - What are the advantages of the Go programming language? 302 | - What is Agile software development? 303 | - What is the most popular JavaScript framework? 304 | - Give me some examples of regular expressions. 305 | - Write a bash script to output today's date. 306 | 307 | - Questions asked in the context of your project. For more information, see "[Asking questions about a specific repository.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github-mobile#asking-exploratory-questions-about-a-repository)" 308 | 309 | - For example, if you chose the repository you are working in as the context, you could ask: 310 | - What is the main purpose of this repo? What problem does it solve or what functionality does it provide? 311 | - What web frameworks are used in this project? 312 | - Where is rate limiting implemented in our API? 313 | - How is the code organized? Explain the project architecture. 314 | - Are there any specific environment requirements for working on this project? 315 | 316 | - Questions about a specific file or specified lines of code within a file. For more information, see "[Asking questions about specific pieces of code.](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github-mobile#asking-questions-about-specific-pieces-of-code)" 317 | 318 | - For example, if you are asking about the entire file, you could enter: 319 | - Explain this file. 320 | - How could I improve this code? 321 | - How can I test this script? 322 | - If you are asking about specific lines, you could enter: 323 | - How could I improve this class? 324 | - Add error handling to this code. 325 | - Write a unit test for this method. 326 | 327 | More infromation: 328 | - [Asking GitHub Copilot questions in GitHub Mobile](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github-mobile) 329 | 330 | 331 | ### GitHub Copilot in the command line 332 | 333 | To ask Copilot in the CLI to explain a command, run gh copilot explain followed by the command that you want explained. For example: 334 | 335 | ```cli 336 | gh copilot explain "sudo apt-get" 337 | gh copilot suggest "Undo the last commit" 338 | ``` 339 | 340 | 341 | More infromation: 342 | - [Using GitHub Copilot in the command line](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-in-the-command-line) 343 | 344 | ### Copilot in Windows Terminal 345 | 346 | More infromation: 347 | - [Asking GitHub Copilot questions in Windows Terminal](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-windows-terminal) 348 | - [Terminal Chat](https://learn.microsoft.com/en-us/windows/terminal/terminal-chat#setting-up-terminal-chat) 349 | 350 | ### Using Copilot Extensions 351 | 352 | GitHub Copilot Extensions are a type of GitHub App that integrates the power of external tools into GitHub Copilot Chat. Copilot Extensions can be developed by anyone, for private or public use, and can be shared with others through the GitHub Marketplace. 353 | 354 | You can get started with Copilot Extensions in one of two ways: 355 | 356 | - Build your own Copilot Extension. See "[About building Copilot Extensions.](https://docs.github.com/en/copilot/building-copilot-extensions/about-building-copilot-extensions)" 357 | - Install a Copilot Extension from GitHub Marketplace. 358 | 359 | More infromation: 360 | - [Using extensions to integrate external tools with Copilot Chat](https://docs.github.com/en/copilot/using-github-copilot/using-extensions-to-integrate-external-tools-with-copilot-chat) 361 | 362 | ### Copilot for Pull Requests 363 | 364 | After you create a pull request, you can continue working on the PR on the GitHub website. This article is about Copilot Workspace, which provides a Copilot-enabled environment for: 365 | 366 | - Refining your pull requests 367 | - Validating changes 368 | - Integrating suggestions from reviewers 369 | 370 | Copilot Workspace enables you to work on your pull requests in one place - on GitHub - from pull request creation to merge. 371 | 372 | You can use GitHub Copilot to generate a summary of a pull request on GitHub. You can use the summary to help reviewers understand your changes, or to quickly understand the changes in a pull request you're reviewing. 373 | 374 | GitHub Copilot will scan through the pull request and provide an overview of the changes made in prose, as well as a bulleted list of changes with the files that they impact. You can generate a summary in the following places. 375 | 376 | - In the description of a new pull request you're creating 377 | - In the description of an existing pull request, by editing the opening comment 378 | - In a comment on the main timeline of a pull request 379 | 380 | More infromation: 381 | - [Using Copilot to help you work on a pull request](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-for-pull-requests/using-copilot-to-help-you-work-on-a-pull-request) 382 | - [Creating a pull request summary with GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/using-github-copilot-for-pull-requests/creating-a-pull-request-summary-with-github-copilot) 383 | 384 | ## Prompt Engineering with Copilot 385 | 386 | - Provide good **context**; giving examples and information about what you're trying to achieve 387 | - Be **specific**; if it's for a certain audience, say that 388 | - **Break down** the problem 389 | - Be **clear** in how you ask your questions. If something comes back that doesn't sound right, clarify it 390 | - **Rephrase and refine** for your prompts 391 | 392 | 393 | ### 3S Principle of Prompt Engineering 394 | 395 | - **Simple** 396 | - The more code - the more errors and hallucinations 397 | - Break your solution in simple steps 398 | - Use 'simple' word in prompt 399 | - Keep relevant files open 400 | - The best prompt is one you don't have to write 401 | - **Specific** 402 | - Don't ask open question 403 | - Use '@workspace' agent to focus on your project as a base for agents (especially in Chat window) 404 | - Use includes like '#editor', '#file', '#selection' or '#terminalLastCommand' 405 | - Always use specific context and specific prompts 406 | - **Short** 407 | - You don't need to phrase long question 408 | - You don't need to use proper grammar 409 | - Type less because typos don't matter 410 | - Command-like prompts are OK 411 | 412 | Simple, specific and short: 413 | ``` 414 | fixed header 415 | fixed footer 416 | main content scrolls between header and footer 417 | ``` 418 | Notice three short prompts insted of large one. 419 | 420 | #### Prompting Tips & Trics 421 | 422 | ##### Getting the most out of Copilot inline suggestions 423 | 424 | - **Provide context** 425 | - Keep relevant files open (source code, input files, even data files like json/xml) 426 | - For code completions, Copilot looks at the current and open files in your editor to analyze the context and create appropriate suggestion 427 | - Provide description on top of the file (E.g. This file parses xyz.json file) 428 | - Just as you would give a brief, high-level introduction to a coworker, a top level comment in the file you're working in can help Copilot understand the overall context of the pieces you are creating. 429 | - Appropriate includes and references (Provide imports/usings etc. for specific packages, namespaces or libraries) 430 | - It's best to manually set the includes or module references you need for your work. Copilot can make suggestions, but you likely know best what dependencies you need to include. This can also help let Copilot know what frameworks, libraries, and their versions you'd like it to use when crafting suggestions. 431 | - Provide specific and desriptive variable, type, function and parameter names 432 | - Using meaningful function names helps Copilot provide a body that does what you want. 433 | - Specific and well-scoped function comments (Give inline hints (in comments inside functions)) 434 | - A function name can only be so descriptive without being overly long. Function comments can help fill in details that Copilot might need to know. 435 | - Prime Copilot with sample code 436 | - One trick to get Copilot on the right page, is to copy and paste sample code that is close to what you are looking for into your open editor. Providing a small example can help Copilot generate suggestions that match the language and tasks you want to achieve. Once Copilot begins providing you with the code you want and will actually use, you can delete the sample code from the file. This can be especially helpful to jump start Copilot to a newer library version when it defaults to providing older code suggestions. 437 | - Be predictable (standard naming conventions etc.) 438 | - Be consistent and keep the quality bar high 439 | - Copilot is going to latch on to your code to generate suggestions that follow the existing pattern, so the adage "garbage in, garbage out" applies. 440 | - Always keeping a high quality bar can take discipline. Especially when you're coding fast and loose to get something working, you might want to disable Copilot completions while in "hacking" mode. You can temporarily disable completions from the Copilot status menu. Bring up the Copilot status menu dropdown by selecting the Copilot Status bar item. 441 | - Double check code against documentation 442 | - If code or syntax provided by Copilot is from older version of langauge or library - copy and paste a more up-to-date example from documentation 443 | - Start code generation by defining proper data/entity models, schemas, data structures etc. (e.g. while using MVC pattern) 444 | 445 | ##### Getting the most out of Copilot Chat 446 | 447 | - Use chat participants and slash commands 448 | - [Chat participants](https://code.visualstudio.com/docs/copilot/copilot-chat#_chat-participants) are designed to collect extra context either about a code base or a specific domain or technology. By using the appropriate participant, Copilot Chat can find and provide better information to send to the Copilot backend. For example, use @workspace if you want to ask questions about your open project, or @vscode to know more about VS Code features and APIs. 449 | - [Slash commands](https://code.visualstudio.com/docs/copilot/copilot-chat#_slash-commands) help Copilot Chat understand your intent when you ask a question. Are you learning about a code base (/explain), do you want help with fixing an issue (/fix), or are you creating test cases (/tests)? By letting Copilot Chat know what you're trying to do, it can tune its reply to your task and provide helpful commands, settings, and code snippets. 450 | - Use chat variables for context 451 | - Chat participants, such as **@workspace** or **@vscode**, can contribute chat variables that provide domain-specific context. You can reference a chat variable in your chat prompt by using the # symbol. By using a chat variable, you can be more specific about the context that you include in your chat prompt. 452 | - For example, the **#file** variable lets you reference specific files from your workspace in your chat prompt. This helps make the answers from Copilot Chat more relevant to your code by providing context about the file you are working with. You can ask questions like "Can you suggest improvements to #file:package.json?" or "How do I add an extension in #file:devcontainer.json?". By using the #file variable, you can get more targeted and accurate responses from Copilot. 453 | - Be specific and keep it simple 454 | - When you ask Copilot to do something, be specific in your ask and break down a large task into separate, smaller tasks. For example, don't ask Copilot to create an Express app, that uses TypeScript and Pug, and that has a products page that retrieves data from a MongoDB database. Instead, first ask Copilot to create the Express app with TypeScript and Pug. Next, ask to add a products page, and finally ask to retrieve the customer data from a database. 455 | - When you ask Copilot to do a specific task, be specific about the inputs, outputs, APIs, or frameworks you want to use. The more specific your prompt is, the better the outcome will be. For example, instead of "read product data from the database", use "read all products by category, return the data in JSON format, and use the Mongoose library". 456 | - Iterate on your solution 457 | - When asking Copilot Chat for help, you aren't stuck with the first response. You can iterate and prompt Copilot to improve the solution. Copilot has both the context of the generated code and also your current conversation. 458 | - Even if you've already accepted a result, you can always ask Copilot to iterate on the code later. 459 | 460 | More resources about prompting for Copilot: 461 | - [Best practices for using GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/prompt-crafting) 462 | - [Effective Prompting for GitHub Copilot](https://www.youtube.com/watch?v=ImWfIDTxn7E) 463 | - [Pragmatic techniques to get the most out of GitHub Copilot](https://www.youtube.com/watch?v=CwAzIpc4AnA) 464 | - [Best practices for prompting GitHub Copilot in VS Code](https://www.linkedin.com/pulse/best-practices-prompting-github-copilot-vs-code-pamela-fox) 465 | - [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/2023-06-20-how-to-write-better-prompts-for-github-copilot/) 466 | 467 | ### Basic techniques 468 | 469 | - Neighboring Tabs 470 | - [Zero-Shot Prompting](https://www.promptingguide.ai/techniques/zeroshot) 471 | - One-Shot Prompting 472 | - [Few-Shot Prompting](https://www.promptingguide.ai/techniques/fewshot) 473 | - [Role Prompting](https://github.com/f/awesome-chatgpt-prompts) 474 | - Let's Think Step by Step 475 | 476 | 477 | More infromation: 478 | 479 | - [Prompt engineering for GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot) 480 | - [Prompt Engineering Guide](https://www.promptingguide.ai/) 481 | 482 | ### Ask general software questions 483 | 484 | You can ask Copilot Chat general software questions. For example: 485 | 486 | - tell me about nodejs web server frameworks 487 | - how to create an express app 488 | - @terminal how to update an npm package 489 | 490 | ### Ask questions about your project 491 | 492 | You can ask Copilot Chat questions about your project. 493 | 494 | - what sorting algorithm does this function use 495 | - @workspace how are notifications scheduled 496 | - #file:gameReducer.js #file:gameInit.js how are these files related 497 | 498 | To give Copilot the correct context, try some of these strategies: 499 | 500 | - Highlight relevant lines of code 501 | - Use chat variables like #selection, #file, #editor, #codebase, or #git 502 | - Use the @workspace chat participant 503 | 504 | ### Write code 505 | 506 | You can ask Copilot to write code for you. For example: 507 | 508 | - write a function to sum all numbers in a list 509 | - add error handling to this function 510 | - @workspace add form validation, similar to the newsletter page 511 | 512 | When Copilot returns a code block, the response includes options to copy the code, or to insert the code at your cursor, into a new file, or into the terminal. 513 | 514 | ### Ask questions about alerts from GitHub Advanced Security features 515 | 516 | You can ask Copilot about security alerts in repositories in your organization from GitHub Advanced Security features (code scanning, secret scanning, and Dependabot alerts). For example: 517 | 518 | - How would I fix this alert? 519 | - How many alerts do I have on this pull request? 520 | - Which line of code is this code scanning alert referencing? 521 | - What library is affected by this Dependabot alert? 522 | 523 | ### Set up a new project 524 | 525 | Use the /new slash command to set up a new project. For example: 526 | 527 | - /new react app with typescript 528 | - /new python django web application 529 | - /new node.js express server 530 | - Copilot will suggest a directory structure and provide a button to create the suggested files and contents. To preview a suggested file, select the file name in the suggested directory structure. 531 | 532 | Use the /newNotebook slash command to set up a new Jupyter notebook. For example: 533 | 534 | - /newNotebook retrieve the titanic dataset and use Seaborn to plot the data 535 | 536 | ### Fix, improve, and refactor code 537 | 538 | If your active file contains an error, use the /fix slash command to ask Copilot to fix the error. 539 | 540 | You can also make general requests to improve or refactor your code. 541 | 542 | - how would you improve this code? 543 | - translate this code to C# 544 | - add error handling to this function 545 | 546 | ### Write tests 547 | 548 | Use the /tests slash command to ask Copilot to write tests for the active file or selected code. For example: 549 | 550 | - /tests 551 | - /tests using the Jest framework 552 | - /tests ensure the function rejects an empty list 553 | 554 | The /tests slash command writes tests for existing code. If you prefer to write tests before writing code (test driven development), omit the /tests command. For example: 555 | 556 | - Add tests for a JavaScript function that should sum a list of integers 557 | 558 | ### Ask questions about Visual Studio Code 559 | 560 | Use the @vscode chat participant to ask specific questions about Visual Studio Code. For example: 561 | 562 | - @vscode tell me how to debug a node.js app 563 | - @vscode how do I change my Visual Studio Code colors 564 | - @vscode how can I change key bindings 565 | 566 | ### Ask questions about the command line 567 | 568 | Use the @terminal chat participant to ask specific questions about the command line. For example: 569 | 570 | - @terminal find the largest file in the src directory 571 | - @terminal #terminalLastCommand to explain the last command and any errors 572 | 573 | ### Refactor code 574 | 575 | Refactoring code is the process of restructuring existing code without changing its behavior. The benefits of refactoring include improving code readability, reducing complexity, making the code easier to maintain, and allowing new features to be added more easily. 576 | 577 | Typical use cases: 578 | - Understanding code 579 | - Optimizing inefficient code 580 | - Cleaning up repeated code 581 | - Making code more concise 582 | - Splitting up complex units of code 583 | - Rewrite conditional code for better readability 584 | - Reformat code to use a different structure 585 | - Improving the name of a symbol 586 | 587 | More infromation: 588 | - [Refactoring code with GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/guides-on-using-github-copilot/refactoring-code-with-github-copilot) 589 | 590 | ### Testing code 591 | 592 | GitHub Copilot can assist you in developing tests quickly and improving productivity. In this article, we’ll demonstrate how you can use Copilot to write both unit and integration tests. While Copilot performs well when generating tests for basic functions, complex scenarios require more detailed prompts and strategies. This article will walk through practical examples of using Copilot to break down tasks and verify code correctness. 593 | 594 | More infromation: 595 | - [Writing tests with GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/guides-on-using-github-copilot/writing-tests-with-github-copilot) 596 | 597 | ### Advaced techniques 598 | 599 | More infromation: 600 | - [Mastering GitHub Copilot for AI Paired Programming](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming) 601 | - [Advanced Techniques for Optimal Development Efficiency](https://www.willowtreeapps.com/craft/unleashing-github-copilot-advanced-techniques-for-optimal-development-efficiency) 602 | 603 | ## Copilot Tips & Tricks 604 | 605 | - [Prompt crafting advanced techniques](https://github.com/kierunb/CopilotCheatSheet/tree/master?tab=readme-ov-file#prompt-crafting) 606 | - 3S Principle of Prompt Engineering 607 | - Break complex tasks into simpler tasks 608 | - Give GitHub Copilot an example or two 609 | - One-shot/Few-shot 610 | - Indicate relevant code 611 | - Experiment and iterate with your prompts 612 | - Keep history relevant 613 | - Q&A Strategy Prompt 614 | - Pros and Cons Prompt 615 | - Stepwise Chain of Thought Prompt 616 | - Role Prompt 617 | - More prompt samples 618 | 619 | More infromation: 620 | - [Using GitHub Copilot in your IDE: Tips, tricks, and best practices](https://github.blog/developer-skills/github/how-to-use-github-copilot-in-your-ide-tips-tricks-and-best-practices/) 621 | - [Best practices for using GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/prompt-crafting) 622 | - [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/) 623 | - [10 unexpected ways to use GitHub Copilot](https://github.blog/developer-skills/programming-languages-and-frameworks/10-unexpected-ways-to-use-github-copilot/) 624 | 625 | ## Building Copilot Extensions 626 | 627 | Copilot Extensions are integrations that expand the functionality of Copilot Chat, allowing developers to bring external tools, services, and custom behaviors into the Chat experience. You can use Copilot Extensions to extend the capabilities of Copilot Chat in a variety of ways, including: 628 | 629 | - Querying documentation: A Copilot Extension can allow Copilot Chat to query a third-party documentation service to find information about a specific topic. 630 | - AI-assisted coding: A Copilot Extension can use a third-party AI model to provide code suggestions. 631 | - Data retrieval: A Copilot Extension can allow Copilot Chat to query a third-party data service to retrieve information about a specific topic. 632 | - Action execution: A Copilot Extension can allow Copilot Chat to execute a specific action, such as posting to a message board or updating a tracking item in an external system. 633 | 634 | More infromation: 635 | - [About building Copilot Extensions](https://docs.github.com/en/copilot/building-copilot-extensions/about-building-copilot-extensions) 636 | 637 | ## Hands-on labs in Codespaces 638 | 639 | - [Introduction to GitHub Copilot](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/03-Introduction-to-GitHub-Copilot/README.md) 640 | - [Using GitHub Copilot with JavaScript](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/04-Using-GitHub-Copilot-with-JavaScript/README.MD) 641 | - [Using GitHub Copilot with Python](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/05-Using-GitHub-Copilot-with-Python/README.md) 642 | - [Using GitHub Copilot with C#](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/06-Using-GitHub-Copilot-with-CSharp/README.md) 643 | - [Creating a Mini Game with GitHub Copilot](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/07-Creating-Mini-Game-with-GitHub-Copilot/README.md) 644 | - [Using Advanced GitHub Copilot Features](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/08-Using-Advanced-GitHub-Copilot-Features/README.md) 645 | 646 | ## Reference 647 | 648 | - [Getting started with prompts for Copilot Chat](https://docs.github.com/en/copilot/using-github-copilot/guides-on-using-github-copilot/getting-started-with-prompts-for-copilot-chat) 649 | - [GitHub Copilot Docs](https://docs.github.com/en/copilot) 650 | - [GitHub Copilot CheatSheet](https://github.com/kierunb/CopilotCheatSheet) 651 | - [Awesome GitHub Copilot](https://github.com/program247365/awesome-github-copilot) 652 | - [GitHub Copilot](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) 653 | - [GitHub Copilot Chat extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot-chat) 654 | 655 | ## Labs 656 | 657 | Copilot Hands-on labs: 658 | - [Introduction to GitHub Copilot](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/03-Introduction-to-GitHub-Copilot/README.md) 659 | - [Using GitHub Copilot with JavaScript](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/04-Using-GitHub-Copilot-with-JavaScript/README.MD) 660 | - [Using GitHub Copilot with Python](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/05-Using-GitHub-Copilot-with-Python/README.md) 661 | - [Using GitHub Copilot with C#](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/06-Using-GitHub-Copilot-with-CSharp/README.md) 662 | - [Creating a Mini Game with GitHub Copilot](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/07-Creating-Mini-Game-with-GitHub-Copilot/README.md) 663 | - [Using Advanced GitHub Copilot Features](https://github.com/microsoft/Mastering-GitHub-Copilot-for-Paired-Programming/blob/main/08-Using-Advanced-GitHub-Copilot-Features/README.md) 664 | - [Activate GitHub Copilot to become more efficient - .NET](https://moaw.dev/workshop/github-copilot/) 665 | - [Enhance Your Efficiency with GitHub Copilot: A Workshop - Java](https://moaw.dev/workshop/github-copilot-java/) 666 | - [Using GitHub Copilot to quickly build a Node.js application with Azure Cosmos DB and App Service](https://moaw.dev/workshop/gh:azure-samples/copilot-nodejs-todo/main/docs/) 667 | 668 | Sample .NET apps for Copilot experiments: 669 | - [TodoApp](https://github.com/davidfowl/TodoApp) 670 | - [TodoAppBlazorServer](https://github.com/claudiobernasconi/TodoAppBlazorServer) 671 | -------------------------------------------------------------------------------- /img/copilot-prompting.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kierunb/CopilotCheatSheet/28e85ca3a1c2b8a4cc993945af490859442e2701/img/copilot-prompting.webp -------------------------------------------------------------------------------- /img/github-copilot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kierunb/CopilotCheatSheet/28e85ca3a1c2b8a4cc993945af490859442e2701/img/github-copilot.png -------------------------------------------------------------------------------- /playground.html: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kierunb/CopilotCheatSheet/28e85ca3a1c2b8a4cc993945af490859442e2701/playground.html -------------------------------------------------------------------------------- /playground.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kierunb/CopilotCheatSheet/28e85ca3a1c2b8a4cc993945af490859442e2701/playground.js -------------------------------------------------------------------------------- /playground.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kierunb/CopilotCheatSheet/28e85ca3a1c2b8a4cc993945af490859442e2701/playground.py -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # GitHub Copilot CheatSheet 2 | 3 | Version 2 is available here: [copilot-cheat-sheet-v2](copilot-cheat-sheet-v2.md) 4 | 5 | ![GitHub Copilot images](img/github-copilot.png) 6 | 7 | ## Supported languages and frameworks 8 | 9 | GitHub Copilot works on any language, including **Java**, **PHP**, **Python**, **JavaScript**, **Ruby**, **Go**, **C#**, or **C++**. Because it’s been trained on languages in public repositories, **it works for most popular languages, libraries and frameworks**. 10 | 11 | - [Quickstart for GitHub Copilot](https://docs.github.com/en/copilot/quickstart) 12 | 13 | ## Use cases for GitHub Copilot in VS Code 14 | 15 | With GitHub Copilot in VS Code you can: 16 | 17 | - Get inline code suggestions while you're writing and iterating on code. 18 | - Start a chat conversation to generate or refactor source code, produce documentation comments, or generate unit tests. 19 | - Get help with fixing errors in your code, or resolve errors while running commands in the terminal. 20 | - Ask questions to help ramp-up on a new code base, or accelerate learning a new programming language or framework. 21 | - Use chat features to discover and configure your VS Code setup. 22 | 23 | ## Main features - Where can I find Copilot? 24 | 25 | Everywhere: Code editor, multiple chat modes, CLI, commit messages and pull request summaries. 26 | 27 | It is also available in **GitHub Mobile App** and on **GitHub.com**, with a subscription to Copilot Enterprise. 28 | 29 | Access Copilot via: 30 | 31 | - **Code completions a.k.a. 'ghost text'** - just start typing 32 | - You can also access Copilot by writing a comment and expressing what you want 33 | - You can also disable completions in **GitHub Copilot status icon** (to avoid to much 'noise') 34 | - **Chat** 35 | - **Inline Chat** - Ctrl + I It gives you what to accept changes first or highlight important code 36 | - **Quick Chat** - Ctrl + Shift + I 37 | - **Side panel Chat** - Ctrl + Alt + I Solve and brainstorm more complex problems or analyse the codebase, learn new languages and frameworks, multiple chats and docking are also possible 38 | - **Sparkle Icon** - a.k.a Smart actions - quicky invoke copilot in code editor, terminal, commit message and pull request summary 39 | - **Context menu** - Highlight a code snippet and navigate to Copilot in VS Code context menu 40 | 41 | 42 | ### Essential keyboard shortcuts 43 | 44 | - Tab - Accept the whole suggestion 45 | - Esc - Dismiss suggestion 46 | - Control + -> - Accept one word only 47 | - Control + Enter - See alternative suggestions 48 | - Alt + ], Alt + [ - Next/Prev alternative suggestion 49 | - Ctrl + L - New chat 50 | 51 | 52 | ### Copilot in CLI 53 | 54 | This feature requires [GitHUb CLI](https://docs.github.com/en/copilot/managing-copilot/configure-personal-settings/installing-github-copilot-in-the-cli). 55 | 56 | ```shell 57 | gh copilot explain "sudo apt-get" 58 | gh copilot suggest "undo the last commit" 59 | ``` 60 | 61 | ### Copilot in GitHub Mobile 62 | 63 | In GitHub Mobile, you can use Copilot Chat to ask: 64 | 65 | - General software-related questions, without a particular context. For more information, see "Asking a general question about software development." 66 | - Questions asked in the context of your project. For more information, see "Asking questions about a specific repository." 67 | - Questions about a specific file or specified lines of code within a file. For more information, see "Asking questions about specific pieces of code." 68 | 69 | Some examples of general questions you could ask are: 70 | 71 | - What are the advantages of the Go programming language? 72 | - What is Agile software development? 73 | - What is the most popular JavaScript framework? 74 | - Give me some examples of regular expressions. 75 | - Write a bash script to output today's date. 76 | 77 | More information under this [link](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-github-mobile). 78 | 79 | ### Code completions in the editor 80 | 81 | Copilot suggests code as you type. Copilot analyzes the context in the file you're editing and related files, and offers suggestions from within the editor. For example, begin typing the name of a method and Copilot suggests the implementation, following your coding style. 82 | 83 | - start with function declaration **function**, **def** to 'wake up' Copilot 84 | - provide more context to get better (less generic) answers 85 | 86 | ### Answering coding questions 87 | 88 | - What is recursion? 89 | - What is complexity of this function? 90 | - What is dependency injection? 91 | 92 | #### Provide guidance and support for common coding tasks and challenges 93 | 94 | Ask Copilot about syntax or general programming concepts without requiring to navigate documentation or search online forums. Copilot gives responses in natural language format or in code snippet format. For example, you can ask questions such as "what is recursion?" or "how to create a singleton in Java?". 95 | 96 | #### Improve code understanding by explaining selected code 97 | 98 | Copilot generates natural language descriptions of the code's functionality and purpose. This can be useful if you want to understand the code's behavior or for non-technical stakeholders who need to understand how the code works. 99 | 100 | #### Provide guidance that is specific to your codebase 101 | 102 | Copilot has the context of your workspace and can give step-by-step guidance and code samples that are tailored to your project. For example, "how to add a contacts page?" or "how do I read customer data from the database?". 103 | 104 | Use @workspace to get help tailored to codebase in your project. 105 | 106 | ### Code refactoring and improvements 107 | 108 | #### Provide suggestions for implementing code refactorings 109 | 110 | Copilot suggests refactorings using the context of your codebase. For example, ask Copilot to refactor a function to not use recursion, or to suggest an algorithm that can improve performance. 111 | 112 | #### Suggest potential improvements to selected code 113 | 114 | Such as improved handling of errors and edge cases, or changes to the logical flow to make the code more readable. 115 | 116 | ### Fix issues/refactor and debug code and code inline 117 | 118 | Highlight the code you want to fix, right click, and select “Fix using Copilot.” Copilot will then provide you with a suggested fix for your code. 119 | 120 | #### Propose a fix for bugs in your code (also Vulenrability detection) 121 | 122 | With the **/fix** command by suggesting code snippets and solutions based on the context of the error or issue. For example, if your code produces an error message or warning, Copilot Chat can suggest possible fixes based on the error message, the code's syntax, and the surrounding code. The changes might consist of changes to variables, control structures, or function calls that could resolve the issue. 123 | 124 | Copilot model targets the most common vulnerabilities coding patterns like hard coded credentials, sql injections, and path injections. The system leverages LLMs to approximate the behavior of static analysis tools and can even detect vulnerabilities patterns in incomplete fragments of code. This means insecure coding patterns can be quickly blocked and replaced by better suggestions before they ever appear in the editor. Copilot code should still be reviewed and tested before being merged into the codebase. 125 | 126 | 127 | 128 | #### Suggest terminal command fixes 129 | 130 | When a command fails to run in the terminal, Copilot displays a sparkle in the gutter that offers a Quick Fix to explain what happened. 131 | 132 | #### Suggest terminal command fixes 133 | 134 | Utilize GitHub Copilot Chat to run commands right in your terminal (e.g. from chat window). 135 | 136 | ### Jumpstart your project 137 | 138 | #### Generate a new VS Code workspace 139 | 140 | For your choice of technologies with /new to get started quickly with a new project. Pick and choose your technology stack, preview the workspace files, and let Copilot scaffold the entire workspace for you. 141 | 142 | #### Scaffold a new Jupyter notebook by using natural language 143 | 144 | Generate a new notebook that is preconfigured based on a description by using /newNotebook. For example, to scaffold a new notebook that loads, inspects, and visualizes a sample dataset, prompt Copilot with "@workspace /newNotebook read titanic dataset with pandas, display key values with seaborn". 145 | 146 | ### Generate unit test cases or practice Test-driven Development (TDD) 147 | 148 | E.g. You can write test before implementation. 149 | 150 | #### Write unit test cases for your test framework 151 | 152 | Based on the code open in the editor or the code snippet you highlight in the editor. Copilot identifies your test framework and coding style and generates matching code snippets. 153 | 154 | #### Identify and write test cases for edge cases and boundary conditions 155 | 156 | Copilot can suggest test cases for error handling, null values, or unexpected input types. 157 | 158 | #### Suggest assertions 159 | 160 | Suggest assertions that ensure the function is working correctly, based on the code's context and semantics. For example, generate assertions to ensure that function input parameters are valid. 161 | 162 | ### Generate code documentation 163 | 164 | Generate code documentation for multiple languages for the code open in the editor or the code snippet you highlight in the editor. Use /doc or a Copilot smart action to help you generate meaningful code documentation. 165 | 166 | ### Improve your productivity in VS Code 167 | 168 | #### AI-generated commit messages and PR descriptions 169 | 170 | Based on the code changes in a commit or the changes in a pull request. Use the sparkle button in the Source Control view or GitHub PR extension to generate a title and description that summarizes your changes. 171 | 172 | #### Ask help in the Command Palette 173 | 174 | To help you find the relevant command in VS Code. You can describe the functionality and Copilot can help identify the matching functionality. For example, type "code preview in scrollbar" in the Command Palette, and Copilot can identify that you're referring to the editor.minimap settings. 175 | 176 | #### Use terminal inline chat 177 | 178 | To ask questions about the terminal or how to use specific shell commands. For example, you can ask questions such as "list the top 5 largest files in the src directory", or "how to enable shell integration". 179 | 180 | #### AI-generated rename suggestions 181 | 182 | For symbols in your source code. When you rename a symbol in your code, Copilot suggests a new name based on the context of the symbol and the codebase. 183 | 184 | ### Assisting non-native English speakers 185 | 186 | GitHub Copilot can understand other languages beyond English. Additionally, GitHub Copilot helps translate words from English to other languages. 187 | 188 | ### Creating data (e.g. dictionaries with lookup data) 189 | 190 | E.g. Create a dictionary with countries and their capitals. 191 | E.g. Create a dictionary with countries and their capitals in english and polish. 192 | 193 | ``` 194 | Produce IO exemplars for sentiment analysis. Examples are categorized as either positive or negative. 195 | Produce 2 negative examples and 8 positive examples. 196 | Use this format for the examples: 197 | Q: 198 | A: 199 | ``` 200 | 201 | ### Matching patterns with regular expressions 202 | 203 | E.g.: 'Create a regex to validate phone number in this format (xxx) xxx-xxxx'. 204 | 205 | ### Working with external APIs 206 | 207 | E.g.: "Write a function to retrieve dog breeds from following API 'https://dog.ceo/api/breed/' and return an array of breed objects". 208 | 209 | Activate GitHub Copilot and use it to generate ideas on how to solve the problem better. 210 | 211 | ### Preparing for technical interviews 212 | 213 | LeetCode etc. 214 | 215 | ### Navigating a new codebase 216 | 217 | Open any project, use *@workspace* agent and start asking questions about codebase. 218 | 219 | ### Run terminal commands from GitHub Copilot Chat 220 | 221 | With the **@terminal** agent in VS Code, you can ask GitHub Copilot how to run a particular command. Once it generates a response, you can then click the “Insert into Terminal” button to run the suggested command. 222 | 223 | In **GitHub CLI** you can use: 224 | ```bash 225 | gh copilot explain "sudo apt-get" 226 | ``` 227 | 228 | ### Write pull request summaries (Copilot Enterprise feature only) 229 | 230 | GitHub Copilot is integrated into pull requests. 231 | 232 | ### Generate commit messages 233 | 234 | I came across this one recently while making changes in VS Code. GitHub Copilot can help you generate commit messages right in your IDE. If you click on the source control button, you’ll notice a sparkle in the message input box. 235 | 236 | ### Get help in the terminal with GitHub Copilot in the CLI 237 | 238 | This is an extension to GitHub CLI that helps you with general shell commands, Git commands, and gh cli commands. 239 | 240 | Common use cases: 241 | - Find the right command to perform a task 242 | - Explain an unfamiliar command 243 | 244 | ### Talk to your repositories on GitHub.com (Copilot Enterprise feature only) 245 | 246 | You can now use GitHub Copilot Chat to explain the repository to you, right in GitHub.com. Just click on the Copilot icon in the top right corner of the repository and ask whatever you want to know about that repository. 247 | 248 | ### Bulk close 1000+ GitHub Issues 249 | 250 | Implement custom GitHub action to automate a tedious task. 251 | 252 | ### Get help with error messages in your terminal 253 | 254 | Error messages can often be confusing. With GitHub Copilot in your IDE, you can now get help with error messages right in the terminal. Just highlight the error message, right click, and select “Explain with Copilot.” GitHub Copilot will then provide you with a description of the error and a suggested fix. 255 | 256 | ### Solve LeetCode problems 257 | 258 | - Install [LeetCode VS Code extension](https://marketplace.visualstudio.com/items?itemName=LeetCode.vscode-leetcode) 259 | - Open the `leetcode` extension 260 | - Select a problem to solve 261 | - Start the coding exercise 262 | - Use the problem descption from leetcode with Copilot Chat (copy & paste), ask it to create the code 263 | - Use the right click pop-up menu in the solutions file to test and submit your solution to leetcode 264 | 265 | ### Debug your GitHub Actions workflow 266 | 267 | Copilot can help you write and debug GitHub actions. 268 | 269 | ### Exit Vim 270 | 271 | ``` 272 | How do I exit Vim? 273 | ``` 274 | 275 | #### References 276 | 277 | - [GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/overview) 278 | - [GitHub Copilot Quickstart](https://code.visualstudio.com/docs/copilot/getting-started) 279 | - [Copilot Chat Tutorial](https://code.visualstudio.com/docs/copilot/getting-started-chat) 280 | - [GitHub Copilot Discussions](https://github.com/orgs/community/discussions/categories/copilot?discussions_q=is%3Aopen++category%3ACopilot+) 281 | - [GitHub Copilot Blog](https://github.blog/changelog/label/copilot/) 282 | - [8 things you didn’t know you could do with GitHub Copilot](https://github.blog/developer-skills/github/8-things-you-didnt-know-you-could-do-with-github-copilot/) 283 | - [10 unexpected ways to use GitHub Copilot](https://github.blog/developer-skills/programming-languages-and-frameworks/10-unexpected-ways-to-use-github-copilot/) 284 | - [Use Copilot to Write and Translate a Binary Search Algorithm](https://dev.to/github/use-copilot-to-write-and-translate-a-binary-search-algorithm-lcm) 285 | - [GitHub Copilot in the CLI](https://docs.github.com/en/copilot/responsible-use-of-github-copilot-features/responsible-use-of-github-copilot-in-the-cli) 286 | 287 | 288 | ### Slash Commands 289 | 290 | /help - display all slash commands and agents (participants) 291 | 292 | 293 | | Command | Description | Usage | 294 | |-----------|--------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------| 295 | | /explain | Get code explanations | Open file with code or highlight code you want explained and type:
/explain what is the fetchPrediction method? | 296 | | /fix | Receive a proposed fix for the problems in the selected code | Highlight problematic code and type:
/fix propose a fix for the problems in fetchAirports route | 297 | | /tests | Generate unit tests for selected code | Open file with code or highlight code you want tests for and type:
/tests | 298 | | /help | Get help on using Copilot Chat | Type:
/help what can you do? | 299 | | /clear | Clear current conversation | Type:
/clear | 300 | | /doc | Add a documentation comment | Highlight code and type:
/doc
You can also press Ctrl+I in your editor and type /doc/ inline | 301 | | /generate | Generate code to answer your question | Type:
/generate code that validates a phone number | 302 | | /optimize | Analyze and improve running time of the selected code | Highlight code and type:
/optimize fetchPrediction method | 303 | | /clear | Clear current chat | Type:
/clear | 304 | | /new | Scaffold code for a new workspace | Type:
/new create a new django app | 305 | | /simplify | Simplify the selected code | Highlight code and type:
/simplify | 306 | | /feedback | Provide feedback to the team | Type:
/feedback | 307 | 308 | ### Agents (participants) 309 | 310 | - **@workspace** - ask a questions related to your actual workspace 311 | - **@terminal** - ask a questions related to info in your terminal 312 | - **@vscode** - ask the questions related to VS Code 313 | - **@github** - get answers grounded in web search, code search, and your enterprise's knowledge bases 314 | 315 | ### Variables 316 | 317 | - **#codebase**: the contents of the current workspace. It includes information about the files and folders in your workspace, as well as any settings or configurations specific to that workspace. 318 | - **#editor**: the code in the active editor. The editor content is implicitly included in the Chat view context. 319 | - **#file**: include a specified file in your workspace as context with your chat prompt. 320 | - **#git**: information about the current git repository, such as the workspace folder, branch name, remotes, and more. 321 | - **#selection**: the visible source code in the active editor. 322 | - **#terminalLastCommand**: the active terminal's last run command. 323 | - **#terminalSelection**: the active terminal's selection. 324 | 325 | --- 326 | 327 | ## Copilot in Action 328 | 329 | ### Basics 330 | 331 | #### Ask a question 332 | 333 | Use valid comment style from you programming language. 334 | 335 | ```javascript 336 | // q: Explain Onion Architecture. 337 | // q: How to cook a beef stew? ;) 338 | ``` 339 | 340 | #### Code suggestions in IDE - basics 341 | 342 | - Suggestions appear as you type. 343 | - Accept whole suggestion with Tab. 344 | - Accept word by word Ctrl + ->. 345 | - Navigate through multpile solutions (Completion Panel) with Ctrl + Enter. 346 | - Guide Copilot with **instructions within the function** to provide conditions, assertions and so on. 347 | - Inline chat Ctrl + I might be better than "ghost text" in many cases. 348 | - Don't recreate common prompts - use **slash commands**. 349 | - Don't forget about context. 350 | 351 | #### Work with regular expressions (regex) 352 | 353 | ```javascript 354 | 355 | // regex to match the phone number in the format (xxx) xxx-xxxx 356 | 357 | // test phoneRegex against the phone number with console.log 358 | 359 | ``` 360 | 361 | #### Work with SQL queries 362 | 363 | Define data structure (e.g. struct, class) like Product(Id, Name, Price, Description) for best results. 364 | ``` 365 | Generate sql to insert Product object into database. 366 | ``` 367 | 368 | #### Work with extenral APIs 369 | 370 | #### Use @file to provide context of specific file 371 | 372 | E.g. Use sample.json file to generate a strongly typed class or interface. 373 | 374 | #### Unit testing 375 | 376 | ```javascript 377 | // test caclulator function 378 | // create unit test for a calculator function using assert/jest framework 379 | ``` 380 | 381 | --- 382 | 383 | ## Copilot rules of engagement 384 | 385 | ### Stay smart and use Copilot don't let copilot fly the plane 386 | 387 | 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. 388 | 389 | **Just as you would review a colleague’s code, you should always assess, analyze, and validate AI-generated code.** 390 | 391 | Copilot is going to latch on to your code to generate suggestions that follow the existing pattern, so the adage "garbage in, garbage out" applies. 392 | 393 | ### Beyond the completions 394 | 395 | **Context, context, context** 396 | 397 | If you understand Large Language Models (LLMs), you will know that they are designed to make predictions based on the context provided. This means, the more contextually rich our input or prompt is, the better the prediction or output will be. 398 | 399 | #### Be specific and keep it simple 400 | 401 | When you ask Copilot to do something, be specific in your ask and break down a large task into separate, smalller tasks. For example, don't ask Copilot to create an Express app, that uses TypeScript and Pug, and that has a products page that retrieves data from a MongoDB database. Instead, first ask Copilot to create the Express app with TypeScript and Pug. Next, ask to add a products page, and finally ask to retrieve the customer data from a database. 402 | 403 | #### Iterate on your solution 404 | 405 | When asking Copilot Chat for help, you aren't stuck with the first response. You can iterate and prompt Copilot to improve the solution. Copilot has both the context of the generated code and also your current conversation. 406 | 407 | #### Open your relevant files 408 | 409 | Having your files open provides GitHub Copilot with context. When you have additional files open, it will help to inform the suggestion that is returned. Remember, if a file is closed, GitHub Copilot cannot see the file’s content in your editor, which means it cannot get the context from those closed files. 410 | 411 | #### Provide a top-level comment 412 | 413 | Just as you would give a brief, high-level introduction to a coworker, a top-level comment in the file you’re working in can help GitHub Copilot understand the overall context of the pieces you will be creating—especially if you want your AI assistant to generate the boilerplate code for you to get going. 414 | 415 | #### Set appropriate includes and references 416 | 417 | It’s best to manually set the includes/imports or module references you need for your work, particularly if you’re working with a specific version of a package. 418 | 419 | #### Meaningful function names matter 420 | 421 | The name of your variables and functions matter. If you have a function named foo or bar, GitHub Copilot will not be able to give you the best completion because it isn’t able to infer intent from the names. 422 | 423 | #### Provide specific and well-scoped function comments 424 | 425 | Commenting your code helps you get very specific, targeted suggestions. 426 | Single, specific, short comments help GitHub Copilot provide better context. 427 | 428 | #### Provide sample code 429 | 430 | Providing sample code to GitHub Copilot will help it determine what you’re looking for. This helps to ground the model and provide it with even more context. 431 | 432 | #### Inline chat is usually more flexible than comments and "ghost text" 433 | 434 | By pressing Cmd + I (Ctrl + I on Windows) you’ll have Copilot right there to ask questions. This is a bit more convenient for quick fixes instead of opening up GitHub Copilot Chat’s side panel. 435 | 436 | #### Remove irrelevant requests in chat side-panel 437 | 438 | You can delete a previously asked question in the chat interface to remove it from the indexed conversation - especially if it is no longer relevant. 439 | 440 | #### Navigate through your conversation 441 | 442 | Another tip is to use the up and down arrows to navigate through your conversation with GitHub Copilot Chat. You can just use my **keyboard arrows** just like in the terminal! 443 | 444 | #### Use chat agent and participants 445 | 446 | The **@workspace** agent for example, is aware of your entire workspace and can answer questions related to it. 447 | 448 | Use **@vscode** to know more about VS Code features and APIs. 449 | 450 | #### Highlight relevant code 451 | 452 | Highlight relevant code in your files before asking it questions. This will help to give targeted suggestions and just provides the assistant with more context into what you need help with. 453 | 454 | #### Organize your conversations with threads 455 | 456 | You can have multiple ongoing conversations with GitHub Copilot Chat on different topics by isolating your conversations with threads. We’ve provided a convenient way for you to start new conversations (thread) by clicking the ** + ** sign on the chat interface. 457 | 458 | #### Use slash commands for common tasks 459 | 460 | Don't reinvent the wheel. Copilot have commands to help you explain code, fix code, create a new notebook, write tests, and many more. They are just shortcuts to common prompts that we’ve found to be particularly helpful in day-to-day development from our own internal usage. 461 | 462 | #### Attach relevant files for reference (chat variables) 463 | 464 | In Visual Studio and VS Code, you can attach relevant files for GitHub Copilot Chat to reference by using **#file**. This scopes GitHub Copilot to a particular context in your code base and provides you with a much better outcome. 465 | 466 | You can also add context to your chat message by using the Attach Context button in the Chat view. You can then select the specific type of context from a Quick Pick, such as the current selection, one or more files from the workspace, or one or more symbols from your source code. 467 | 468 | #### Be on the lookout for sparkles 469 | 470 | In VS Code, you can quickly get help from GitHub Copilot by looking out for “magic sparkles.” For example, in the commit comment section, clicking the magic sparkles will help you generate a commit message with the help of AI. You can also find magic sparkles inline in your editor as you’re working for a quick way to access GitHub Copilot inline chat. 471 | 472 | #### Use voice interactions 473 | 474 | With the voice control capabilities in VS Code, provided by the VS Code Speech extension, you can initiate a chat conversation by using your voice: 475 | 476 | - Use your voice to dictate your chat prompt 477 | - Use the "Hey Code" voice command to start a voice session with Copilot Chat 478 | - Accelerate voice input for chat by using the "hold to speak" mode 479 | 480 | #### Don't ignore feedback features 481 | 482 | Copilot is always incrementally improve and is always learning - use feedback tools like :thumbsup: or :thumbsdown:. 483 | 484 | ##### References 485 | 486 | - [Tips, tricks, and best practices](https://github.blog/developer-skills/github/how-to-use-github-copilot-in-your-ide-tips-tricks-and-best-practices/) 487 | - [Best practices for using GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/prompt-crafting) 488 | - [Effective Prompting for GitHub Copilot](https://www.youtube.com/watch?v=ImWfIDTxn7E) 489 | - [Pragmatic techniques to get the most out of GitHub Copilot](https://www.youtube.com/watch?v=CwAzIpc4AnA) 490 | - [Best practices for prompting GitHub Copilot in VS Code](https://www.linkedin.com/pulse/best-practices-prompting-github-copilot-vs-code-pamela-fox/) 491 | - [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/) 492 | 493 | --- 494 | 495 | ## Prompt crafting 496 | 497 | ![Copilot Prompting](img/copilot-prompting.webp) 498 | 499 | 500 | - Provide good **context**; giving examples and information about what you're trying to achieve 501 | - Be **specific**; if it's for a certain audience, say that 502 | - **Break down** the problem 503 | - Be **clear** in how you ask your questions. If something comes back that doesn't sound right, clarify it 504 | - **Rephrase and refine** for your prompts 505 | 506 | 507 | ### 3S Principle of Prompt Engineering 508 | 509 | - **Simple** 510 | - The more code - the more errors and hallucinations 511 | - Break your solution in simple steps 512 | - Use 'simple' word in prompt 513 | - Keep relevant files open 514 | - The best prompt is one you don't have to write 515 | - **Specific** 516 | - Don't ask open question 517 | - Use '@workspace' agent to focus on your project as a base for agents (especially in Chat window) 518 | - Use includes like '#editor', '#file', '#selection' or '#terminalLastCommand' 519 | - Always use specific context and specific prompts 520 | - **Short** 521 | - You don't need to phrase long question 522 | - You don't need to use proper grammar 523 | - Type less because typos don't matter 524 | - Command-like prompts are OK 525 | 526 | Simple, specific and short: 527 | ``` 528 | fixed header 529 | fixed footer 530 | main content scrolls between header and footer 531 | ``` 532 | Notice three short prompts insted of large one. 533 | 534 | ### Prompting tips & Trics 535 | 536 | - Provide context 537 | - Be predictable (standard naming conventions etc.) 538 | - Keep relevant files open (source code, input files, even data files like json/xml) 539 | - Provide description on top of the file (E.g. This file parses xyz.json file) 540 | - Provide imports/usings etc. for specific packages, namespaces or libraries 541 | - Provide specific and desriptive variable, type, function and parameter names 542 | - Give inline hints (in comments inside functions) 543 | - Double check code against documentation 544 | - If code or syntax provided by Copilot is from older version of langauge or library - copy and paste a more up-to-date example from documentation 545 | - Start code generation by defining proper data/entity models, schemas, data structures etc. (e.g. while using MVC pattern) 546 | 547 | ### Basic techniques 548 | 549 | - Neighboring Tabs 550 | - Zero-Shot Prompting 551 | - One-Shot Prompting 552 | - Few-Shot Prompting 553 | - Role Prompting 554 | - Let's Think Step by Step 555 | 556 | ### Set the stage with a high-level goal, then get specific 557 | 558 | This is most helpful if you have a blank file or empty codebase. In other words, if GitHub Copilot has zero context of what you want to build or accomplish, setting the stage for the AI pair programmer can be really useful. 559 | 560 | ``` 561 | /* 562 | Create a basic markdown editor in Next.js with the following features: 563 | - Use react hooks 564 | - Create state for markdown with default text "type markdown here" 565 | - A text area where users can write markdown 566 | - Show a live preview of the markdown text as I type 567 | - Support for basic markdown syntax like headers, bold, italics 568 | - Use React markdown npm package 569 | - The markdown text and resulting HTML should be saved in the component's state and updated in real time 570 | */ 571 | ``` 572 | 573 | ### Make your ask simple and specific. Aim to receive a short output from GitHub Copilot. 574 | 575 | Once you communicate your main goal to the AI pair programmer, articulate the logic and steps it needs to follow for achieving that goal. 576 | 577 | When writing a prompt for Copilot, first give Copilot a broad description of the goal or scenario. Then list any specific requirements. 578 | 579 | For example: 580 | 581 | - Write a function that tells me if a number is prime 582 | - The function should take an integer and return true if the integer is prime 583 | - The function should error if the input is not a positive integer 584 | 585 | ### Break complex tasks into simpler tasks 586 | 587 | If you want Copilot to complete a complex or large task, break the task into multiple simple, small tasks. 588 | 589 | For example, instead of asking Copilot to generate a word search puzzle, break the process down into smaller tasks, and ask Copilot to accomplish them one by one: 590 | 591 | - Write a function to generate a 10 by 10 grid of letters. 592 | - Write a function to find all words in a grid of letters, given a list of valid words. 593 | - Write a function to that uses the previous functions to generate a 10 by 10 grid of letters that contains at least 10 words. 594 | - Update the previous function to print the grid of letters and 10 random words from the grid. 595 | 596 | ### Avoid ambiguity 597 | 598 | Avoid ambiguous terms. For example, don’t ask "what does this do" if "this" could be the current file, the last Copilot response, or a specific code block. Instead, be specific: 599 | 600 | - What does the createUser function do? 601 | - What does the code in your last response do? 602 | 603 | Ambiguity can also apply to libraries: 604 | 605 | - If you are using an uncommon library, describe what the library does. 606 | - If you want to use a specific library, set the import statements at the top of the file or specify what library you want to use. 607 | 608 | ### Give GitHub Copilot an example or two. 609 | 610 | ``` 611 | // Map through an array of arrays of objects 612 | // Example: Extract names from the data array 613 | // Desired outcome: ['John', 'Jane', 'Bob'] 614 | ``` 615 | 616 | Use examples to help Copilot understand what you want. You can provide example input data, example outputs, and example implementations. 617 | 618 | For example: 619 | 620 | Write a function that finds all dates in a string and returns them in an array. Dates can be formatted like: 621 | 622 | - 05/02/24 623 | - 05/02/2024 624 | - 5/2/24 625 | - 5/2/2024 626 | - 05-02-24 627 | - 05-02-2024 628 | - 5-2-24 629 | - 5-2-2024 630 | 631 | Example: 632 | 633 | findDates("I have a dentist appointment on 11/14/2023 and book club on 12-1-23") 634 | 635 | Returns: ["11/14/2023", "12-1-23"] 636 | 637 | Unit tests can also serve as examples. Before writing your function, you can use Copilot to write unit tests for the function. Then, you can ask Copilot to write a function described by those unit tests. 638 | 639 | #### Example 1 - Zero-shot vs One-shot 640 | 641 | ``` 642 | // zero-shot 643 | Write me unit tests for all the operations in my calculator.js file 644 | 645 | // one-shot 646 | Write me unit tests for all the operations in my calculator.js file 647 | Use the following example: test(‘adds 1 + 2 to equal 3’, () => ( expect(calculate(1, 2 ‘+’)).toBe(3); }); 648 | ``` 649 | 650 | #### Example 2 - Zero-shot vs One-shot 651 | 652 | ``` 653 | // zero-shot 654 | Can you create an HTML calculator for me? 655 | 656 | // one-shot 657 | Can you create an HTML calculator for me? Use the following example: 658 | 659 | 660 | 661 | Calculator 662 | 663 | 664 | 665 | ``` 666 | 667 | #### Example 3 - Zero-shot vs One-shot 668 | ``` 669 | Zero-shot “Can you write me the code for a snake game?“ 670 | One-Shot “Can you write me the code for a snake game? Example: Ensure that there is a leaderboard mechanic“ 671 | ``` 672 | 673 | ### Indicate relevant code 674 | 675 | If you are using Copilot in your IDE to get suggestions as you code, open any relevant files and close irrelevant files. Copilot will use the open files to understand your request. 676 | 677 | If you are using Copilot Chat in your IDE, open the file or highlight the code that you want Copilot to reference. You can also specify which files Copilot Chat should reference. For example, in VS Code, use the #file variable or the @workspace participant. 678 | 679 | ### Experiment and iterate with your prompts 680 | 681 | Just how conversation is more of an art than a science, so is prompt crafting. So, if you don’t receive what you want on the first try, recraft your prompt by following the best practices above. 682 | 683 | - If you don’t get the result that you want, iterate on your prompt and try again. 684 | - If you are using Copilot to get suggestions as you code, you can delete the suggestion entirely and start over. Or you can keep the suggestion and request modifications. 685 | - If you are using Copilot Chat, you can reference the previous response in your next request. Or, you can delete the previous response and start over. 686 | 687 | ### Keep a couple of relevant tabs open 688 | 689 | We don’t have an exact number of tabs that you should keep open to help GitHub Copilot contextualize your code, but from our experience, we’ve found that one or two is helpful. 690 | 691 | ### Keep history relevant 692 | 693 | Copilot Chat uses the chat history to get context about your request. To give Copilot only the relevant history: 694 | 695 | - Use threads to start a new conversation for a new task 696 | - Delete requests that are no longer relevant or that didn’t give you the desired result 697 | 698 | ### Follow good coding practices 699 | 700 | That includes providing descriptive variable names and functions, and following consistent coding styles and patterns. 701 | 702 | If you aren't getting the responses you want when you ask Copilot for suggestions or explanations in your codebase, make sure that your existing code follows best practices and is easy to read. For example: 703 | 704 | - Use a consistent code style and patterns 705 | - Use descriptive names for variables and functions 706 | - Comment your code 707 | - Structure your code into modular, scoped components 708 | - Include unit tests 709 | 710 | Tip: Use Copilot to help your code follow best practices. For example, ask Copilot to add comments or to break a large function into smaller functions. 711 | 712 | ### Q&A Strategy Prompt 713 | 714 | ``` 715 | @workspace Propose a file/folder structure for this project. Ask me a series of yes/no questions that will help you provide a better recommendation. 716 | ``` 717 | 718 | ### Pros and Cons Prompt 719 | 720 | ``` 721 | What are a few different ways that I can implement this db connection logic. Give me the pros and cons of each strategy. #file:DbRepository.ts 722 | ``` 723 | 724 | ### Stepwise Chain of Thought Prompt 725 | 726 | ``` 727 | help me refactor the code in #file:ProductService.ts. Go one step at a time. Do not move to the next step until i give the keyword "next". Begin. 728 | ``` 729 | 730 | ### Role Prompt 731 | 732 | ``` 733 | You are a skilled instructor who makes complex topics easy to understand. You come up with fun exercises so that your students can learn by doing. Your goal is to teach students to be proficient with regex. Move one step at a time and wait for the student to provide the correct answer before you move on to the next concept If the student provides the wrong answer, give them a hint. Begin. 734 | ``` 735 | 736 | ### More prompting techniques 737 | 738 | - Zero-shot Prompting 739 | - Few-shot Prompting 740 | - Chain-of-Thought Prompting 741 | - Self-Consistency 742 | - Generate Knowledge Prompting 743 | - Prompt Chaining 744 | - Tree of Thoughts 745 | - Retrieval Augmented Generation 746 | - Automatic Reasoning and Tool-use 747 | - Automatic Prompt Engineer 748 | - Active-Prompt 749 | - Directional Stimulus Prompting 750 | - Program-Aided Language Models 751 | - ReAct 752 | - Reflexion 753 | - Multimodal CoT 754 | - Graph Prompting 755 | 756 | ### Example prompts for Copilot Chat 757 | 758 | #### Ask general software questions 759 | 760 | You can ask Copilot Chat general software questions. For example: 761 | 762 | - tell me about nodejs web server frameworks 763 | - how to create an express app 764 | - @terminal how to update an npm package 765 | 766 | #### Ask questions about your project 767 | 768 | You can ask Copilot Chat questions about your project: 769 | 770 | - what sorting algorithm does this function use 771 | - @workspace how are notifications scheduled 772 | - #file:gameReducer.js #file:gameInit.js how are these files related 773 | 774 | To give Copilot the correct context, try some of these strategies: 775 | 776 | - Highlight relevant lines of code 777 | - Use chat variables like #selection, #file, #editor, #codebase, or #git 778 | - Use the @workspace chat participant 779 | 780 | #### Write code 781 | 782 | You can ask Copilot to write code for you. For example: 783 | 784 | - write a function to sum all numbers in a list 785 | - add error handling to this function 786 | - @workspace add form validation, similar to the newsletter page 787 | 788 | When Copilot returns a code block, the response includes options to copy the code, or to insert the code at your cursor, into a new file, or into the terminal. 789 | 790 | #### Set up a new project 791 | 792 | Use the /new slash command to set up a new project. For example: 793 | 794 | - /new react app with typescript 795 | - /new python django web application 796 | - /new node.js express server 797 | 798 | Copilot will suggest a directory structure and provide a button to create the suggested files and contents. To preview a suggested file, select the file name in the suggested directory structure. 799 | 800 | Use the /newNotebook slash command to set up a new Jupyter notebook. For example: 801 | 802 | - /newNotebook retrieve the titanic dataset and use Seaborn to plot the data 803 | 804 | #### Fix, improve, and refactor code 805 | 806 | If your active file contains an error, use the /fix slash command to ask Copilot to fix the error. 807 | 808 | You can also make general requests to improve or refactor your code. 809 | 810 | - how would you improve this code? 811 | - translate this code to C# 812 | - add error handling to this function 813 | 814 | #### Detect security issues and propose remediations 815 | 816 | ``` 817 | This code looks insecure. Help me understand the issue and how to resolve it. 818 | ``` 819 | 820 | 821 | #### Write tests 822 | 823 | Use the /tests slash command to ask Copilot to write tests for the active file or selected code. For example: 824 | 825 | - /tests 826 | - /tests using the Jest framework 827 | - /tests ensure the function rejects an empty list 828 | 829 | The /tests slash command writes tests for existing code. If you prefer to write tests before writing code (test driven development), omit the /tests command. For example: 830 | 831 | - Add tests for a JavaScript function that should sum a list of integers 832 | 833 | #### Ask questions about Visual Studio Code 834 | 835 | Use the @vscode chat participant to ask specific questions about Visual Studio Code. For example: 836 | 837 | - @vscode tell me how to debug a node.js app 838 | - @vscode how do I change my Visual Studio Code colors 839 | - @vscode how can I change key bindings 840 | 841 | #### Ask questions about the command line 842 | 843 | Use the @terminal chat participant to ask specific questions about the command line. For example: 844 | 845 | - @terminal find the largest file in the src directory 846 | - @terminal #terminalLastCommand to explain the last command and any errors 847 | 848 | #### Copilot on GitHub Mobile 849 | 850 | - Explain this file 851 | - Summarize this issue 852 | - What changes are introduced in this PR? 853 | - How can I improve these changes? 854 | - What's the latest release in this repo? 855 | - What are the key points in this discussion? 856 | 857 | ##### References 858 | 859 | - [Prompt engineering for GitHub Copilot](https://docs.github.com/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot) 860 | - [Example prompts for Copilot Chat](https://docs.github.com/en/copilot/using-github-copilot/example-use-cases/example-prompts-for-copilot-chat) 861 | - [Prompting Techniques](https://www.promptingguide.ai/techniques) 862 | - [Prompt crafting](https://code.visualstudio.com/docs/copilot/prompt-crafting) 863 | - [How to write better prompts](https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/) 864 | - [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/) 865 | - [Essential AI prompts & strategies for developers](https://www.youtube.com/watch?v=H3M95i4iS5c) 866 | - [Prompting with Copilot](https://www.youtube.com/watch?v=ImWfIDTxn7E) 867 | - [Awesome ChatGPT Prompts](https://github.com/f/awesome-chatgpt-prompts) 868 | 869 | 870 | --- 871 | 872 | ## Bibliography 873 | 874 |
875 | Basics 876 | 877 | - [Features](https://github.com/features/copilot) 878 | - [GitHub Copilot in VS Code - Extension](https://marketplace.visualstudio.com/items?itemName=GitHub.copilot) 879 | - [GitHub Copilot in VS Code - Overview](https://code.visualstudio.com/docs/copilot/overview) 880 | - [Getting started with GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/getting-started) 881 | - [Getting started with GitHub Copilot Chat in VS Code](https://code.visualstudio.com/docs/copilot/getting-started-chat) 882 | - [Code completions with GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/ai-powered-suggestions) 883 | - [Using Copilot Chat in VS Code](https://code.visualstudio.com/docs/copilot/copilot-chat) 884 | - [Copilot Chat in your workspace](https://code.visualstudio.com/docs/copilot/workspace-context) 885 | 886 |
887 | 888 |
889 | Prompt engineering 890 | 891 | - [A Beginner's Guide to Prompt Engineering with GitHub Copilot](https://dev.to/github/a-beginners-guide-to-prompt-engineering-with-github-copilot-3ibp) 892 | - [Prompt engineering for AI](https://dev.to/github/prompt-engineering-for-ai-what-is-prompt-engineering-and-how-to-get-good-results-from-ai-engines-5ch6) 893 | - [How GitHub Copilot is getting better at understanding your code](https://github.blog/ai-and-ml/github-copilot/how-github-copilot-is-getting-better-at-understanding-your-code/) 894 | - [Prompt Engineering Guide](https://www.promptingguide.ai/) 895 | -[Master the core principles of prompt engineering with GitHub Copilot](https://www.youtube.com/watch?v=hh1nOX14TyY) 896 |
897 | 898 |
899 | Blogs 900 | 901 | - [GitHub Copilot Blog](https://github.blog/tag/github-copilot/) 902 | - [Copilot code suggestions in IDE](https://docs.github.com/en/copilot/using-github-copilot/getting-code-suggestions-in-your-ide-with-github-copilot) 903 | - [Chat in IDE](https://docs.github.com/en/copilot/using-github-copilot/asking-github-copilot-questions-in-your-ide) 904 | 905 |
906 | 907 |
908 | Sessions 909 | 910 | - [GitHub Copilot Series](https://www.youtube.com/playlist?list=PLj6YeMhvp2S5_hvBl2SE-7YCHYlLQ0bPt) 911 | - [GitHub Copilot in VSCode: Top 10 Features Explained](https://www.youtube.com/watch?v=2nPoiUJpDaU) 912 | - [Easy web deployment with GitHub Copilot](https://www.youtube.com/watch?v=w-K6awH2TEM) 913 | - [Copilot Best Practices (What Not To Do)](https://www.youtube.com/watch?v=2q0BoioYSxQ&t) 914 | 915 |
916 | 917 | 918 |
919 | Tips and Tricks 920 | 921 | - https://github.com/github/copilot-hack/blob/main/copilot-tips.md 922 | - https://github.blog/developer-skills/github/how-to-use-github-copilot-in-your-ide-tips-tricks-and-best-practices/ 923 | - https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/ 924 | - https://medium.com/@lars.vandedonk/10-tips-and-tricks-to-master-github-copilot-9b28625a2041 925 | - https://code.visualstudio.com/docs/copilot/prompt-crafting 926 | - https://docs.github.com/en/copilot/using-github-copilot/prompt-engineering-for-github-copilot 927 | - https://learn.microsoft.com/en-us/visualstudio/ide/copilot-chat-context?view=vs-2022 928 | - https://medium.com/@arikbidny/mastering-github-copilot-essential-tips-tricks-and-prompt-engineering-for-optimal-coding-efd420864c3d 929 | - https://www.linkedin.com/pulse/github-copilot-best-practices-tips-tricks-how-get-ai-its/ 930 | - https://nira.com/github-copilot/ 931 | 932 |
933 | 934 | 935 |
936 | Best Practices 937 | 938 | - [Copilot IDE - Tips, tricks, and best practices](https://github.blog/developer-skills/github/how-to-use-github-copilot-in-your-ide-tips-tricks-and-best-practices/) 939 | - [How to use GitHub Copilot: Prompts, tips, and use cases](https://github.blog/developer-skills/github/how-to-write-better-prompts-for-github-copilot/) 940 | - [Best practices for using GitHub Copilot in VS Code](https://code.visualstudio.com/docs/copilot/prompt-crafting) 941 | 942 |
943 | 944 |
945 | Use cases 946 | 947 | - [How I used GitHub Copilot to build a browser extension](https://github.blog/developer-skills/github/how-i-used-github-copilot-to-build-a-browser-extension/) 948 | 949 |
950 | 951 |
952 | GitHub Workshops 953 | 954 | - [ps-copilot-sandbox](https://github.com/ps-copilot-sandbox) 955 | - [Copilot Fundamentals Training Exercises](https://github.com/ps-copilot-sandbox/copilot-fundamentals-training) 956 | - [Custom GitHub Copilot workshop with Python Django, ReactJS, and PostgreSQL stacks](https://github.com/ps-copilot-sandbox/copilot-custom-workshop-django-react-web) 957 | - [Copilot Javascript Unit Testing Demo Workshop and examples](https://github.com/ps-copilot-sandbox/copilot-javascript-testing-workshops) 958 | - [Flight Delay Hackathon TS/Python](https://github.com/ps-copilot-sandbox/flight-delay-hackathon) 959 | - [GitHub Copilot demos for Javascript](https://github.com/ps-copilot-sandbox/copilot-demo-github-javascript) 960 | - [Solving algorithm puzzle of distributing swag items - Copilot with Python](https://github.com/ps-copilot-sandbox/python-simpleLockerPuzzle-demo) 961 | 962 |
963 | 964 | --------------------------------------------------------------------------------