A branch in Git is a lightweight, movable pointer to a commit. Creating one gives you a separate line of work that starts from your current commit, so you can build a feature or fix a bug without affecting the main branch. Because a branch is just a pointer, creating one is fast and cheap.
There are two things to keep straight: creating a branch and switching to it. The command git branch makes a new branch but leaves you where you are. To actually work on the new branch, you switch to it with git switch or git checkout. There's also a shortcut, git switch -c or git checkout -b, that creates the branch and switches in one step.
This guide walks through each option: creating a branch, switching to it, doing both at once, starting a branch from a specific commit, and pushing the branch to a remote so your team can see it. It also covers the common mistakes, like committing to the wrong branch because you forgot to switch. All of this is local until you push, so you can experiment freely.
Before you start
What you will learn
- How to create a new branch in Git, switch onto it, create a branch from a specific point, and push it so your team can see it.
- Branches let you work on a change in isolation without touching the main line, which is the basis of almost every Git workflow, from features to fixes.
Requirements
- A local Git installation and a Git repository (created with git init or cloned from a remote).
- Git installed on your machine. git switch and git restore need Git 2.23 or newer; the git checkout equivalents work on older versions.
Good to know
- Creating and switching to a branch takes seconds; the whole tutorial is a few minutes.
- Commands use the standard Git CLI. git switch needs Git 2.23 or newer; if yours is older, the git checkout equivalents are shown alongside.
- This tutorial changes your local repository by adding a branch and, optionally, pushing it. It doesn't delete anything, and a branch can be removed the same way it's created.
Quick answer
To create a branch and start working on it in one step, run git switch -c followed by your branch name (or git checkout -b on older Git). To create a branch without switching, use git branch. When you're ready to share it, run git push -u origin followed by the branch name. Branches are local until you push.
git switch -c <branch-name>Step-by-step tutorial
7 stepsCheck where you are first
See your current branch and status before creating a new one.
Before branching, check which branch you're on and whether you have uncommitted changes. Run git status to see the current branch and any pending changes, and git branch to list your local branches. The current branch is marked with an asterisk.
Your new branch will start from wherever you are now, so it's worth confirming you're on the right base, usually main.
git status
git branchIf git status shows uncommitted changes, commit or stash them before switching branches later.
Create a new branch
Make a new branch pointer at your current commit.
Create a branch with git branch followed by a name, for example git branch feature-login. This makes a new branch that points to your current commit.
Important: git branch only creates the branch. It does not switch you onto it, so you're still on your original branch until you switch. Use short, descriptive names like feature-login or fix-typo.
git branch feature-loginNothing else changes yet. Your files and other branches stay exactly as they were.
Switch to the new branch
Move onto the branch so your work goes there.
Switch onto the branch with git switch followed by the name, for example git switch feature-login. On Git older than 2.23, use git checkout feature-login instead.
Once you switch, HEAD points to the new branch, and every commit you make from now on is recorded on it, not on main.
git switch feature-login
# On older Git:
# git checkout feature-loginThis is the step people forget. If you skip it, your commits land on the old branch.
Shortcut: create and switch in one step
Do both actions with a single command.
Most of the time you want to create a branch and start working on it right away. Do both at once with git switch -c followed by the name, for example git switch -c feature-login. On older Git, the equivalent is git checkout -b feature-login.
This is the same as running git branch and then git switch, just in one command, and it's the usual way people create branches.
git switch -c feature-login
# On older Git:
# git checkout -b feature-loginIf you use this shortcut, you don't need the separate create and switch steps above.
Optional: start a branch from a specific point
Base the new branch on a commit or branch other than your current one.
By default a branch starts from your current commit. To start it somewhere else, add a start point: a branch name, tag, or commit hash. For example, git switch -c hotfix main creates hotfix based on the tip of main, wherever you are now.
The same works with git branch: git branch hotfix main creates the branch from main without switching to it.
git switch -c hotfix main
# Create only, from a commit hash:
# git branch hotfix 1a2b3c4This is handy for basing a fix on the released main branch even while you're working elsewhere.
Push the branch to share it
Publish the local branch to the remote so others can see it.
A new branch lives only in your local repository until you push it. Publish it with git push -u origin followed by the branch name, for example git push -u origin feature-login. The -u flag sets the upstream, so later you can just run git push and git pull.
Make at least one commit on the branch first, since an empty branch has nothing to push beyond the pointer.
git push -u origin feature-loginorigin is the default name for your main remote. Substitute it if your remote is named differently.
Verify your branch
Confirm the branch exists and that you're on it.
Confirm everything with git branch -vv, which lists your local branches, marks the current one with an asterisk, and shows each branch's last commit and its tracked remote branch. Run git status to double check you're on the new branch.
Seeing your branch marked as current, with an upstream after you pushed, means the setup worked.
git branch -vv
git statusgit log --oneline --graph --all is a nice way to see how your branches relate.
Confirm your branch is set up correctly
After these steps you should have a new branch, be switched onto it, and, if you pushed, have it visible on the remote. The quickest confirmation is git status, which names your current branch, and git branch -vv, which lists your branches, marks the current one, and shows upstream tracking.
A common thing to double check is that your commits are landing where you expect. Make a small commit, then run git log --oneline. If the commit appears on your new branch and not on main, the branch is working as intended. If you pushed, the branch and its commits will also show up in your remote host, such as GitHub or Bitbucket.
- git status shows you're on the new branch, git branch lists it, new commits are recorded on it, and after pushing it appears on the remote with upstream tracking.
- You're still on the old branch, or your commits are going to main. That usually means you created the branch but didn't switch to it, so you're not actually working on it yet.
- git switch -c feature-login creates the branch and moves you onto it; git status then shows feature-login.
- git push -u origin feature-login puts the branch on the remote and sets it as the upstream.
Troubleshooting
You created a branch but your commits still go to the old branch
Cause: git branch creates the branch but doesn't switch to it, so you never moved onto it.
Run git switch <branch-name> to move onto your branch, then commit. Next time, use git switch -c <branch-name> to create and switch in one step. If you already committed to the wrong branch, you can move those commits with git switch and git cherry-pick, or reset the old branch carefully.
Git won't let you switch branches
Cause: You have uncommitted changes that would be overwritten by switching.
Commit your changes, or set them aside with git stash, then switch. After switching you can bring the changes back with git stash pop. This keeps your work safe while you move between branches.
fatal: a branch named X already exists
Cause: The branch name you chose is already in use.
Pick a different name, or switch to the existing branch with git switch X. If the old branch is no longer needed and is merged, delete it with git branch -d X first, then recreate it.
Your teammates can't see the new branch
Cause: The branch is still local; branches aren't shared until you push them.
Publish it with git push -u origin <branch-name>. The -u flag sets the upstream so future pushes and pulls are simpler. Make sure you've committed something on the branch first.
Frequently asked questions
What is a branch in Git?
A branch is a lightweight, movable pointer to a commit. Creating one gives you a separate line of work that starts from your current commit, so you can make changes without affecting other branches.
What's the difference between git branch and git switch?
git branch creates a new branch but leaves you on your current one. git switch moves you onto a branch so your commits go there. To create and switch at once, use git switch -c.
How do I create a branch and switch to it in one command?
Run git switch -c followed by the branch name, for example git switch -c feature-login. On Git older than 2.23, use git checkout -b feature-login instead. Both create the branch and move you onto it.
What's the difference between git switch and git checkout?
git switch is newer (Git 2.23 and later) and only handles moving between and creating branches, which makes it clearer. git checkout does that plus other jobs, which is why it's easier to misuse. Both can create and switch branches.
How do I create a branch from a specific commit or branch?
Add a start point after the name. For example, git switch -c hotfix main bases hotfix on main, and git branch hotfix 1a2b3c4 creates it from a specific commit without switching to it.
Does creating a branch change my files or other branches?
No. Creating a branch only adds a new pointer at your current commit. Your files and other branches stay the same until you switch to the new branch and start committing.





