Learn GitHub
$ gh ghautomation.ghcodespaces

Cloud Development with Codespaces

4 learning objectives · 5 key terms · 4 commands · 1 diagrams · 5 quiz questions

$objectives

Cloud Development with Codespaces

Automation & Security

Module 4 of 5 · lesson 19 in this module

What you'll be able to do

  • Launch a ready-to-code environment from any branch or pull request
  • Define reproducible environments with devcontainer.json
  • Prebuild images so new environments start in seconds
  • Control costs by stopping idle codespaces and choosing sizes
$concept

The idea, in plain language

Codespaces are full VS Code development environments running in the cloud, launched from a branch, a pull request, or a template — dependencies installed, ports forwarded, extensions present. They matter because onboarding collapses from a day of setup to a clicked link, and every contributor works in an identical, disposable machine.

Reproducibility comes from devcontainer.json: base image, features (languages, tools), extensions, forwarded ports, and setup commands, all versioned beside the code. Prebuilds compile that definition ahead of time so new codespaces open in seconds, and per-user dotfiles plus secrets follow individuals without entering the repository. Costs accrue per minute of compute plus storage, so stopping (not just closing the tab) is the habit that keeps bills boring.

$key terms

Key terms

codespace
A cloud-hosted VS Code environment attached to a repository and branch.
devcontainer.json
The versioned definition of the environment: image, features, ports, setup.
prebuild
A precompiled environment snapshot that makes new codespaces start in seconds.
port forwarding
Exposing the codespace localhost to your browser for running apps.
dotfiles
Personal shell/editor configuration applied to your codespaces.
$steps

How it works, step by step

  1. 1
    Launch from context

    The Code dropdown, a pull request, or gh codespace create — always attached to a specific branch, so work starts reviewable.

    createbash
    gh codespace create --repo ada/app --branch feature-x
  2. 2
    Define the environment

    .devcontainer/devcontainer.json names the image, adds features (node, python, docker), forwards ports, and runs setup commands on create.

    devcontainerbash
    {
      "image": "mcr.microsoft.com/devcontainers/base:ubuntu",
      "features": { "ghcr.io/devcontainers/features/node:1": {} },
      "forwardPorts": [3000],
      "postCreateCommand": "npm ci"
    }
  3. 3
    Prebuild for speed

    Prebuild configurations compile the definition on push, so contributors open warm environments instead of waiting on installs.

    lifecyclebash
    gh codespace ports  # inspect forwarded ports
     gh codespace stop  # stop billing compute
  4. 4
    Stop, don’t just close

    Closing the tab leaves compute billing; stop or delete codespaces when done, and pick the smallest machine type that fits.

    costsbash
    gh codespace list
    gh codespace delete --all --force  # careful: unpushed work dies here
$commands

Commands worth memorising

Every block below is copy-ready — use the Copy button, then paste it into your terminal. Expected output is shown underneath so you know what “working” looks like.

create on a branchbash
gh codespace create --repo ada/app --branch feature-x
Output
Created codespace delightful-garbanzo (4 cores, 8GB)

Starts attached to the branch, so commits land where reviewers expect.

list your environmentsbash
gh codespace list
Output
NAME                  STATE    REPO
 delightful-garbanzo   Active   ada/app

Audit what is running (and billing) before the invoice does.

open in VS Codebash
gh codespace code --web
Output
(browser tab opens on the cloud environment)

Choose browser or desktop VS Code per preference; the machine is the same.

stop billingbash
gh codespace stop
Output
✓ Codespace stopped

Compute stops; storage persists until deletion.

$diagrams

Workflow diagrams

Diagram 1From branch to running environment

Definition is versioned; prebuilds make creation instant; stopping ends billing.

Reading the diagram
  1. Version the environment definition beside the code.
  2. Prebuild so creation takes seconds.
  3. Create per branch or pull request; stop when done.
$example

Worked example

Onboarding a contributor onto a Node API with zero local setup.

first day in one linkbash
# maintainer, once:
# .devcontainer/devcontainer.json with node feature + postCreateCommand: npm ci
# contributor, day one:
gh codespace create --repo ada/api --branch main
# browser opens: dependencies installed, port 3000 forwarded, tests green
Output
Contributor runs the test suite within minutes of accepting the invite — no local installs, no version drift.
1The versioned definition guarantees the same Node version, extensions, and ports for everyone.
2postCreateCommand installs dependencies during creation, so arrival means ready.
3Forwarded ports expose the running API to the browser for manual testing.
4Stopping at day end ends compute billing; the branch holds all work.
$use cases

Where you'll use it

▸Onboarding contributors without local setup marathons.
▸Reviewing pull requests in a runnable copy of the change.
▸Standardizing toolchains across heterogeneous laptops.
▸Spiking on unfamiliar stacks with disposable machines.
$mistakes

Common mistakes

  • Closing the tab instead of stopping — compute keeps billing.
  • Deleting codespaces with unpushed commits still inside.
  • Oversized machine types for text-editing workloads.
  • Secrets in devcontainer.json instead of personal secrets stores.
$best practices

Best practices

  • Version devcontainer.json with the code it serves.
  • Prebuild on main so creation takes seconds.
  • Stop or delete environments when done; audit with gh codespace list.
  • Keep personal config in dotfiles, project config in the repository.
  • Right-size machines; upgrade only measured workloads.
$exercise

Hands-on exercise

Define and use a codespace

Take a repository from local-only to cloud-ready.

  1. Add .devcontainer/devcontainer.json with the project runtime and a postCreateCommand installing dependencies.
  2. Create a codespace on a branch and confirm the app runs with forwarded ports.
  3. Push a change from inside the codespace and open it as a pull request.
  4. Configure (or simulate) a prebuild and note the creation-time difference.
  5. List, stop, and delete the codespace, confirming no billable compute remains.

💡 Hint: The VS Code Ports panel shows forwarded ports and their visibility (private vs public).

Done when: A branch-tested change developed entirely in a stopped-afterwards cloud environment.

$quiz

Checkpoint quiz

0 of 5 answered · 0 correct

  1. Multiple choiceQ1

    What does devcontainer.json define?

  2. Multiple choiceQ2

    Closing the browser tab on a codespace does what to billing?

  3. True or falseQ3

    True or false: prebuilds compile the environment ahead of time so new codespaces start in seconds.

  4. Multiple choiceQ4

    Where do personal secrets for codespaces belong?

  5. TroubleshootingQ5

    Ports forwarded from a codespace are unreachable. What should you check first?

$summary

Module summary

  • Codespaces are disposable cloud VS Code environments attached to branches.
  • devcontainer.json versions the environment beside the code.
  • Prebuilds make creation take seconds instead of install-minutes.
  • Only stopping (not closing tabs) ends compute billing.
  • Personal config lives in dotfiles; secrets never in the repository.

Next up: Environments handled; next is keeping giant files out of Git without breaking clones.

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Put the GitHub course to work: open one of the company-wise repositories and follow the workflow you just learned, branch to release.