How Do You Get Gemini to Write Code That Fits Your Repo?
Import the code rather than describing it, then ask for a plan that names real files before you ask for any code. In the Gemini app, that's Add files > More Uploads > Import Code. You'll want Google AI Pro or Ultra for anything larger than a small project, because they have the 1 million token window.
I've imported my repo. Stack: [framework, version, package manager]. Change I need: [the behavior, in one or two sentences]. Constraints: follow the pattern in [path to a similar feature]. Don't add dependencies. Don't touch [paths]. Don't write code yet. First list: 1. The files you read that matter, with paths. 2. Existing functions and types you'll reuse, with paths. 3. Each file you'd change, and why. 4. Anything you couldn't find, or are assuming. Then stop and wait for my OK. After that, give me one file at a time as complete code, plus the command to test it.
- 1Import Code
- 2Ask for a plan
- 3Approve one change
- 4Paste and test
Try it now
- 1In the chat box, click Add files, then More Uploads, then Import Code.
- 2Enter a GitHub repository or branch URL, or choose Upload folder. Then paste the prompt above and click Submit.
- 3Open the paths in Gemini's plan. Correct anything that's wrong in your reply before you give your OK.
- 4You'll know it worked when each path and function it names really exists, and the code it writes uses them.
Use it when
A change that has to follow patterns already in your code, such as a new endpoint, a new page like an existing one, or a change touching several files. Also use it when you're new to a repo, and don't yet know which files matter.
Skip it when
A self-contained function with no dependencies on your project. Just ask. Also skip this when you want the work done in your files with tests run for you. The help page describes importing code for analysis, not writing back to your repo, so use Antigravity for that.
Why it works
New code clashes with a project when the model has to guess names, types and conventions. Importing gives it the real ones, but only what fits. Google's limits page gives the app's context window as 32k tokens with no plan, 128k on AI Plus and 1 million on Pro and Ultra, and says 1 million is roughly 30,000 lines of code. By that figure, 32k is only about 960 lines (arithmetic, not a Google figure). The help page says going over can cause answers to miss details spread through large files. Asking for a plan first shows you what Gemini read and what it's assuming.
Example
Say you have a Next.js app with a PDF export on the invoices page, and you want a CSV export too. Import the repo, name app/invoices as the pattern to follow, say no new dependencies, and ask for the plan. If the plan reuses your existing invoice query and permission check, approve it. If it proposes a new query, you've caught the problem before any code exists.
Going further
Google's help lists a limit of one code folder or GitHub repository per chat, up to 5,000 files and 100 MB. It also says each file in an uploaded folder counts toward the 10 files per prompt limit, which doesn't sit well with the 5,000 figure. If a folder upload is refused, use the GitHub URL, or a branch URL so Gemini sees your feature branch and not the default one. A second repo needs a new chat.
Common mistakes
- Importing a whole repo on a free account. At 32k tokens, most of it can't be taken into account, and the answer may not tell you that.
- Uploading a folder with
node_modulesor build output. That uses up the 100 MB limit on files nobody needs to read. - Asking for an entire feature at once. You get more code than you can review, built on assumptions you never saw.
- Taking a confident answer as proof. Google warns that Gemini Apps can make mistakes, so run the code and check that the functions it names exist.
Sources
- Upload and analyze files, Gemini Apps Help
- Gemini Apps limits and feature availability
- Use Gemini Apps, Gemini Apps Help
Checked 6 November 2026. Products change often, so confirm details with the vendor.