Skip to main content

Deep dive into GitHub Codespaces

Explore more detail about how GitHub Codespaces work.

GitHub Codespaces is an instant, cloud-based development environment that uses a container to provide you with common languages, tools, and utilities for development. GitHub Codespaces is also configurable, allowing you to create a customized development environment for your project. By configuring a custom development environment for your project, you can have a repeatable codespace configuration for all users of your project.

Creating your codespace

There are a number of entry points to create a codespace.

  • From a GitHub template or any template repository on GitHub to start a new project
  • From a branch in your repository for new feature work
  • From an open pull request to explore work-in-progress
  • From a commit in a repository's history to investigate a bug at a specific point in time

Puede crear un codespace en GitHub, en Visual Studio Code o utilizando el GitHub CLI.

Your codespace can be ephemeral if you need to test something or you can return to the same codespace to work on long-running feature work.

For more information, see Creación de un codespace para un repositorio, Creación de un codespace a partir de una plantilla, and Apertura de un codespace existente.

Note

You can create more than one codespace per repository or even per branch. However, there are limits to the number of codespaces you can create, and the number of codespaces you can run at the same time. If you reach the maximum number of codespaces and try to create another, a message is displayed telling you that you must remove an existing codespace before you can create a new one.

The codespace creation process

When you create a codespace, various steps happen in the background before the codespace is available to you.

Step 1: VM and storage are assigned to your codespace

When you create a codespace, a virtual machine (VM) is created using either the stable or versión preliminar pública release of the VM host image. For more information, see Elección de la imagen de host estable o beta. The host image defines the version of Linux that is used for the VM. The VM is both dedicated and private to you. Having a dedicated VM ensures that you have the entire set of compute resources from that machine available to you. If necessary, this also allows you to have full root access to your container.

A shallow clone is then made of your repository, or of the template repository if you're creating a codespace from a template. This is cloned into the /workspaces directory of the VM and subsequently mounted into the dev container. For more information, see About the directory structure of a codespace below.

Step 2: Dev container is created

GitHub Codespaces uses a Docker container as the development environment. This container is created based on configurations that you can define in a devcontainer.json file and, optionally, a Dockerfile. If you create a codespace from GitHub's blank template, or from a repository with no devcontainer.json file, GitHub Codespaces uses a default image, which has many languages and runtimes available. For more information, see Introducción a los contenedores dev. For details of what the default image for dev containers includes, see the devcontainers/images repository.

Note

If you want to use Git hooks in your codespace and apply anything in the git template directory to your codespace, then you must set up hooks during step 4 after the container is created.

Since your repository is cloned onto the host VM before the container is created, anything in the git template directory will not apply in your codespace unless you set up hooks in your devcontainer.json configuration file using the postCreateCommand in step 4. For more information, see Step 4: Post-creation setup.

Step 3: Connecting to the codespace

When your container has been created and any other initialization has run, you'll be connected to your codespace. You can connect to it by using:

Step 4: Post-creation setup

Once you are connected to your codespace, your automated setup may continue to build based on the configuration specified in your devcontainer.json file. You may see postCreateCommand and postAttachCommand run.

If you want to use Git hooks in your codespace, set up hooks using the devcontainer.json lifecycle scripts, such as postCreateCommand. For information about the lifecycle scripts, see the dev containers specification on the Development Containers website.

If you have a public dotfiles repository for GitHub Codespaces, you can enable it for use with new codespaces. When enabled, your dotfiles will be cloned to the container and the install script will be invoked. For more information, see Personalización de GitHub Codespaces para la cuenta.

Finally, if you created the codespace from a repository, the entire history of the repository is copied down with a full clone. If you created the codespace from a template, the full history of the template repository is not preserved; instead, unless you are using the blank template, you will start with an initial commit for the contents of the template repository.

During post-creation setup you'll still be able to use the integrated terminal and make edits to your files, but take care to avoid any race conditions between your work and the commands that are running.

Codespaces lifecycle

Saving files in your codespace

Save changes to files in the normal way, depending on the editor you are using.

If you work on codespaces in Visual Studio Code, you can enable Auto Save to ensure that your changes are always saved.

Closing or stopping your codespace

Your codespace will keep running while you are using it, but will time out after a period of inactivity. File changes from the editor and terminal output are counted as activity, so your codespace will not time out if terminal output is continuing. The default inactivity timeout period is 30 minutes. You can define your personal timeout setting for codespaces you create, but this may be overruled by an organization timeout policy. For more information, see Configuración del periodo de tiempo de espera para GitHub Codespaces.

If a codespace times out it will stop running, but you can restart it from the browser tab (if you were using the codespace in the browser), from within VS Code, or from your list of codespaces at https://github.com/codespaces.

To stop your codespace you can

If you exit your codespace without running the stop command (for example, by closing the browser tab), or if you leave the codespace running without interaction, the codespace and its running processes will continue for the duration of the inactivity timeout period.

When you close or stop your codespace, all uncommitted changes are preserved until you connect to the codespace again.

Running your application

Port forwarding gives you access to TCP ports running within your codespace. For example, if you're running a web application on port 4000 within your codespace, you can automatically forward that port to make the application accessible from your browser.

Port forwarding determines which ports are made accessible to you from the remote machine. Even if you do not forward a port, that port is still accessible to other processes running inside the codespace itself.

Diagram showing connections, over the internet, between a code editor or a browser on your device and a codespace on the cloud.

When an application running inside GitHub Codespaces outputs a port to the console, GitHub Codespaces detects the localhost URL pattern and automatically forwards the port. You can click on the URL in the terminal, or the link in the "toast" notification message that pops up at the bottom right corner of VS Code, to open the port in a browser. By default, GitHub Codespaces forwards the port using HTTP. For more information on port forwarding, see Reenviar puertos en tu codespace.

While ports can be forwarded automatically, they are not publicly accessible to the internet. By default, all ports are private, but you can manually make a port available to your organization or public, and then share access through a URL. For more information, see Reenviar puertos en tu codespace.

Running your application when you first land in your codespace can make for a fast inner dev loop. As you edit, your changes are automatically saved and available on your forwarded port. To view changes, go back to the running application tab in your browser and refresh it.

Committing and pushing your changes

Git is installed by default in your codespace and so you can rely on your existing Git workflow. You can work with Git in your codespace either via the Terminal or by using the source control features of VS Code.

If you're working with an existing repository, you can create a codespace from any branch, commit, or pull request in the repository, or you can switch to a new or existing branch from within your active codespace. Because GitHub Codespaces is designed to be ephemeral, you can use it as an isolated environment to experiment, check a teammate's pull request, or fix merge conflicts.

If you only have read access to a repository, then you can create a codespace for the repository as long as you can fork it. When you make a commit from the codespace, or push a new branch, GitHub Codespaces either automatically creates a fork of the repository for you, or links the codespace to an existing fork if you already have one for the upstream repository.

If you're working in a codespace created from a template, Git is installed by default, but you will need to publish your codespace to a remote repository to persist your work and to share it with others. If you start from GitHub's blank template, you first need to initialize your workspace as a Git repository (for example by entering git init) to start using source control within the codespace.

For more information, see Utilizar el control de código fuente en tu codespace.

Note

Commits from your codespace will be attributed to the name and public email configured at https://github.com/settings/profile. A token scoped to the repository, included in the environment as GITHUB_TOKEN, and your GitHub credentials will be used to authenticate.

Personalizing your codespace with extensions

You can add extensions within a codespace to personalize your experience in VS Code.

VS Code extensions

If you work on your codespaces in the VS Code desktop application, or the web client, you can add any extensions you need from the Visual Studio Code Marketplace. For information on how extensions run in GitHub Codespaces, see Supporting Remote Development and GitHub Codespaces in the VS Code documentation.

If you already use VS Code, you can use Settings Sync to automatically sync extensions, settings, themes, and keyboard shortcuts between your local instance and any codespaces you create.

About the directory structure of a codespace

Al crear un codespace, el repositorio se clona en el directorio /workspaces del codespace. Es un directorio persistente que se monta en el contenedor. Todo cambio que se realice dentro de este directorio (incluida la edición, la adición o la eliminación de archivos) se conserva al detener e iniciar el codespace, así como al recompilar el contenedor en el codespace.

Fuera del directorio /workspaces, el codespace contiene una estructura de directorios de Linux que varía en función de la imagen de contenedor dev que se utiliza para compilar el codespace. Puede agregar archivos o realizar cambios en los archivos fuera del directorio /workspaces. Por ejemplo, puede instalar nuevos programas o puede establecer la configuración del shell en un archivo como ~/.bashrc. Como usuario no raíz, puede que no tengas acceso de escritura automáticamente a ciertos directorios, pero la mayoría de las imágenes permiten el acceso raíz a estos directorios con el comando sudo.

Fuera de /workspaces, a excepción del directorio /tmp, los directorios de un codespace están vinculados al ciclo de vida del contenedor. Esto significa que los cambios que se realicen se conservan al detener e iniciar el codespace, pero no al recompilar el contenedor. For more information on the /tmp directory, see Conservación de variables de entorno y archivos temporales.

Clearing the directories outside /workspaces helps to ensure the rebuilt container is in the same state as it would be in a newly created codespace. If you're rebuilding a container to apply configuration changes to the codespace you're working in, you can be confident that any configuration changes you have made will work the same for users creating new codespaces with the same configuration. For more information, see Introducción a los contenedores dev.

If you want to make changes to your codespace that will be more robust over rebuilds and across different codespaces, you have several options.

  • To install programs and tools in all codespaces created from a repository, in your dev container configuration, you can use lifecycle command properties such as postCreateCommand to run custom installation commands, or you can choose from pre-written installation commands called "features." For more information, see the dev containers specification on the Development Containers website and Adición de características a un archivo devcontainer.json.
  • To install tools or customize your setup in every codespace you create, such as configuring your bash profile, you can link GitHub Codespaces with a dotfiles repository. The dotfiles repository is also cloned into the persistent /workspaces directory. For more information, see Personalización de GitHub Codespaces para la cuenta.
  • If you want to preserve specific files over a rebuild, you can use a devcontainer.json file to create a symlink between the files and a persistent directory within /workspaces. For more information, see Recompilación del contenedor en un codespace.

Further reading