Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 36 ยท Repositories and Collaboration

Branches on GitHub

Work with branches on GitHub: the default branch, creating and renaming branches, naming conventions, comparing branches, tags, and cleaning up branches after merge.

A branch is a movable pointer to a commit. On your machine you create branches with Git; on GitHub they appear in the branch selector, in the Branches page, in compare views and as the source of every pull request. Understanding how GitHub treats branches, especially the default branch, makes teamwork smoother. If branching in Git itself is new, read the Git branching lessons first.

The default branch

Every repository has a default branch, normally main. It is what visitors see first, what new clones check out, and what pull requests target unless you choose otherwise. Repositories created on GitHub today use main; older ones may still use master.

To change it, go to Settings > Branches > Default branch. To rename the existing one, Settings > Branches, then the pencil icon next to the branch name. GitHub retargets open pull requests and shows a redirect, but every collaborator must update their clone:

Terminal
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

Creating and switching branches

Locally:

Terminal
git switch -c feature/login        # create and switch
git push -u origin feature/login   # publish it to GitHub

On the website you can open the branch selector, type a new name and choose Create branch from main. You can also create one from an issue (Development > Create a branch), which links the two and names the branch after the issue.

Naming conventions

Names are free-form, but a convention helps people scan a long list:

Pattern Example Meaning
feature/<topic> feature/dark-mode New functionality
fix/<topic> fix/crash-on-empty Bug fixes
docs/<topic> docs/install-guide Documentation
<user>/<topic> ada/refactor-parser Personal work branches
release/<version> release/2.4 Maintenance lines

Avoid spaces and odd characters. Add the issue number if your team uses one (fix/123-crash-on-empty). Because branch names become part of URLs and CI rules, lowercase with hyphens is safest.

JS Stand-in: branch name checker

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The Branches page and the branch selector

github.com/<owner>/<repo>/branches has tabs: Overview (default branch and the ones you recently touched), Yours, Active and Stale. Each row shows how far a branch is ahead and behind the default branch, its last update and any pull request. From here you can delete merged branches, open a pull request, or rename.

In the code view, the selector at top left switches between Branches and Tags. Any URL with /tree/<branch>/path or /blob/<branch>/file can be shared and will show that branch. For a permanent link to a specific version of a file, press y on a file page: it rewrites the URL to use the commit SHA instead of the branch name, so it never changes.

Comparing branches

Open github.com/<owner>/<repo>/compare/main...feature/login. The three-dot form shows commits and changes in feature/login since it diverged from main, which is what a pull request would contain. Other handy forms:

  • compare/v1.0...v1.1 compares two tags.
  • compare/<sha1>...<sha2> compares commits.
  • Append .diff or .patch to the URL for plain text.
  • From a fork, compare/main...other-user:feature compares across forks.
JS Stand-in: ahead and behind counts

Press Run (or Ctrl + Enter) to see the console output here.

No output.

That is the same computation the Branches page does for its ahead/behind badges.

Tags

A tag marks one specific commit permanently, usually a release such as v1.2.0. Create one locally and push it:

Terminal
git tag -a v1.2.0 -m "Release 1.2.0"
git push origin v1.2.0

Tags show under the Tags tab and become the base for Releases (see the Releases lesson later in this track).

Cleaning up

Merged branches clutter the list. Two settings help:

  • Settings > General > Automatically delete head branches removes a pull request's branch right after it merges.
  • Locally, prune references to deleted remote branches: git fetch --prune (or git config --global fetch.prune true).

You can delete a remote branch with git push origin --delete feature/login. Deleting a branch deletes only the pointer, and the commits remain reachable for a while (GitHub shows a Restore branch button on closed pull requests).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the default branch control?

    The default branch is the repository's front door and the default pull request base.
  2. 2. Which compare URL shows what a pull request from feature to main would contain?

    base...head with the three dots shows commits in head since the merge base.
  3. 3. You want a link to a file that will never change even if the branch moves. What do you do?

    The y shortcut produces a permanent link with the commit hash.
  4. 4. How do you remove local references to remote branches that were deleted on GitHub?

    fetch --prune removes stale remote-tracking branches.

Summary

  • The default branch (usually main) is the target of pull requests and what clones check out.
  • Use a clear naming convention; lowercase with hyphens avoids surprises.
  • The Branches page and compare view show ahead and behind counts and the changes between refs.
  • Press y for permanent links; tag releases; auto-delete merged branches and prune locally.

Next: Forks and Upstream