Brent Laster Foreword by Andrew Stellman Learning GitHub Copilot Multiplying Your Coding Productivity Using AI
ISBN: 978-1-098-16465-2 US $69.99 CAN $87.99 SOF T WARE DEVELOPMENT Supercharge your coding productivity with generative AI using GitHub Copilot. In this practical guide, author Brent Laster guides you through using generative AI for writing better code faster, generating tests with ease, creating polished documentation at any stage of development, and more. You’ll also explore advanced uses—like leveraging Copilot’s Agent functionality to add features autonomously and reviewing pull requests automatically. Learning GitHub Copilot is for developers, testers, DevOps engineers, and software professionals at all levels. Alongside the fundamentals, you’ll dive into Copilot Edits, Agent mode, and Copilot Vision. You’ll also learn how to create your own Copilot extensions to expand its capabilities. Whether you’re working in Python, JavaScript, or any other language, this book helps you confidently integrate AI into your development workflow. • Harness real-time AI insights to explore and understand unfamiliar code and algorithms • Master inline completions and the chat interface to automate common tasks • Turn natural language prompts into complete functions, tests, and docs quickly and easily • Optimize AI results with context and prompts to get targeted solutions • Streamline feature development and refactors with AI assistance in your IDE Learning GitHub Copilot “Learning GitHub Copilot is practical, thoughtful, and grounded in how developers work on code every day.” Andrew Stellman, developer, team lead, instructor, and author “Every programmer should understand AI-assisted programming. And GitHub Copilot is the clear leader. Brent’s book is the ultimate guide to mastering this powerful tool.” Tom Taulli, author of AI-Assisted Programming Brent Laster is a global trainer, author, and speaker on AI, DevOps, and related topics. He is a former developer and director with many years of experience working with top technology companies. He is also the founder of Tech Skills Transformations, a company dedicated to helping others level up and get the technical skills and understanding they need to achieve their goals.
Praise for Learning GitHub Copilot Learning GitHub Copilot is practical, thoughtful, and grounded in how developers work on code every day. —Andrew Stellman, developer, team lead, instructor, and author Every programmer should understand AI-assisted programming. And GitHub Copilot is the clear leader. Brent’s book is the ultimate guide to mastering this powerful tool. —Tom Taulli, author of AI-Assisted Programming This book is the missing link between using Copilot and truly mastering its potential. It’s an invaluable resource for developers who want to elevate their workflow. —Balaji Dhamodharan, data science leader This book will help to unlock the future of coding. It’s a guide to the tool that simplifies the entire coding process, code explainability, and optimization by offering clear lessons and examples on Github Copilot. Whether you’re new to coding or a seasoned coder, this book will help you increase your productivity. —Aditya Goel, GenAI consultant and assistant vice president at a global bank
Learning GitHub Copilot is a comprehensive description of Github Copilot’s features along with usage examples. I found it very informative. —Chhaya Methani, Microsoft This book expertly balances foundational knowledge with practical applications, making it a must read for anyone eager to explore AI-enhanced software development. —Sundeep Goud Katta, lead software engineer
Brent Laster Foreword by Andrew Stellman Learning GitHub Copilot Multiplying Your Coding Productivity Using AI
978-1-098-16465-2 [LSI] Learning GitHub Copilot by Brent Laster Copyright © 2025 Tech Skills Transformation, LLC. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or corporate@oreilly.com. Acquisitions Editor: John Devins, Louise Corrigan Development Editor: Michele Cronin Production Editor: Jonathon Owen Copyeditor: Sharon Wilkey Proofreader: Heather Walley Indexer: Potomac Indexing, LLC Cover Designer: Susan Thompson Cover Illustrator: Monica Kamsvaag Interior Designer: David Futato Interior Illustrator: Kate Dullea July 2025: First Edition Revision History for the First Edition 2025-07-09: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781098164652 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Learning GitHub Copilot, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the author and do not represent the publisher’s views. While the publisher and the author have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the author disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights.
To Anne-Marie, coauthor of the amazing book that is our life together.
(This page has no text content)
Table of Contents Foreword. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii 1. Foundations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 What Is GitHub Copilot? 2 How Does Copilot Work? 3 Large Language Models 4 Code and Generative AI 5 High-Level Flow 7 Usage Considerations 11 Timeliness 12 Relevance 13 Completeness 13 Accuracy 14 Privacy 14 Security 16 Copilot Versus ChatGPT 17 Copilot Plans 18 Conclusion 22 2. Coding with Copilot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Inline Suggestions 24 Getting Additional Suggestions 28 Leveraging Comments 30 Directive Comments 31 Questions via Comments 34 Copilot and the Context Menu 35 vii
Using Copilot to Review Your Code 39 Using Copilot’s Default Review Options 39 Creating Custom Review Instructions 42 Configuring Copilot in the IDE 44 Conclusion 49 3. Chatting with Copilot in the IDE. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 Accessing Chat’s Main Interface 52 Understanding Chat Output 55 Managing Chat Sessions 59 Prompt Engineering for Copilot 62 Making Effective Use of the Prompt Dialog 63 Adding Context Elements 64 Adding Participants 66 Understanding Options for Submitting Your Prompt 67 Using the Editor Inline Chat Interface 69 Using the Quick Chat Interface 73 Chat Shortcuts 74 Chat Participants 77 @workspace 78 @vscode 80 @terminal 81 Chat Variables 83 Chat in the Terminal 86 Creating Custom Code-Generation Instructions 87 Dealing with Hallucinations and Bad Answers 90 Conclusion 91 4. Advanced Editing and Autonomous Workflows in the IDE. . . . . . . . . . . . . . . . . . . . . . . . . 93 Predictive Edits with Next Edit Suggestions 93 Copilot Edits 97 Agent Mode 104 Copilot Vision 111 Debugging with Copilot 119 Conclusion 123 5. Testing with Copilot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 How Do I Test? 126 Creating Unit Tests 132 Using the /tests Command 133 Creating Tests from Explicit Prompts 136 Creating Tests from Comments 137 viii | Table of Contents
Validating Inputs 139 Creating Integration Tests 141 Defining Custom Testing Instructions 144 Testing Before the Coding and Leveraging Frameworks 145 Leveraging Agent Mode for Testing Workflows 151 Conclusion 154 6. Using Copilot to Document and Explain Code. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155 Documenting Content 156 Generating Documentation Inline 156 Generating Documentation Through Chat 159 Generating Framework-Compatible Documentation 162 Generating Documentation for APIs 164 Creating Functional Documentation 165 Extracting Summary Documentation from the Code 167 Explaining Content 169 Understanding Code in a Language New to You 169 Understanding Generated Code and Conventions 170 Explaining the Logic Used in a Section of Code 172 Explaining What Might Go Wrong with a Set of Code 173 Explaining Items from the Terminal 174 Explaining How to Do Something in the Terminal 177 Conclusion 178 7. Keeping Copilot Timely and Relevant. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 Where Context Originates 182 How Timeliness and Relevancy May Be Affected 183 Training Cutoffs 183 Hallucinations 186 Lack of Real-Time Validation 187 Misplaced Context 188 Missing Context 189 User-Based Coping Strategies 191 Telling Copilot What to Use for Context 191 Changing the Model 194 Querying the Model to Determine Which Version Is Current 195 Guiding Copilot by Example 198 Adding Context to Make Code More Relevant 199 Conclusion 202 8. Other Ways to Leverage Copilot. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203 Using Copilot with SQL 204 Table of Contents | ix
Queries 204 Stored Procedures 207 Optimizations 209 Working with YAML and Kubernetes 210 Generating Regular Expressions 215 Automatic Data Generation 216 GitHub CLI and Copilot 218 Conclusion 220 9. Using Copilot in GitHub. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 223 Using Chat with GitHub Repositories 224 Using Copilot for Change Workflows 231 Using Copilot with Pull Requests 237 Having Copilot Review Pull Requests 238 Leveraging Copilot to Simplify Initiating Pull Requests 241 Exploring Code Changes with Copilot 245 Using Copilot with GitHub Issues 249 Conclusion 251 10. Extending Copilot’s Functionality. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253 Copilot Extensions Versus VS Code Extensions for Copilot 253 What Are Copilot Extensions? 254 Getting Copilot Extensions from the Marketplace 255 Understanding Copilot Extension Implementation Types 263 Assembling Building Blocks for Extensions 264 GitHub Apps 264 Endpoint Server 266 Implementing an Extension as an Agent 266 Performing a Basic Implementation 267 Configuring a GitHub App for a Copilot Agent Extension 272 Implementing an Extension via Skillsets 280 Performing a Basic Implementation 281 Configuring a GitHub App for a Copilot Extension Using Skillsets 285 Creating VS Code Extensions for Copilot 289 Conclusion 296 Index. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 297 x | Table of Contents
Foreword I’ve spent a lot of time helping developers learn how to use AI tools as part of their daily work—by mentoring people, running training sessions, and writing about them in books—and I’ve relied on those same tools myself while building real software. One really stands out to me: GitHub Copilot. It’s a game-changer. I use it all the time. I’ve been coding for decades, and like every developer, sometimes I get stuck. Maybe it’s when I’m using a new technology or API, and I’m dreading going through poorly written documentation. Maybe it’s when I’m refactoring a particularly nasty bit of code that everyone on my team has been avoiding. Or maybe I just don’t feel like writing dull boilerplate code that has to get written but is going to cost me a half hour of my life that I’ll never get back. That’s where Copilot can really make a difference. It might give me all the code I need, complete and working. Or it might give me just a starting point—something we’ll iterate on together. Either way, it’s valuable to me, and it almost always gives me enough to get things moving again. But working with Copilot isn’t a one-way street—it comes with responsibility. Copilot can write code that compiles, runs, even passes tests. But if you don’t understand what it gave you, or why it works—or, most importantly, how you got it to produce that result—you’re not really in control of what’s going into your codebase. This book puts you in control. Good developers need to do more than just learn Copilot’s features. We need to learn an effective way to work with AI, one that supports us in what we already do: write code, make decisions, and solve problems alongside other people. That learning starts with understanding that Copilot isn’t perfect—and it isn’t trying to be perfect. It’s trying to help you move forward by offering suggestions based on the patterns and context in your code. And a lot of the time, it gets satisfyingly close. xi
But like any tool, it works best when you know how to guide it—and when to slow down and double-check what it gave you. And that’s why this book matters. Learning GitHub Copilot is practical, thoughtful, and grounded in how developers work on code every day. Brent Laster has done a masterful job because he doesn’t just list off Copilot’s features; he shows you how to use them when you’re doing real work—when things are messy, nonlinear, and often under a deadline. He starts with Copilot’s built-in strengths, like explaining code and writing tests, and then moves into showing you how it helps with things that trip people up—SQL queries, regular expressions, YAML files, and all the other small (and sometimes not so small!) tasks that eat up time. I appreciate all the examples throughout the book because they reflect the kinds of problems developers face in real life. I think you’ll appreciate them too. Copilot works as an important everyday development tool—not because it promises magic, but because it respects how developers think. This book does the same thing. It assumes you’re thoughtful, capable, and still learning, and it gives you space to make good decisions with the tool instead of just following along. In my own teaching and writing, I’ve seen how quickly developers improve when they get clear, honest guidance—especially when it helps them make better decisions when they’re doing real work. That’s exactly what this book provides. It’s not hype. It’s not oversimplified. It’s a practical guide to a powerful tool, written for developers who want to stay in control of their code. If you’re new to Copilot, this book is a great place to start. If you’ve been using it for a while, you’ll pick up techniques that make it more effective and easier to trust. And if you’ve been skeptical, that’s healthy. This book meets you where you are, and it’ll show you how to get the most out of the tool without giving up what makes you a good developer. — Andrew Stellman Developer, team lead, instructor, and author of Learning Agile, Head First C#, and many other O’Reilly books Brooklyn, New York, April 2025 xii | Foreword
Preface The winds and the waves are always on the side of the ablest navigators. —Edward Gibbon I’ve been writing code for a very long time now—longer than I care to admit—and I’ve seen the winds and waves of change that have swept across the software develop‐ ment industry multiple times (and written about or trained on many of them). From the internet to CI/CD to containers to cloud, the paradigm shifts have upended the old ways of doing things, and spawned innovation, incredible tools, and turnarounds in our approaches to creating software. And, broadly, those shifts have also generated uncertainty and confusion as individuals tried to understand them and figure out how to adapt and apply their skills.. Now we have generative AI, arguably the biggest wave of change we have encoun‐ tered in tech, especially for those developing software. Just like the others, it is inspir‐ ing massive innovation and driving massive changes. But in some respects, it resembles more of a tsunami where new tools, new models, and new capabilities exhibit tremendous power, all while coders are being swept along and trying to keep our collective heads above water. This book is my attempt to help you surf the wave—at least as far as using the AI powerhouse that is GitHub Copilot. I won’t claim it will answer all your questions, but hopefully it will help you get to a very good comfort level with the tool, let you understand its capabilities (from basic to advanced topics), and inspire you to make your coding tasks simpler and easier than they’ve ever been before. I’ve been using Copilot in multiple ways for several years: in personal projects, as a former DevOps director piloting it in a corporate R&D organization, as a trainer on Copilot for multiple vendors, as a skeptic, and as an enthusiast. Regarding those last two roles, I wasn’t as impressed with Copilot in its early days as I was with other tools. But over the course of writing this book, developing courses on it, and watching it evolve, it’s become one of the primary tools I use on a regular basis when working in xiii
my integrated development environment (IDE) or in GitHub. Copilot has come a long way, even in the last few months. There’s been some impressive progress with features and functionality. I’ve tried to capture as many of those as I could in the book. One of my goals in writing these chapters is to share the benefits and the challenges of using Copilot, and try to help you avoid surprises. So, before you jump in, here’s a few things to be aware of: • For the last year or so, GitHub Copilot has changed the appearance and location of at least one dialog or control with nearly every release. I’ve done my best to update screenshots and descriptions as they are at the time of writing. But it’s cer‐ tain that some will have been moved, changed in appearance, etc. by the time you are reading this. • Copilot is now releasing new features on a fairly regular basis. So, over time there will be new features that are not represented in the book. Likewise, some func‐ tionality in Copilot is noted as being in preview or experimental as of the time of writing. Those may have been moved to production by the time you read this or may have been removed if they didn’t make the cut. • Copilot runs in many different environments, including multiple IDEs. In each IDE, there can be (and are) differences in integration, appearance, and even func‐ tionality. GitHub has historically prioritized Visual Studio Code and the environ‐ ments that use the same interface (like GitHub Codespaces) to roll out new features and to contain the superset of functionality. For that reason, all screen‐ shots and examples in the book are done with either VS Code or in a Codespace. • For those of you using non-VS Code IDEs, some features or functions may have not been implemented by GitHub in that environment. Please consult the Copilot and/or IDE documentation for the latest information. • Similarly, IDEs that integrate with Copilot can usually be run on multiple plat‐ forms including Windows, Mac, and Linux. Since it’s not practical to cover all the variations in the book, the examples were done on a Mac. So the windows and controls shown may reflect the Mac style. Also keyboard shortcuts will differ among the platforms. To avoid having to list all keyboard shortcuts each time we reference them, we’ve adopted Meta as a notation that means to insert the appro‐ priate shortcut for your platform. All of these things apply across the entire structure of the book, which was written to take you from the What is it basics to the advanced Here’s how you go beyond the core functionality. Most of the chapters can be read standalone, and you should feel free to skip between chapters as your needs and comfort level dictate. xiv | Preface
The Structure of This Book Since you’re reading this book, I imagine you’re interested in GitHub Copilot as a potential AI coding assistant or at least looking to learn more about how it and simi‐ lar coding assistants can be used to help you work more quickly and efficiently. To help with that, an outline of the book’s structure follows. Chapters 1, 2, and 3 guide you through the foundations you need to understand what Copilot is and how to interact with it in its two main modes—code completion and the chat (conversational) interface. Chapter 4 explores advanced ways of interacting with Copilot to do more autono‐ mous editing as well as using Agent mode to drive changes from prompt to finish automatically. This chapter also explores the unique interface of Copilot Vision as well as debugging with Copilot. Chapters 5 and 6 show you how you can leverage Copilot to create rich sets of tests and multiple kinds of documentation to simplify those tasks and give you more time to focus on other coding tasks. Chapter 7 focuses on how you can improve the timeliness and relevance of results from Copilot. Chapter 8 explores how you can use Copilot to handle a number of less common but useful tasks. Chapter 9 explores Copilot’s chat interface in GitHub and how Copilot can help you understand more about your projects and simplify working with GitHub Issues and pull requests. Finally, Chapter 10 provides information on enhancing Copilot’s functionality through the integration of GitHub Extensions and shows how to build your own. As you start reading, you don’t have to know anything about Copilot to use this book. But I do assume you’re familiar with basic coding skills and have a basic working knowledge of Git and GitHub. The next section explains a bit more about the types of readers who may benefit from the book. Intended Audience This book is for anyone who is trying to learn more about using AI assistants for developing software. Yes, it’s specifically about GitHub Copilot, but many of the examples and flows have parallels in other AI coding tools and environments. To get the most out of this book, you should already have experience coding in an IDE and have a basic knowledge of GitHub. To be clear, this book is not going to teach you how to code, how to use an IDE, or how to use GitHub. But it will help you multiply Preface | xv
your productivity in all of those areas through using Copilot. Whether you are a soft‐ ware developer, quality engineer, SRE, or someone who is just looking to understand how an AI assistant like Copilot can benefit them, I believe you’ll find meaningful information and insights. Here are some audiences that I had in mind while writing the book and that I think can benefit from it: • Those who are new (or newish) to AI coding assistants and looking to under‐ stand what they are all about and how to best make use of them • Those who already understand the concepts and flow of using AI coding assis‐ tants and want to understand what GitHub Copilot has to offer • Those who want to understand and evaluate GitHub Copilot as a possible tool to use on a wider scale in their organization or business • Those who already have some experience with GitHub Copilot and want to take full advantage of its features and functionality • Those who are looking to understand how to compensate for some of GitHub Copilot’s shortcomings • Those who want to understand the new features of Copilot, such as Agent mode • Those who want to create their own GitHub Copilot extensions • Those who work with GitHub and want to take advantage of the Copilot integra‐ tion there If one of these fits your use case, I hope the book will provide you with the value you’re looking for. If you read it and have the opportunity, feedback is always wel‐ come through reviews or interactions at future conferences or training venues. Info about how to ride the wave of Generative AI in coding begins in Chapter 1. Happy surfing! Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program ele‐ ments such as variable or function names, databases, data types, environment variables, statements, and keywords. xvi | Preface
Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values deter‐ mined by context. This element signifies a tip or suggestion. This element signifies a general note. This element indicates a warning or caution. Using Code Examples Supplemental material (code examples, exercises, etc.) is available for download at https://github.com/techupskills/learning-github-copilot. If you have a technical question or a problem using the code examples, please send an email to support@oreilly.com. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. Preface | xvii
We appreciate, but generally do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: “Learning GitHub Copilot by Brent Laster (O’Reilly). Copyright 2025 Tech Skills Transformation, LLC, 978-1-098-16465-2.” If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at permissions@oreilly.com. O’Reilly Online Learning For more than 40 years, O’Reilly Media has provided technol‐ ogy and business training, knowledge, and insight to help companies succeed. Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit https://oreilly.com. How to Contact Us Please address comments and questions concerning this book to the publisher: O’Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-889-8969 (in the United States or Canada) 707-827-7019 (international or local) 707-829-0104 (fax) support@oreilly.com https://oreilly.com/about/contact.html We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at https://oreil.ly/learning-github-copilot. For news and information about our books and courses, visit https://oreilly.com. Find us on LinkedIn: https://linkedin.com/company/oreilly-media. Watch us on YouTube: https://youtube.com/oreillymedia. xviii | Preface
Loading comments...
Reply to Comment
Edit Comment