About source control in Codespaces
You can perform all the Git actions you need directly within your codespace. For example, you can fetch changes from the remote repository, switch branches, create a new branch, commit and push changes, and create a pull request. You can use the integrated terminal within your codespace to enter Git commands, or you can click icons and menu options to complete all the most common Git tasks. This guide explains how to use the graphical user interface for source control.
Source control in GitHub Codespaces uses the same workflow as Visual Studio Code. For more information, see the VS Code documentation "Using Version Control in VS Code."
A typical workflow for updating a file using GitHub Codespaces would be:
- From the default branch of your repository on GitHub, create a codespace. See "Creating a codespace."
- In your codespace, create a new branch to work on.
- Make your changes and save them.
- Commit the change.
- Raise a pull request.
Creating or switching branches
- If the current branch is not shown in the status bar, at the bottom of your codespace, right-click the status bar and select Source control.
- Click the branch name in the status bar.
- In the drop-down, either click the branch you want to switch to, or enter the name for a new branch and click Create new branch.
Tip: If someone has changed a file on the remote repository, in the branch you switched to, you will not see those changes until you pull the changes into your codespace.
Pulling changes from the remote repository
You can pull changes from the remote repository into your codespace at any time.
- In the Activity Bar, click the Source Control view.
- At the top of the side bar, click the ellipsis (...).
- In the drop-down menu, click Pull.
If the dev container configuration has been changed since you created the codespace, you can apply the changes by rebuilding the container for the codespace. For more information, see "Introduction to dev containers."
Setting your codespace to automatically fetch new changes
You can set your codespace to automatically fetch details of any new commits that have been made to the remote repository. This allows you to see whether your local copy of the repository is out of date, in which case you may choose to pull in the new changes.
If the fetch operation detects new changes on the remote repository, you'll see the number of new commits in the status bar. You can then pull the changes into your local copy.
- Click the Manage button at the bottom of the Activity Bar.
- In the menu, slick Settings.
- On the Settings page, search for:
autofetch
. - To fetch details of updates for all remotes registered for the current repository, set Git: Autofetch to
all
. - If you want to change the number of seconds between each automatic fetch, edit the value of Git: Autofetch Period.
Committing your changes
- In the Activity Bar, click the Source Control view.
- To stage your changes, click + next to the file you've changed, or next to Changes if you've changed multiple files and you want to stage them all.
- Type a commit message describing the change you've made.
- To commit your staged changes, click the check mark at the top the source control side bar.
Raising a pull request
- After you've committed changes to your local copy of the repository, click the Create Pull Request icon.
- Check that the local branch and repository you're merging from, and the remote branch and repository you're merging into, are correct. Then give the pull request a title and a description.
- Click Create.
Pushing changes to your remote repository
You can push the changes you've made. This applies those changes to the upstream branch on the remote repository. You might want to do this if you're not yet ready to create a pull request, or if you prefer to create a pull request on GitHub.
- At the top of the side bar, click the ellipsis (...).
- In the drop-down menu, click Push.