10 Prompts for GitHub Copilot: From Commit Messages to Code Review
Why Prompting Copilot Matters
GitHub Copilot is a powerful AI pair programmer, but its output quality depends on how you ask. A well-crafted prompt can turn Copilot from a code autocomplete into a code reviewer, documentation writer, and test engineer. This collection is based on real usage from my daily workflow and official GitHub documentation. Each prompt includes a template and a concrete example you can copy and adapt.
Note: These prompts work with GitHub Copilot in VS Code, Visual Studio, and Copilot CLI. For the code review examples, you'll need to provide the code or a diff as context.
1. Generate a Commit Message from a Diff
When to use: You have staged changes and want a professional commit message.
Prompt template:
Generate a conventional commit message for the following diff.
Follow the format: <type>(<scope>): <subject>
Types: feat, fix, refactor, docs, test, chore.
Keep the subject under 50 characters. Add a body with key changes.
Diff:
{your diff}
Example:
$ git diff
- const x = calc(a, b);
+ const x = calc(a, b, true); // add flag to use cache
Copilot output:
perf(calc): add cache flag to calculation
- Add third parameter to enable caching
- Update all call sites
- Improve performance on repeated calls
Why it works: By giving a clear type list and format, you avoid generic messages like "Update file".
2. Explain This Code (for Code Review and Onboarding)
When to use: You encounter a complex function in a PR and need a plain-English explanation.
Prompt template:
Explain the following code like I'm a junior developer.
Describe what it does, why it exists, and identify any potential issues.
Code:
{code}
Example:
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
return merge(merge_sort(arr[:mid]), merge_sort(arr[mid:]))
Copilot output: "This is a recursive implementation of merge sort. It splits the array in half, sorts each half, then merges them. Potential issue: the slicing creates new lists, which is inefficient for large arrays."
Why it works: The "like a junior developer" instruction forces Copilot to simplify.
3. Find Bugs in This Function
When to use: Before opening a PR, you want a second pair of eyes.
Prompt template:
Act as a code reviewer. Find bugs, off-by-one errors, race conditions, and logic flaws.
For each issue, explain the problem and suggest a fix.
Code:
{code}
Example:
function findMax(arr) {
let max = 0;
for (let i = 1; i < arr.length; i++) {
if (arr[i] > max) max = arr[i];
}
return max;
}
Copilot output: "Bug: initializing max = 0 fails for arrays with negative numbers. Fix: initialize with arr[0]."
Why it works: Explicitly listing bug categories directs Copilot's attention.
4. Suggest Security Improvements
When to use: You want to harden code against common vulnerabilities like SQL injection or XSS.
Prompt template:
Perform a security review of this code. Look for OWASP Top 10 issues: injection, broken auth, XSS, insecure deserialization.
For each finding, propose a concrete fix.
Code:
{code}
Example:
def get_user(name):
conn = sqlite3.connect('db.sqlite')
cur = conn.execute(f"SELECT * FROM users WHERE name = '{name}'")
return cur.fetchall()
Copilot output: "SQL injection: user input is concatenated directly into the query. Use parameterized queries: conn.execute('SELECT * FROM users WHERE name = ?', (name,))."
Why it works: Referencing OWASP Top 10 grounds the review in well-known categories.
5. Write Unit Tests for This Function
When to use: You need test coverage for a new or existing function.
Prompt template:
Generate unit tests for the following function using {pytest / JUnit / Jest}.
Include edge cases: empty input, null values, large numbers, negative values.
Use descriptive test names.
Code:
{code}
Example:
def calculate_total(items):
return sum(i['price'] * i['count'] for i in items)
Copilot output:
def test_empty_items_returns_zero():
assert calculate_total([]) == 0
def test_single_item():
assert calculate_total([{'price': 2, 'count': 3}]) == 6
Why it works: Specifying the test framework and edge cases produces ready-to-run tests.
6. Refactor This Code for Readability
When to use: The code works, but it's hard to understand.
Prompt template:
Refactor this code to improve readability, naming, and structure.
Keep the same behavior. Explain each change.
Code:
{code}
Example:
const a = [1,2,3,4].map(x => x * 2).filter(x => x > 4);
Copilot output: "Extract the transformation into a named function: const doubleAndFilter = arr => arr.map(x => x * 2).filter(x => x > 4); Let's also rename a to result for clarity."
Why it works: "Explain each change" makes Copilot act like a mentor, not just a formatter.
7. Review a Pull Request for Best Practices
When to use: You want a high-level review of your PR before assigning a human reviewer.
Prompt template:
Review this pull request. Check for: code style, error handling, performance, missing documentation, and test coverage.
Give a summary and a checklist of action items.
PR diff:
{diff}
Example: Provide a diff with a new API endpoint. Copilot might output a checklist like "Missing input validation on POST body", "No try-catch around database query", "Endpoint not covered by tests."
Why it works: The checklist format gives you actionable tasks.
8. Generate Docstrings / JSDoc Comments
When to use: You wrote a function and need proper documentation.
Prompt template:
Write a docstring (JSDoc, DOCX, etc.) for this function. Include parameters, return value, and exceptions.
Use the Google style if possible.
Code:
{code}
Example:
def divide(a, b):
return a / b
Copilot output:
```python
def divide(a, b):
"""Divide two numbers.
Args:
a (float): Numerator.
b (float): Denominator.
Returns:
float: Result of division.
Raises:
ZeroDivisionError: If b is zero.
Comments