Matua Doc

Matua Doc

Git branches in GitHub Codespaces

Learning intentions

In this lesson, you will learn how to create and manage Git branches in GitHub Codespaces.

  • Explain what a branch is and why developers use branches.
  • Identify the current branch in Codespaces.
  • Create a new branch from inside the Codespaces editor.
  • Switch between branches safely.
  • Explain what commit and stash mean.
  • Publish or sync a branch so other people can see your work.

What is a branch?

A branch is a separate line of development in a Git repository. It lets you work on a new feature, fix a bug, or try an idea without changing the main version of the project.

For example, instead of editing main directly, you might create a branch called feature/login-screen and do your work there. When the work is finished and tested, it can be merged back into main.

Branches are useful because they help you:

  • keep unfinished work away from the stable version
  • work on more than one task at a time
  • collaborate without everyone editing the same files on the same branch

Before you start

In GitHub Codespaces, some setup is already done for you:

  • the repository is already open in the editor
  • Git is already available inside the Codespace
  • your work is connected to a GitHub repository

That means you do not need to install Git yourself or open a local folder first in desktop VS Code.

You do still need:

  • a GitHub repository
  • a Codespace opened for that repository
  • permission to push branches to the repository, if your class workflow allows it

The instructions and screenshots in this lesson were adapted from the official VS Code documentation on Git branches and source control. The Codespaces editor uses the same source control tools and looks very similar.

Finding the current branch

Codespaces shows the current branch in the Status Bar at the bottom of the window. Selecting that branch name opens branch actions such as switching branches or creating a new one.

Where to look

  1. Open your repository in GitHub Codespaces.
  2. Look at the bottom-left area of the Status Bar.
  3. Find the Git branch icon and branch name.
  4. Select that branch name to open the branch menu.

This is the fastest way to check which branch you are currently using.

Current branch shown in the Status Bar and Source Control view

Creating a new branch

In Codespaces, the easiest way to create a branch is from the branch name in the Status Bar or with the Command Palette.

Method 1: Status Bar

  1. Select the current branch name in the Status Bar.
  2. Choose Create new branch from… if it appears, or select the branch action that creates a branch.
  3. Type a descriptive branch name. One will usually be suggested in the slides.
  4. Choose the branch to create it from, usually main.

Codespaces then checks out the new branch automatically, which means you are now working on it.

Create branch dialog in VS Code

Choosing a good branch name

Branch names should be short but clear. A good branch name tells other developers what the work is about.

Good examples:

  • constants-variables
  • revision-task
  • object-oriented-task-01

Avoid vague names such as:

  • stuff
  • new-branch
  • test2

Reviewing changed files

Before you switch branches, check whether you have changed any files.

  1. Open the Source Control view.
  2. Look under Changes.
  3. Files marked with M have been modified.
  4. Files marked with U are untracked and have not been added to Git yet.

If you can see files here, you still have work that has not been safely saved into Git history.

Changed files listed in Source Control

Note: autocommit has been enabled for your Codespaces, so this list may be empty or become empty whilst you are looking at it.

Switching between branches

Switching branches is also called checking out a branch.

To switch branches in Codespaces:

  1. Select the branch name in the Status Bar.
  2. Choose a different branch from the list.
  3. Wait for the editor to update the files in your working folder.

If you have uncommitted changes, Git may stop you from switching branches. That is a safety feature to prevent losing work.

Before switching branches, make sure you have done one of these:

  • committed your work
  • stashed your work
  • discarded changes you no longer need

If you ignore unfinished changes, you can end up with conflicts or blocked branch changes.

What does commit mean?

A commit is a saved checkpoint in your Git history. It records a set of changes along with a message explaining what changed.

In practice, committing means:

  • choosing which changed files should be included
  • writing a short message such as Add navbar links
  • saving that snapshot into the branch history

Staging and committing

  1. Open Source Control.
  2. Select the + button beside a file to stage it.
  3. Type a commit message at the top of Source Control.
  4. Select Commit.

Stage means “prepare this change to go into the next commit”.

Stage changes in Source Control

Commit button in Source Control

Only the files you have staged are included in the commit. If you forget to stage a file, that file stays outside the commit.

Note: autocommit has been enabled for your Codespaces, so this list may be empty or become empty whilst you are looking at it.

What does stash mean?

A stash is a temporary storage area for uncommitted changes. It lets you put your unfinished work aside so you can switch branches without losing it.

Use a stash when:

  • you are not ready to commit yet
  • you need to quickly change to another branch
  • your work is incomplete or messy and should not go into history yet

You can think of a stash as “hide these changes for now, and bring them back later”.

Commit or stash before switching

If you want to move to another branch, decide which option makes sense:

  • Commit if the work is ready to be saved in branch history.
  • Stash if the work is unfinished but you want to come back to it later.
  • Discard only if you are certain you do not need the changes.

For classwork, committing is usually better than stashing because it creates a clear record of your progress.

Publishing a branch

Creating a branch inside Codespaces does not automatically send it to GitHub.

To publish a branch:

  1. Create or switch to the branch in Codespaces.
  2. Make your changes.
  3. Commit your work.
  4. Use Publish Branch, Push, or Sync Changes when the editor offers it.

Publishing is important because it backs up your work and lets your kaiako view or review it.

Push, sync, and publish

These words are related, but not exactly the same:

  • Push means send your local commits up to GitHub.
  • Publish branch usually means create the branch on GitHub for the first time.
  • Sync changes usually means pull any new remote changes and push your local commits.

For students, the important idea is simple: after committing on your branch, you usually still need to send that branch to GitHub.

When to sync or publish

  1. Commit your changes on your branch.
  2. Look for Publish Branch, Push, or Sync Changes.
  3. Select it so GitHub receives your latest commits.

If you skip this step, your work may only exist inside the current Codespace session and on the local branch, not on GitHub where the teacher or teammates can view it.

Sync changes button in Source Control

A sensible branch workflow

One simple workflow is:

  1. Start on main.
  2. Create a new branch for one specific task.
  3. Do the work and check Source Control for changed files.
  4. Commit regularly, or stash if you must switch away from unfinished work.
  5. Publish or sync the branch to GitHub.
  6. Open a pull request or merge it back later.

This keeps each piece of work organized and easier to review.