Creating a branch means making a separate copy of your code where you can work without affecting the main version

A Git branch is a separate line of work inside your repository. When you create one, you get your own space to make changes, test them, and fix problems without touching the code that other people are using. The main branch (usually called main or master) stays stable while you work on your branch. Once your changes are ready, you merge the branch back into main.

The fastest way to create and switch to a new branch is a single command. If you already have a repository on your computer and you are inside its folder in your terminal, you can create a branch and start working in it right now.

Key Takeaways

  • The command git checkout -b branch-name creates a new branch and switches to it in one step.
  • Branch names should be short and describe what you are working on, like fix-login-bug or add-dark-mode.
  • You can see all your branches with git branch, and the one you are currently on will have an asterisk next to it.
  • Switching between branches uses git checkout branch-name (or git switch branch-name in newer Git versions).
  • Your changes stay on the branch you created them on until you merge that branch back to main.

The command to create and switch to a new branch

Open your terminal and navigate to your repository folder. Then type this command, replacing branch-name with a name that describes your work:

git checkout -b branch-name

This does two things at once: it creates a new branch and switches you to it. You are now working on that branch instead of main. Any changes you make from this point forward will be saved to this branch only.

If you want to create a branch but stay on your current branch (not switch to it yet), use git branch branch-name instead. Then switch to it later with git checkout branch-name.

Naming your branch so others understand what you are doing

Branch names should be short and tell anyone reading them what the work is about. Good names are fix-password-reset, add-search-feature, or update-readme. Avoid spaces — use hyphens instead. Many teams use a prefix like feature/ or bugfix/ to organize branches, so a name might look like feature/dark-mode or bugfix/login-timeout.

The name does not affect how Git works, but it makes life easier for you and anyone else reading your repository history. A branch called work-stuff tells nobody what you actually did. A branch called fix-cart-total-calculation tells the whole story.

Checking which branch you are on and seeing all your branches

At any time, you can see which branch you are currently working on and what other branches exist. Type this command:

git branch

The output will list all your local branches. The branch you are on right now will have an asterisk (*) next to it. If you have created three branches called fix-login, add-footer, and update-docs, and you are currently on fix-login, the output will look like this:

* fix-login add-footer update-docs main

This command shows only branches on your computer. If you want to see branches that exist on the remote repository (the version stored on GitHub or another server), add the -a flag: git branch -a.

Switching between branches you have already created

Once you have created multiple branches, you will need to switch between them. Use this command:

git checkout branch-name

Replace branch-name with the name of the branch you want to work on. If you want to switch from fix-login to add-footer, type git checkout add-footer. Your working folder will update to show the code from that branch.

Newer versions of Git also support git switch branch-name, which does the same thing. Both commands work — use whichever one your version of Git supports. If you are not sure, try git switch first, and if it does not work, use git checkout.

What happens to your changes when you create a branch

When you create a branch, Git takes a snapshot of the current code and gives you a separate space to work. Any changes you make on your new branch stay on that branch. If you switch back to main without merging, those changes disappear from your view — but they are not lost. They are still saved on the branch you created them on.

This is the whole point of branches: you can work on multiple things at once without them interfering with each other. You might have one branch fixing a bug, another adding a new feature, and main stays clean and ready to use. When your work on a branch is done and tested, you merge it back to main so everyone gets those changes.

Common mistakes when creating branches

The most common mistake is forgetting to create a branch before you start making changes. If you make changes directly on main and then realize you should have used a branch, you can still fix it — create the branch now, commit your changes to it, and then reset main. But it is easier to create the branch first.

Another mistake is creating a branch from the wrong starting point. If you create a branch while you are on an old branch instead of main, your new branch will include all the changes from that old branch. If that is not what you wanted, delete the new branch and create it again while on main: git checkout main, then git checkout -b new-branch-name.

Do not worry about creating too many branches. They take almost no space, and you can delete them later with git branch -d branch-name once you have merged them back to main.

Frequently Asked Questions

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

Yes. When you create a new branch, it starts from whatever branch you are currently on. If you are on feature/login and you create bugfix/login-timeout, the new branch will include all the changes from feature/login. This is useful when you want to build on work that is not finished yet, but be careful — you will need to merge both branches back to main eventually.

What if I create a branch but never use it?

Unused branches just sit in your repository taking up almost no space. You can delete them anytime with git branch -d branch-name. If the branch has changes that have not been merged, Git will warn you before deleting it, which is a safety feature to prevent losing work.

Do I need to push my branch to GitHub or another server right away?

No. Branches you create on your computer stay local until you push them. You can work on a branch for days without pushing it. When you are ready to share it or back it up, use git push origin branch-name. Until then, the branch exists only on your computer.

Can I rename a branch after I create it?

Yes, use git branch -m old-name new-name. If you are currently on the branch you want to rename, you can just use git branch -m new-name. If you have already pushed the branch to a server, you will need to push the renamed version and delete the old one on the server too.