Creating a branch on GitHub takes three steps: navigate to your repository, click the branch dropdown, and type a new name

A branch is a separate copy of your code where you can make changes without affecting the main version. GitHub stores branches inside your repository, and you can create one directly through the website without using the command line. The fastest way is through the branch dropdown on the repository's main page.

You can also create a branch from the command line if you have Git installed on your computer. Both methods accomplish the same thing — they give you an isolated workspace to experiment, add features, or fix bugs before merging your changes back into the main branch.

Key Takeaways

  • The branch dropdown on your repository's main page shows all existing branches and lets you create a new one by typing a name.
  • Branch names should be lowercase, use hyphens instead of spaces, and describe what work happens on that branch.
  • Creating a branch through GitHub's website copies the current code from whichever branch you have selected when you start.
  • If you use Git on your computer, the command git checkout -b branch-name creates and switches to a new branch in one step.
  • After you create a branch and push changes to it, GitHub will prompt you to open a pull request so others can review your work.

Creating a branch through the GitHub website

Open your repository on GitHub and look for the branch dropdown near the top left of the file list. It usually shows "main" or "master" as the current branch. Click the dropdown arrow next to the branch name to see all branches in your repository.

Type a new branch name in the search box that appears. GitHub will show "Create branch: your-name from 'main'" (or whichever branch you currently have selected). Click that option to create the branch. The new branch copies all the code from the branch you selected, so you start with an exact duplicate of that version.

After creation, GitHub automatically switches you to the new branch. You can now edit files, create new files, or delete files without changing anything on the main branch. Every change you make stays on your new branch until you push it to GitHub and open a pull request.

Naming your branch so others understand its purpose

Branch names should describe the work happening on that branch. Use lowercase letters, numbers, and hyphens. Avoid spaces, uppercase letters, and special characters like @ or #, because they can cause problems with Git commands.

Good branch names follow patterns like fix-login-bug, add-dark-mode, update-readme, or refactor-database-queries. The pattern tells someone reading your repository what kind of work is on each branch. If you are part of a team, ask whether they use a naming convention — some teams prefix branches with their initials or a ticket number, like jr-fix-login-bug or ticket-1234-dark-mode.

Creating a branch from the command line with Git

If you have Git installed on your computer and have cloned your repository locally, you can create a branch without opening GitHub. Open your terminal or command prompt, navigate to your repository folder, and run git checkout -b branch-name. Replace branch-name with your actual branch name.

This command creates the branch and switches to it in one step. You can then edit files, add them with git add, and commit them with git commit -m "message". When you are ready to share your work, push the branch to GitHub with git push origin branch-name.

If you want to create a branch from a branch other than main, first switch to that branch with git checkout branch-name, then create your new branch from it. The new branch will copy all the code from whatever branch you were on when you ran the create command.

Pushing your branch to GitHub so others can see it

After you create a branch on your computer and make changes, you need to push it to GitHub so it appears in your repository online. Run git push origin branch-name in your terminal. The first time you push a new branch, Git may ask you to set the upstream branch — follow the instruction it provides, or run git push -u origin branch-name to set it automatically.

Once your branch is on GitHub, it appears in the branch dropdown and in the "Branches" tab of your repository. Other people can see it, and you can open a pull request to ask for feedback on your changes. Until you push, your branch only exists on your computer.

Switching between branches without losing your work

If you create a branch but realize you need to work on a different one, you can switch branches with git checkout branch-name on the command line, or by clicking the branch dropdown on GitHub and selecting a different branch. However, if you have made changes that you have not committed, Git will warn you and may prevent the switch to protect your work.

Before switching, commit your changes with git add and git commit, or save them temporarily with git stash. The stash command stores your changes safely so you can switch branches and come back to them later with git stash pop. This prevents you from accidentally losing work or mixing changes from different branches.

Deleting a branch after your work is merged

Once your pull request is reviewed and merged into the main branch, you can delete the branch to keep your repository clean. On GitHub, open the pull request and look for a "Delete branch" button after the merge is complete. Click it to remove the branch from your repository.

If you created the branch on your computer, delete it locally with git branch -d branch-name. If the branch has not been merged yet, Git will refuse to delete it — use git branch -D branch-name to force deletion. You can also delete branches through the "Branches" tab on GitHub by clicking the trash icon next to the branch name.

Frequently Asked Questions

Can I create a branch from a branch that is not main?

Yes. Switch to the branch you want to copy from, then create your new branch. On GitHub, make sure the branch dropdown shows the branch you want to copy from before you create the new one. On the command line, run git checkout source-branch first, then git checkout -b new-branch. The new branch will contain all the code from the source branch at that moment.

What happens to my changes if I switch branches without committing?

Git will warn you and usually prevent the switch if you have uncommitted changes. Commit your work with git commit before switching, or use git stash to save changes temporarily. If you switch anyway and lose work, you may be able to recover it with git reflog, but committing regularly is the safest approach.

Do I have to use the command line to create a branch?

No. You can create branches entirely through GitHub's website using the branch dropdown. The command line is faster if you are already working on your computer, but both methods create the same branch. Choose whichever fits your workflow.

Why does my new branch show old code I thought I deleted?

When you create a branch, it copies the code from the branch you selected at that moment. If you created the branch from main, it will have whatever code is currently on main, including code that may have been deleted from other branches. This is normal — each branch is an independent snapshot.