GitHub

How to Create a Branch in GitHub: Step-by-Step

How to Create a Branch in GitHub: Step-by-Step

The branch dropdown on a repository page does most of the work. Type a name nobody has used yet, click Create branch, and you’re on it.

The Git command line, GitHub CLI, GitHub Desktop, VS Code and the Development sidebar of an issue all end up in the same place.

Per GitHub Docs, a new branch starts from the repository’s default branch unless you select another one first. Creating it on github.com also takes write access to the repository, and that second part catches people out more than the clicking does.

What do you need before creating a branch on GitHub?

YouTube player

Write access to the repository is the one hard requirement for creating a branch on github.com. The branch itself is a separate line of work that points to a commit.

Anything you do on a separate Git branch stays away from the default branch until you merge it. That’s what makes a branch a safe place to build features and fix bugs, or to try an idea that might go nowhere.

Write access also covers opening a pull request and deleting or restoring branches. With read access only, you fork the repository and branch inside your copy. GitHub Desktop is the odd one out, since it will create a branch locally on read access, though pushing that branch still needs write access.

Contributors without push rights work from a fork of the repository, which gives them write access to their own copy. The branch travels back to the original through a pull request.

GitHub’s flow guide puts branching first, before you edit anything, and recommends one branch per set of unrelated changes. Reviews get easier that way, and a change that stalls doesn’t hold up the rest.

How should you name a branch?

Characters and patterns Git rejects in branch names, good short descriptive examples from GitHub flow, and why feature and feature/login cannot coexist

Keep the name short and descriptive. increase-test-timeout and add-code-of-conduct are both examples from GitHub’s flow guide. Whatever you pick still has to pass every check defined by git-check-ref-format.

Git rejects spaces, tildes, carets and colons. Question marks, asterisks, opening square brackets and backslashes are out too, along with the sequences .. and @{. A leading or trailing slash fails, doubled slashes fail, and so does a trailing dot. A leading dash is also refused when the name is checked as a branch.

The branch dropdown on github.com itself only asks for a unique name. Git does the stricter checking, so a name that looks fine in the browser can still be rejected when you create it locally.

Teams that adopt the Git Flow model often borrow its prefixes. Vincent Driessen’s original post names only release-* and hotfix-*, and lets feature branches take any other name, so a feature/ prefix is a team habit and not part of the spec.

Driessen’s 2020 note goes further and suggests a much simpler workflow for teams doing continuous delivery. For most web projects, I’d say that means GitHub flow and plain descriptive names.

Naming styleUpsideDownside
Flat: add-login-testsSimple, no collisionsNo grouping in branch lists
Slash prefix: feature/loginGroups related branchesFails if a branch named feature already exists
Issue number first: 482-fix-redirectTraces back to a ticketThe number alone says nothing about the change

The slash-prefix collision is real Git behavior. A branch called just feature can’t coexist with feature/feature-1, since a file and a directory can’t share a name.

Prefixes do give rulesets something to match, because a Restrict creations rule limits branches whose names match a pattern to users with bypass permission. Whatever you choose, keep one style per repository. I’d stay flat unless the team is big enough that the branch list needs grouping.

How do you create a branch on github.com?

The branch dropdown on the repository page handles it. Type a unique name into the “Find or create a branch…” field and click Create branch.

Create a branch from the branch dropdown

YouTube player

This is the fastest route. It works from the file tree or from the top of the integrated file editor.

  1. Open the repository’s main page on github.com.
  2. Select the branch dropdown.
  3. If the new branch should start from something other than the default branch, click that branch first, then open the dropdown again.
  4. Type a unique name in the “Find or create a branch…” field.
  5. Click Create branch.

Everything happens in the browser, so there’s no need to clone the repository locally first.

Create a branch from the branches page

Use this path when you want to set the source explicitly instead of relying on whatever branch you happen to be viewing.

  1. From the main page, open the branch dropdown and click View all branches.
  2. Click New branch.
  3. Type a name for the new branch.
  4. Choose a source under “Source”.
  5. Click Create new branch.

These steps follow GitHub’s branch management docs. For forks, the source picker lets you choose your fork or the upstream repository before you pick a branch.

Create a branch from an issue

Issues can spawn their own branches.

From the issue page, click Create a branch under Development in the right sidebar. The dialog asks for an optional name and an optional destination repository, then lets you either work locally or open GitHub Desktop.

Anyone with write permission can do this, and one issue can link several branches. By default the branch lands in the current repository, cut from the default branch. Once you open a pull request from it, the sidebar swaps the branch link for the pull request.

GitHub Docs labels the feature a public preview and subject to change, so the dialog can look different from the steps above.

Which source should the new branch start from?

YouTube player

Start from the default branch unless the change depends on work that hasn’t merged yet. GitHub always creates a branch from an existing one, and the default branch is the base for pull requests and code commits.

BaseOn github.comGit commandPick it when
Default branchThe default choicegit switch -c name mainThe change is independent
Another branchSelect it first, or use Source on the branches pagegit switch -c name other-branchThe change builds on unmerged work
TagNot described in current docsgit branch hotfix v2.6.14Patching a shipped release
CommitNot described in current docsgit branch name a1b2c3dReproducing an earlier state

A Git tag pins a release, so branching from one suits a patch to code that already shipped.

Giving Git a commit hash as the start point pins the branch to an exact state, which helps when you need to reproduce a bug as it stood on a given day.

GitHub’s own sources don’t fully agree on tags and commits. The original announcement of web-based branch creation said the new branch follows your current context, including a specific tag or commit, while the current docs describe only the default branch and other branches. Test the dropdown on a tag view before relying on it.

The command line has no such ambiguity. git branch accepts a branch name, a commit id, or a tag as the start point.

Branching from a stale feature branch drags its unmerged commits into your pull request, which is an easy mistake to make and an annoying one to untangle.

How do you create a branch with Git and GitHub CLI?

git switch -c name creates the branch and moves you onto it, and git push -u origin name publishes it. Plain git branch creates the branch but leaves you where you were.

Create and push a branch with Git

YouTube player

Update the default branch first. A new branch starts at the current HEAD unless you give a start point, so any stale history comes with it.

  1. Clone the repository, or run git fetch if you already have it.
  2. Run git switch main (or your default branch name), then git pull.
  3. Run git switch -c add-login-tests.
  4. Commit your changes.
  5. Run git push -u origin add-login-tests.

The -c flag is the transactional equivalent of git branch followed by git switch, so the branch isn’t created if the switch fails. git checkout -b does the same job, and git switch has been around since Git 2.23.

Here origin is the default name Git gives the remote you cloned from. The -u flag records the pushed branch as the upstream, so later pushes and pulls need no arguments.

Until that first push, the branch exists only on your machine and won’t show up in the github.com dropdown. For the full set of local commands, see this guide to creating a new branch in Git.

The capital -C is a trap. It resets an existing branch to the start point instead of failing, so use it only when you mean to overwrite.

Create an issue-linked branch with gh

The GitHub CLI manual describes gh issue develop as the command that manages linked branches for an issue.

--name sets the branch name and --base picks the remote branch to cut from. Add --checkout to check the branch out right after creating it, or --branch-repo to create it in another repository, such as your fork.

The simplest call is gh issue develop 123 --checkout, which creates a branch for issue 123 and checks it out. With --base, the new branch also becomes the base for pull requests made with gh pr create.

If you haven’t used the tool before, there’s a separate guide to using GitHub CLI worth reading first.

How do you create a branch in GitHub Desktop and VS Code?

In GitHub Desktop you click Current Branch, then New Branch, give it a name, pick a base and click Create Branch. VS Code is quicker if you live in the Command Palette, where Git: Create Branch does the job.

Either way the branch stays local until you publish it.

Create a branch in GitHub Desktop

YouTube player
  1. Click Current Branch in the repository bar and select the branch to base the new one on.
  2. Click New Branch.
  3. Enter a name under Name in the “Create a Branch” window.
  4. Choose a base under “Create branch based on…”.
  5. Click Create Branch.

The first branch you create is based on the default branch. After that, you choose between the currently checked-out branch and the default branch.

To start from older history, open History in the left sidebar, right-click a commit and pick Create Branch from Commit. Then click Publish branch to make it available on GitHub.

If a ruleset requires a naming pattern, Desktop shows a warning and refuses to create a branch that breaks it. The mistake surfaces before the push, not after, which is a small thing but a nice one.

For the rest of the app, there’s a separate guide to using GitHub Desktop.

Create a branch in VS Code

To branch from HEAD, run Git: Create Branch… from the Command Palette and enter a name. To start from somewhere else, run Git: Create Branch From… and pick a branch such as main or origin/main, or a tag. The Status Bar works too. Click the branch name and choose “Create new branch…”.

The VS Code docs say both commands switch you to the new branch, and the Publish Branch action sends it to the remote.

The GitHub Pull Requests and Issues extension adds another route, creating a branch directly from a GitHub issue. VS Code can also generate random branch names through the git.branchRandomName.enable setting, which is worth leaving off when your team enforces a naming pattern.

Create a branch in a codespace

Inside a Codespace, the status bar does the work.

Click the branch name, type a name, and choose Create new branch. If the branch name isn’t visible, right-click the status bar and select Source control first.

Publishing depends on how the codespace started. For a branch already linked to a remote, use Commit & Push, or open the ellipsis menu in the Source Control side bar and click Push.

Which method should you use?

Six ways to create a GitHub branch, from the web dropdown and issue sidebar to gh issue develop, git switch -c, GitHub Desktop and VS Code, with the best use for each

The web dropdown suits small edits. For ticket-driven work, the issue sidebar or gh issue develop fits better, and when the code has to run on your machine, the command line wins.

MethodStrengthLimitBest for
github.com dropdownNo clone, fastestDefault or another branch as sourceSmall edits
Issue sidebar or ghBranch linked to the issueSidebar feature is a public previewTicket-driven work
Git CLIAny start pointLocal until pushedWork with local tests
GitHub DesktopBranch from a commit, ruleset warningsNeeds a local cloneVisual workflow
VS Code or CodespacesStays inside the editorSeparate publish stepEditor-centered work

If the change needs a local test run, create the branch locally. If it doesn’t, the browser wins on speed.

Cloning a repository just to fix a typo in a README is overkill. The dropdown does that job in a few clicks.

Why can’t you create a branch, and what goes wrong?

Branch creation usually fails because of missing write access, a name collision, a ruleset name requirement, a creation restriction, or a branch that was never pushed.

SymptomCauseFix
“Permission denied” on pushThe account in use has no write accessGet added as a collaborator, or fork
cannot lock ref ... existsA shorter branch name blocks the slash nameRename the old one: git branch -m feature feature/feature-1
Desktop refuses to create the branchA ruleset requires a name patternRename to match the pattern
New branch rejected on pushCreation is restricted for matching namesAsk an admin for bypass, or use an allowed name
Branch missing on github.comIt only exists locallyRun git push -u origin name

The permission error means the key or token belongs to an account without access to the repository. GitHub’s fix is that the owner adds your account as a collaborator, or to a team with write access.

When the owner won’t, forking the repository sidesteps the problem. You branch inside your copy instead.

The collision error is documented in Atlassian’s knowledge base. A branch called just feature cannot exist next to feature/feature-1.

Branch protection rules vs rulesets

Branch protection rules compared with rulesets on rules per branch, limiting branch creation and admin bypass, plus protection defaults and the plan limit

Branch protection rules disable force pushes and block deletion by default. Push restrictions can also cover creating branches that match the rule, for example letting only one team create branches containing “release”. Patterns use fnmatch syntax, such as *release*, and only one rule applies to a branch at a time.

Rulesets don’t have that one-rule limit. A Restrict creations rule limits matching branch names to users with bypass permission, and bypass can be handed to GitHub Apps as well as to roles and specific teams.

Protection rules also exempt admins by default, so a restriction can look broken to an admin while it works fine for everyone else. GitHub ships a guide for converting branch protection rules to rulesets, which is the sensible move for any repository that has outgrown the single-rule limit.

Protected branches also have a plan limit. They work in public repositories on GitHub Free, and in private repositories on paid plans.

What happens after you create a branch?

The five steps after creating a GitHub branch: commit and push, open the pull request, review, merge and delete, with a short-lived branch lasting a couple of days

The branch moves through commit, push, pull request, merge and deletion.

  1. Commit your changes and push the branch.
  2. Open the repository’s main page, pick the branch, and click Compare & pull request in the yellow banner.
  3. Confirm the base and compare branches, add a title, and click Create Pull Request (or create a draft).
  4. Merge after review.
  5. Delete the branch.

A pull request needs two different branches and commits on the compare branch, so a freshly created branch with no commits can’t be proposed. This walkthrough on creating a pull request covers the review side.

GitHub’s own pages agree on permissions. The branches page lists opening a pull request among the actions that need write access, and the pull request page says that opening one in a public repository needs write access to the head or source branch. The practical reading is that the head branch has to live somewhere you can write to, which is why forks exist.

The first push can also start CI. Without an on.push.branches filter, a push to any branch starts a push-triggered workflow, so name patterns matter here too.

Cleanup is the step teams skip. Repository admins can switch on Automatically delete head branches in General settings, and branch protection rules and repository rules can block that deletion.

Without the setting, delete the branch by hand, as shown in this guide to deleting a branch in GitHub. For fork pull requests, GitHub checks the setting on the fork, not the base repository.

Trunk Based Development guidance sets the life of a short-lived branch at a couple of days, with time spent waiting for code review counting toward it.

GitHub Branch FAQ

How do you switch to an existing branch?

Run git switch branch-name in a terminal, or pick the branch from the dropdown on github.com. Running git switch - jumps back to the previous branch.

The browser dropdown only changes which branch you’re viewing, not any files on your machine.

How do you list all branches in a repository?

On github.com, open the branch dropdown and click View all branches.

In a terminal, git branch lists local branches, and git branch -a adds remote-tracking ones. The -vv flag also shows each branch’s upstream.

Can you rename a branch after creating it?

Yes. On github.com, open View all branches, use the menu next to the branch and choose Rename branch, while git branch -m old-name new-name renames the local copy.

Write permission covers most branches, but the default branch, and any branch covered by branch protection or a ruleset, needs a repository admin. Renaming the head branch of an open pull request closes that pull request.

Does creating a branch copy the repository?

No. A branch is a lightweight pointer to a commit, and the Pro Git book likens creating one to writing 41 bytes to a file.

Nothing gets duplicated. The new branch just points at the commit it started from.

What is the difference between a branch and a fork?

Scope, mostly. A branch lives inside a single repository.

A fork is a separate repository that starts as a copy of another one, with its own settings, permissions and branches, though it stays connected to the upstream.

Why do some repositories use main and others master?

GitHub made main the default for new repositories on October 1, 2020, and left existing repositories on whatever default they already had.

Git itself still names the first branch master unless you set init.defaultBranch, a setting added in Git 2.28.

How to Keep a GitHub Branch Up to Date

Fetch the default branch, then merge it into the GitHub branch or rebase onto it before opening a pull request. Running git pull performs the fetch and the merge in one command, so commit local work first.

  • Run git fetch origin to see remote changes
  • Merge origin/main into the branch, or rebase onto it
  • Resolve conflicts, then push

Rebase gives a linear history without a merge commit, but it rewrites your commits. Pushing them back needs a force push, which a branch protection rule blocks by default.

The Update branch button syncs a pull request without a local clone, yet it only works when there are no merge conflicts, and it may be unavailable when the head branch is protected. A strict required status check makes the sync mandatory before merge.

Once the branch is current, merging branches in GitHub goes through cleanly.

Bogdan Sandu

Stay sharp. Ship better code.

Every week: one curated article, one tool worth knowing, one tip you can use tomorrow. No noise, no padding.