How to create a new branch in Git

A branch in Git is a separate line of work that lets you make changes without affecting the main codebase. To create a new branch, open your terminal or command prompt in your project folder and run git branch branch-name, replacing "branch-name" with whatever you want to call it. This creates the branch but does not switch you to it — you are still on your current branch.

To both create and switch to the new branch in one command, use git checkout -b branch-name or git switch -c branch-name. The second option is newer and clearer about what it does. After running either command, your terminal prompt or Git interface will show you are now on the new branch, and any commits you make will be isolated from other branches until you merge them back.

Key Takeaways

  • git branch branch-name creates a new branch but leaves you on your current branch.
  • git switch -c branch-name creates a new branch and switches to it immediately, which is the most common workflow.
  • Branch names should be lowercase, use hyphens instead of spaces, and describe the work you are doing — for example, fix-login-bug or add-dark-mode.
  • You can see all branches on your computer with git branch and all branches including remote ones with git branch -a.
  • Creating a branch does not affect any existing code; it only creates a new starting point for your changes.

Naming branches so others understand what you are doing

Branch names matter because other people (or you, months later) will read them to understand what work is happening. Use lowercase letters and hyphens, not spaces or underscores. A name like fix-login-bug or update-readme tells someone exactly what the branch contains. A name like work or test tells them nothing.

Some teams use a prefix to organize branches by type. For example, feature/dark-mode, bugfix/password-reset, or docs/api-guide. This makes it easier to scan a list of branches and find what you need. If your team has a naming convention, follow it — consistency matters more than the specific format.

Creating a branch from a specific commit or another branch

By default, git switch -c branch-name creates a new branch starting from wherever you are right now. If you want to create a branch from a different point in history, specify that point after the branch name. For example, git switch -c bugfix-old-issue abc1234 creates a new branch starting from the commit with ID abc1234.

You can also create a branch from another branch without switching to it first. Run git branch new-branch existing-branch to create new-branch based on existing-branch. This is useful when you want to branch off someone else's work or split a feature into smaller pieces. After creating it, switch to it with git switch new-branch.

Viewing all branches and understanding which one you are on

To see all branches that exist on your computer, run git branch. The output shows a list with an asterisk (*) next to the branch you are currently on. If you want to see remote branches too — branches that exist on a server like GitHub — run git branch -a. Remote branches appear with a prefix like origin/main or origin/feature-name.

Your Git interface or editor may also show the current branch in the status bar or sidebar. In VS Code, for example, the branch name appears in the bottom left corner. Knowing which branch you are on prevents the common mistake of committing work to the wrong branch.

Switching between branches you have already created

Once you have created a branch, switch to it with git switch branch-name or the older git checkout branch-name. Both do the same thing; git switch is just clearer. When you switch branches, Git updates your files to match the state of that branch. If you have uncommitted changes, Git will warn you and refuse to switch until you commit or stash them.

If you try to switch to a branch that does not exist, Git tells you so. This is a safety feature — it prevents you from accidentally creating a branch when you meant to switch to an existing one. If you want to create and switch in one step, use git switch -c as described earlier.

Pushing a new branch to a remote repository

Creating a branch on your computer does not automatically send it to GitHub, GitLab, or another remote server. To push a new branch, run git push origin branch-name, replacing "origin" with the name of your remote if it is different. The first time you push a new branch, Git may ask you to set an upstream branch — follow the suggestion it prints, or run git push -u origin branch-name to set it up in one command.

After pushing, the branch exists both on your computer and on the remote server. Other people can now see it and switch to it. If you delete the branch on your computer later, the remote branch stays until someone deletes it there too. To delete a remote branch, run git push origin --delete branch-name.

Deleting branches you no longer need

Once you have merged a branch back into main or another branch, you can delete it to keep your branch list clean. To delete a branch on your computer, run git branch -d branch-name. Git will refuse to delete a branch that has not been merged, as a safety measure. If you are sure you want to delete it anyway, use git branch -D branch-name (capital D) to force the deletion.

Deleting a local branch does not delete the remote version. If you pushed the branch to GitHub or another server, delete it there separately with git push origin --delete branch-name. Some teams delete remote branches automatically after a pull request is merged, so you may not need to do this manually.

Frequently Asked Questions

What is the difference between git branch and git switch -c?

git branch branch-name creates a branch but leaves you on your current branch. git switch -c branch-name creates a branch and immediately switches to it. Most of the time you want to switch, so git switch -c is the faster choice. The older command git checkout -b does the same thing as git switch -c.

Can I create a branch with spaces in the name?

Technically yes, but do not. Spaces in branch names cause problems in scripts and on the command line. Use hyphens instead: fix-login-bug instead of fix login bug. This is a convention across almost all teams and makes your branch names easier to type and share.

What happens to my changes if I create a new branch?

Creating a branch does not affect your files at all. If you have uncommitted changes when you create a branch, they stay in your working directory. If you want those changes to be part of the new branch, commit them after switching to it. If you want them on a different branch, stash them first with git stash, switch branches, and then unstash with git stash pop.

How do I know if a branch has been merged?

Run git branch --merged to see branches that have been merged into your current branch. Run git branch --no-merged to see branches that have not been merged. This helps you decide which branches are safe to delete and which ones still contain unmerged work.