📱

Get Our Mobile App

Take your business learning on the go!

Download on the App StoreGet it on Google Play

GitHub Copilot Crash Course: Ultimate Guide for Developers

KodeKloud38:31

Transcription

Hey everyone, I'm Jeremy Morgan from CodeCloud. Welcome to today's lesson from our GitHub Copilot and action course. In this video, you'll discover how GitHub C-Pilot can revolutionize your coding experience. If you're eager to explore more and unlock the full potential of AI-driven development, be sure to check out the complete course details below. Let's dive right in.

Welcome everyone. Today we're exploring an exciting evolution in software development. AI pair programming with GitHub Copilot. This represents a fundamental shift in how we write code, combining human creativity with AI assistance to enhance our software development workflow. And here's what we're going to talk about. We'll talk about pair programming and AI pair programming, and we'll compare the two. We'll talk about the benefits of pair programming with Copilot, and we'll look at the workflow transformation before and after Copilot, and we'll cover some best practices: when to use Copilot and when to be a little cautious of it.

Let's start with understanding traditional pair programming. Now, pair programming has developers collaborating side by side, and this has been a proven practice in software development for decades. It involves two developers working together on the same code, typically with one writing code (the driver) and one reviewing and thinking strategically (the navigator).

AI pair programming transforms this traditional model. Now, GitHub Copilot acts as your AI pair programming partner, offering real-time suggestions based on your context. Unlike human pair programming, it's available 24/7 and it scales across entire teams. Most importantly, it offers suggestions without ego or judgment, so you're always in control of accepting or rejecting its input.

Let's talk about traditional pair programming versus AI pair programming. Now, here we see the three key components of traditional pair programming: knowledge exchange between developers, real-time feedback as code is written, and immediate error catching. This human-to-human interaction has been the foundation of collaborative coding practices, and this is how pair programming has been for a long time.

The AI pair programming model transforms these elements into pattern recognition from vast code repositories, instant suggestions that appear as you type, and automated documentation generation. This creates a continuous feedback loop that enhances your coding process.

Now, traditional pair programming brings unique advantages that remain valuable: human creativity and intuition for complex problems, architectural decision-making that requires a deep understanding, knowledge transfer between team members, and the social aspects of team building that strengthen your organization. Copilot complements these traditional benefits with its own strengths: instant access to common coding patterns, rapid prototyping capabilities, consistent code style suggestions across your project, and automated documentation assistance that helps maintain clear code documentation.

Now, let's talk about before Copilot came along. The traditional development workflow often involved the cycle of writing code, checking documentation, searching Stack Overflow for solutions, reviewing and refactoring. And this process could be time-consuming and it often interrupted the coding flow.

With Copilot, the workflow becomes more streamlined. You write code while receiving real-time suggestions, and you can accept, modify, or reject these suggestions instantly, get documentation as you code, and review with AI assistance. So the learning process becomes integrated into your coding flow.

Now, when should you use Copilot? Well, C-Pilot excels in specific scenarios: handling repetitive tasks and boilerplate code, suggesting common API integration patterns, generating test cases, writing documentation, and converting natural language comments into functional code. These are areas where AI assistance can significantly boost productivity.

However, there are still situations where human oversight is crucial: security-critical code that needs careful review, complex business logic that requires deep domain knowledge, performance-critical sections that need optimization, and when working with licensed or proprietary algorithms. These are times that you want to be cautious when working with GitHub Copilot.

Now, AI pair programming is a powerful tool, but it's not a replacement for human developers. So, moving forward, start small with simple tasks. Learn from the suggestions it provides. Share your experiences with your team and keep experimenting to find the best way to integrate it into your workflow. The goal is to enhance our software developers, not replace them.

So today, we're diving into GitHub C-Pilot, a groundbreaking AI-powered coding assistant that's transforming how developers write code. Now, this tool represents a significant advancement in AI-assisted development, combining OpenAI's large language models with GitHub's vast code repository knowledge. Let's look at some of the strengths and limitations of GitHub Copilot.

Let's start with a high-level overview of GitHub Copilot's architecture. On the left, we have its core strengths: code generation, productivity features, and learning capabilities. On the right, we see its limitations around code quality, security, and dependencies. So, this balanced understanding is crucial for using C-Pilot effectively.

Now, let's break down Copilot's fundamental strengths. First, it dramatically increases productivity through real-time suggestions. The boilerplate generation is particularly impressive. Copilot can write repetitive code patterns instantly. Most importantly, it shows remarkable context understanding, analyzing your existing code to provide relevant suggestions. Beyond basic coding, C-Pilot supports over 200 programming languages, making it incredibly versatile. And it doesn't just write code. It can generate documentation on the fly and even suggest relevant test cases. And we'll do that in this course. This comprehensive support makes it valuable across the entire software development life cycle.

These features represent Copilot's daily impact on your development workflow. The real-time code completion is context-aware, offering you suggestions as you type. It recognizes common API patterns from your codebase, automates repetitive tasks, and maintains a consistent coding style. The integrated workflow means it works seamlessly within your IDE.

And Copilot isn't just a code generator. It's a learning tool. It recognizes patterns in your code, suggests best practices, and offers alternative approaches to problems. On the collaboration side, it facilitates knowledge sharing by suggesting standardized approaches. It assists with code reviews and helps maintain consistent documentation across teams. The benefits extend beyond just faster coding. Developers report reduced cognitive load since Copilot handles routine tasks. It accelerates prototyping significantly, provides built-in documentation help, and serves as a learning tool for new patterns. The team benefits include knowledge sharing and streamlined code reviews.

But it's crucial to understand Copilot's limitations. It requires a stable internet connection. It can slow down with large codebases and uses significant computational resources. Sometimes it misunderstands context, and there are API rate limits to consider. Regarding code quality, be aware that C-Pilot might suggest outdated patterns or introduce security vulnerabilities. The code quality can be inconsistent, and solutions tend to be generic rather than optimized for your specific needs. It also lacks deep understanding of project-specific requirements.

Now, security is a major consideration. There are data privacy concerns when sharing code context, potential for code leakage, and limited security features. The dependency on cloud services and compliance requirements needs careful consideration in enterprise environments. Remember, Copilot complements but it doesn't replace human understanding. It requires a careful review of suggestions, and it might struggle with complex algorithms. There's also a learning curve to using it effectively.

Now, to implement Copilot successfully, establish clear usage guidelines. Monitor its impact on productivity. Gather feedback from team members and continuously refine your approach based on your experience. To get the most from Copilot, write clear comments that guide its suggestions. Use descriptive variable names. Break down complex tasks. Carefully review all the suggestions and learn from its patterns to improve your own coding practices. Watch out for these common mistakes: over-relying on generated code without understanding it, accepting suggestions without review, using it for complex algorithms that it might not handle well, and ignoring security implications of the generated code.

In conclusion, C-Pilot is a powerful tool with clear boundaries. Success comes from balancing automation with oversight, focusing on appropriate use cases, maintaining security awareness, and continuously evaluating its effectiveness in your workflow. When it's used properly, it can significantly enhance your development process and avoid some of those potential pitfalls.

So, I have Visual Studio Code open here with my project that I'm going to be starting out, and I want to install GitHub Copilot. So, we'll go to View and Extensions. And in here, we can search for GitHub, and you can see there's quite a few of them, but what we want is GitHub Copilot. And we'll click on Install, and it's going to install here. And it will log in. Now, sometimes this will ask for a GitHub login. It'll pop up the GitHub window. So, you just log in through the web browser and then come in through here. But now we have the GitHub Copilot extension here, and we can say GitHub Copilot, and you can see here we have GitHub Copilot Chat, which is included as a secondary extension, and I'm not sure why, but you want to make sure that Chat is installed also. If you just install GitHub Copilot from here, it will install this, and you'll be ready to go. And as you can see, we go here, go down to the lower right-hand side, click on here, and we have status: Ready.

Now, in here, we can go to GitHub Copilot Chat, which we just brought up earlier, and this is a chat window. Now, what makes this different is this isn't just a chat window that is, you know, a small window to the outside world. This is a chat window that goes to GitHub Copilot. However, this chat window is aware of the code that's inside your editor here. That's what makes it special. So, you know, if you've been coding with something like ChatGPT, you have a web browser open on the other side, it's different from that because it can look at your existing code and use that as context with the things that you're asking.

Now, if we click up here, we can see the GitHub Copilot Chat. You can say Disable Completions. Now, I'll be honest that sometimes I do this when I just want to get in here and write code really fast and get something knocked out. I will disable the completions because I don't want them to get in the way. However, this is just a personal preference type of thing. We're going to show the completions here in this course and show how they work, but you can disable them. If you just want to code really fast and then go into Copilot and have it check it later, this is a great way to do it.

You can edit some settings here. So, GitHub Copilot has a bunch of settings. So, let's take a look at those. We have Window > New Window Profile. This specifies the profile to use when opening a new window, right? So, I have a few different profiles on here. And you can select these, and this will change your window profile features. Really, it just has this notebook as a default formatter. So, this is a formatter that takes precedence over all other formatter settings, right?

And then we have Extensions here. We can see that I already have GitHub with Git Authentication. So that's why I didn't have to authenticate when I installed GitHub Copilot is because I already had Git Authentication installed here. And this basically says, you know, this is so you can connect to other Git things. And here we have GitHub Copilot. You can enable it or disable it for certain languages. So here we have the asterisk equals true, and that means everything's set to true. So that means as a default, the first rule is GitHub Copilot can see any text file in my whole project. However, you can go here and override and say, I don't want it for plain text. I don't want it for markdown, scrim input, or YAML. Um, these are all things I sometimes do enable the YAML if I'm working on YAML stuff. Sometimes it does make some good suggestions, and it looks at your syntax. Um, Copilot isn't necessarily going to understand the context of what you're working on all the time, but it can be helpful with YAML once in a while. And in here, you can set in things like use instruction files, you can have a debug command enabled, etc. These are all part of preview right now, but by the time this you're watching this, they may be already in production.

We have Experimental Settings. I don't mess with these too much because, as it says, experimental. So these might be some really cool features that you're super interested in, but you don't have to do it, and they're all experimental, so who knows, you know, how it's going to work out. Generally, I kind of ignore these. And they have Workspace Settings here that you can actually edit a JSON file and go right here, and you can put in instructions into settings.json JSON if you'd like. So, I'm going to not save that.

And now we've got GitHub Copilot. We've got the chat window open here. Um, next we're going to jump in and kind of show how the code completion works and how things work with GitHub Copilot.

And our final section, let's look at mastering GitHub Copilot: the core features. Now, here's what we're going to do in this module. We're going to start with an introduction. We're going to Okay. So, we're going to do some basic code completion in GitHub Copilot so I can kind of show you how it works. So, we're going to create a Python file here. Now, you can see here immediately it says press Command key and I to ask GitHub Copilot to do something. Now, let's close this GitHub Copilot chat and just do the I, and it says, "Ask Copilot to do something." So I can say, "Hello world in Python," and I can just give it a command. It'll start churning, and there we go. And I can say, "Accept," "Discard," or I can "Regenerate" it and see if it comes up with something else. And in this case, there aren't too many variations on this. So we can hit accept, and our hello world works.

Now, let's try some other things here. Imagine we need to write a function to calculate the factorial of a number. Well, instead of typing out the entire function, which again is a pretty straightforward process, we can start with a simple docstring. And sometimes this stuff will automatically already start. As you can see, what I was going to type in here, it already knows basically what I'm going to do. But let's say that it doesn't. And so instead of this, we can put in a docstring. Now, GitHub Copilot can tell what I'm trying to do just basically based on what I've named this method and the parameter I put in. It's kind of can see what I'm trying to do. But let's show another method for doing this, and that's a docstring. And we can say, "Returns the factorial of a given number." Now, if we put in this docstring here, we can then ask Copilot to generate it, or Copilot will come up with something like this. So when you put in your docstring, you can say, "This function does X." GitHub Copilot will make an attempt to write this out for you. So that's one way that it does code completion, and it will instantly suggest the complete function implementation if it can guess what your function is trying to do. So as you saw when we typed in up here, "def factorial," etc., it kind of guessed, "I think I know what this person's trying to do," and it spits it out. But if it doesn't do that, then you can use this docstring and you can explain your function, and it will do it based on that as well.

Now, let's say we have a list of dictionaries that are representing users, and we want to extract a list of usernames. We can go down here and say, "usernames =" and you can see right here it autocompletes. This is such a common thing in Python that GitHub Copilot knows, "Hey, I think this is probably what you want to do." So we'll do that autocomplete and we'll check our terminal and let's see if it works. And it doesn't, because we need to print out usernames. So I can go here and put in a comment, "Print each username," and there's the step we're missing. And now we have Michael, Sanjie, and Jeremy. So these are two different ways. And when you start to do things that are pretty standard, pretty straightforward, GitHub Copilot does a great job of guessing what it thinks you mean and what it thinks you need. As soon as I typed in "usernames," it automatically thought, "Well, I bet this programmer is trying to find the name, not the ID, and just display the names." So, this is great.

Now, let's say we're working with a file, and we want to gracefully handle exceptions when the file isn't found. What can we do? We'll do a "try" "except." And you can see that immediately GitHub Copilot has looked at this and says, "Okay, they're trying to open data.ext. How about FileNotFoundError?" Right? That's one of the most common ones ever. So it'll do a FileNotFoundError. Next one, we say, "default_data = print(data)." Okay. And so we don't have a data.ext. So let's see what it says. "Default data." So, so we have some custom information we can put in here, and we can say, "File not found, silly." And GitHub Copilot kind of guesses what you you're going to look for and what you need. And notice we can just put "exception" in here and have any old kind of exception. We can do "GeneratorExit," "ExceptionGroup," just "Exception e." And here we can see "Error number 2: No such file or directory: data.ext," etc. But it's kind of nice that GitHub Copilot immediately says, "Hey, the first one you're probably going to need is a FileNotFoundError. We can deal with other types of exceptions later, but this is just one of the easy ways that GitHub Copilot does code completion." Okay.

And let's say we want to use a requests library to make an API call. We'll type in "import requests." GitHub Copilot knows this is probably what we want to do. We can go here, and it says, you know, that there's an error here. We have a warning. And here's a set of fixes that we can use: "Remove the unused import," "Remove all unused imports," "type: ignore," etc., or "Fix using Copilot." And you can see here it says, "You need to install the requests library using pip." All right. Awesome.

Now over here, if we go to the right, we can say "Insert into terminal," "Insert at cursor," which "Insert at cursor" isn't going to help us here because pip needs to be run down in our terminal, or we can "Copy" it. Let's click on "Insert into terminal." And now we have "pip install requests." We can install requests. We can say, "Close." There we go. Now we're good. And now if we do "response =" you can see it automatically knows probably how you're going to use requests. Based on naming that variable "response," it's going to say, "Here's a pretty typical thing that people are looking for when they're building a response. They're usually doing a request.get or a request.something." So, Copilot will likely suggest the get method and kind of guide you towards building the URL and handling the process.

Now, you can also view this kind of thing in chat, right? You can say, let's just make this an an error on purpose. Put "requests.response.code." And we can see here, "Oh, 'request' has no attribute 'name_test'." Well, let's go in here and ask GitHub Copilot, "Why isn't this working?" GitHub Copilot is going to check out my code and it says, "The 'requests' module doesn't have a 'test' method. You should use a 'get' method instead." Another way you can do this, we can say "accept." Another way you can do this, of course, is opening up the chat. So you can say, "GitHub Copilot Chat, why isn't my code working?" This sounds really basic and simple, but this is uh exactly what you can do to ask. And say, "Here it says, 'I need more information. What isn't working as expected?'" And I can just say, "It's just throwing an error." I've done some tests where this has worked out pretty well. "To help diagnose the issue, what kind of error are you getting?" And you can go in here, and again, this is part of the code completion. We can say "Apply to editor," "Insert at cursor," or "Copy." So you have those three. Each one has its pluses and minuses. I've seen it when you go to "Apply to editor," I've seen it remove code that it shouldn't remove. So, being fully transparent, this isn't always the best way. "Cursor" can be interesting because it's going to install this entire, you know, all the way up to "import requests." It's going to install all of that at the cursor. So, um, you may not want to do that. Usually, "Copy," I found, is the best thing is copy and then look in there. But let's do "Apply to editor" just so we can see how it works here.

"Apply to editor." It changes our code for us, and we have the option to "Accept the changes," "Discard them," or "Show the changes." Now, here when we do "Show changes," this is a basic common diff type thing that you're probably familiar with, where it says, "Red: we're going to remove these two lines. We're going to replace it with this." And you can hit "Accept changes." There we go. Now, this is going to produce basically the same thing. But actually, it's not going to replace the same thing because it fixed the error. Let's make it break again. And then it's going to produce basically the same thing, but it's going to be handled with an exception.

So, in this demo, we just kind of looked at some of the really basic stuff. And GitHub Copilot is an awesome tool for beginner or experienced developers. As an experienced developer, you might use it a little bit differently, and we're going to look at that in this course. But by leveraging these code capabilities, once you start getting good at it, get the hang of it, you'll really streamline your workflow, you'll be able to work faster, build code faster, things like that.

So, some of the good things about GitHub Copilot: it automates repetitive tasks. You know, we started to write out a method, and GitHub Copilot said, "Hey, I've seen this method a million times. Here's what it probably is." So, it automates the repetitive stuff, reduces your errors because it's suggesting syntactically correct most of the time and semantically meaningful code most of the time. Um, and it kind of boosts your creativity because you can explore different approaches. Sometimes it'll suggest something new, and uh, you can spend less time on the boring stuff and more time on the fun stuff. But enough of that. Let's jump in and build a real project with GitHub Copilot stuff, right? We just took that class, we moved it to its own file here, and now we're calling it from here. But it's nice that GitHub Copilot can do this for us.

So, I don't think I'm going to need this conversation again. So, the best way you can do it is you can reset it. And if you go to "Show Chats," here we go. Here's the chats that we've had today, which is kind of cool. Um, but easiest way is just hit the new button, reset it, and here we go. And you might notice that here in the main function, we have a docstring. And that could have been inserted by GitHub Copilot no matter what, or it could have been because of the rules that we set in here with our C-Pilot instructions. And it's the same here. We've got uh some documentation tells us what to do. Now, this is great.

Now, what I'm going to do is I'm going to create a separate file that has nothing to do with this. This file is going to uh go to an LLM and grab a bunch of fake data and write it to a file. And this is going to be part of our project setup. Now, I'm going to use Ollama. That's a local LLM. Not really that important how I'm doing this, but I do want to show off some of the cool capabilities of Copilot outside of your code. Let's see how much it knows about the outside world. I think I want to use this 54 latest model. So, this is something to keep in mind here. And what I'm going to do is say, we're going to create a new file called "create_fake_data.py," and we're just going to see how much GitHub Copilot knows about Ollama. Now, I know how to create uh an application that will communicate with Ollama. And I can go online and look at that. If I didn't know how to do it, I could go online. I could read the directions, follow the directions, do it. But what if this is just such a non-trivial thing that it just really doesn't matter? I don't care to spend a bunch of time on it. We're just getting fake data. And actually, there is a Python library out there that kind of generates fake data. There's other sources. Mockaroo.com is one of my favorites, but I just kind of want to include this and use this method to show how much GitHub Copilot may know about other things. For this, I'm going to try the Claude 3.5 Sonnet. And I'm going to ask Copilot, "I would like to connect to my Ollama. I'm going to be very specific with this and we'll see what kind of code it generates to my Ollama server. I want to use the FI latest 54 latest model. I would like to send a prompt to it asking for fake data and have it return in a CSV format."

So what is this going to do? Well, we're asking a lot out of GitHub Copilot right now. I'm going to press Enter. We're asking it to figure out how to connect to Ollama with Python. Figure out how to make a prompt. Send it the prompt. Get the stuff back. It's going to have to figure out what kind of prompt. And here it says, "Enable cloud preview for all clients." We can hit "Enable." And now it's going to start generating code. And now it has generated a bunch of stuff that we're going to blindly post in here and see if it works. So, but let's just just take a look at it here and see what it's suggesting. It's saying, "Import required libraries, request for API calls. Cool. Uh, set up connection parameters for Ollama server. Create a function to generate a prompt for fake data. Create a function to call Ollama API. Parse response and format as CSV and save the output to a file." Like I said, we're asking a lot, and I don't expect this to run on the first try. However, I am going to try it.

So, we're going to go here to "Apply to editor." This should apply it to fake_data.py because it's open in our editor and we have it highlighted. And there we go. And note that this is well-documented also. So, I'll go here and then just kind of do a quick skim of the code. Make sure it's not going to do anything wild and crazy. And it looks like we have a "create_ollama_prompt" method. It says, "Generate data for five people in CSV format with these columns." Now, these suggested columns, but I could also change this if I wanted to, right? I could say, uh, "email, address, age, city, occupation." Um, all of that's fine. "Please only return the raw CSV data without any additional text." So, GitHub Copilot new. Now, I know because I've done this before, and you've probably done this before, but I know that you need to specify only returning raw data without additional text because otherwise it'll say, "Sure, here's a CSV file of, you know, this fake data." So hopefully this will work for us. And we look at this, it says, "Sends a prompt to Ollama API. Returns a response." We're not streaming because it's just going back and forth. It's not a chat server. And this actually looks looks pretty good. And if we get a response, we're going to write it to fake_data.csv.

So let's go ahead and run it. And we can see here that we need to import requests, which is fine. "pip install requests." And we can upgrade pip. All right, now we should be able to run it. Now, this is one of two ways to do this. Actually, there's more than two ways, but two main ways. This way, we're just sending a request to the Ollama server and asking for a response. There is an Ollama Python library also, which I'm surprised that that didn't come up, but uh, most of the time you do this with an Ollama Python library, but it's showing us an older method where you just uh make a request to it. Um, which should be fine. Let's take a look at our output. fake_data.csv. Look at that. So, it is wrapped here in CSV, but that's not a a big deal. This is looks like a CSV file. And I'm going to run this a bunch of times. I'm going to try to really fill this up. And in the next part of our project, we're going to put this into a database. And then we're going to pull it out of the database every time we need to generate fake data. This is the first step. But I just kind of wanted to show how to set up a project, how to get it going, how to scaffold things out, stuff like this.

Now before we close this video, I did talk about how to install Python programs. This might be something you've done before, but we have a dependency in this program, right? We did a pip install of requests. So we do have a dependency in this pro program. Here's how we can handle that. We'll say "pip freeze." When you do that, it shows these are the libraries that this needs to run. And if I do "pip freeze" and just pipe that to "requirements.txt," and we have this available in requirements.txt. So when we uh put this back on GitHub, I'm going to put this in this venv file or virtual environment. I'm going to put that into .gitignore. So it's not going to ship to GitHub with all those Python libraries attached to it. In fact, it's going to be pretty empty. And you will have to run "pip install -r requirements.txt," which of course this one says it's already satisfied, but you will have to do that when you build your own Python environment because we don't want to ship our entire Python environment everywhere we go. If you just have one line you got to run at the console to reinstall, then that's great.

So while we're here, we're going to add that to .gitignore. And that's something that we can ask Copilot about actually. And so this right here actually um I didn't think it would run on the first try until I went and looked at the code. Actually ran on the first try. So we're good. And I can say, "How can I add the venv folder?" Or I could say, "We'll we'll change this a little bit. We'll pretend like we have no idea, but we've heard this wild thing about this, right? How can I keep Git from seeing my venv folder?" And this will suggest the .gitignore work. And what it's looking at here is notice it looked at the instructions. If you have a way to pause the video, it said looking at first before anything it searched Copilot instructions. So here it says, "Let me help you create a .gitignore file to exclude the virtual environment folder." And so here we have this will exclude our virtual environment or several renditions of that. It'll exclude a bunch of cache, uh, IDE settings that I have set up, .DS_Store, all of this stuff. So this thing looks great. Now I haven't committed anything in Git yet, so I don't have to do this, but it also gives you some extra steps.

So let's go ahead and uh "Apply to editor." So now it's going to create a new file called .gitignore. We're going to save it. This .gitignore should have everything we need, including environments, things like that. And we have a good .gitignore. But it is kind of cool that it says, "Add it to git.gitignore, commit it." And it says, "If the venv folder is already tracked by Git (which it's not), but if it was, you can remove it from tracking with this." So it's pretty cool. I mean, this is a big time saver, and right now we have just used Copilot to help us set up this project. We've got a running project. We've pulled in some data for it. Um, everything looks really good, and this is able to set it up and save us some time. So, this is kind of boring stuff, right? This is boilerplate setup stuff, and we used GitHub Copilot to help us out. And I wanted to show that not only can GitHub Copilot help set up our project in Python that it knows about, but it can go out and figure out things about Ollama and things like that, and it can kind of go out and figure out this .gitignore thing. And so it's not just your code that GitHub Copilot can help you with. It's all kinds of things.

So in our next video, we're going to start building this thing out.