Creating a new branch means making a separate copy of your project where you can work without changing the main version

A branch in Git is a parallel version of your project files. When you create a new branch, Git copies your current work into an isolated space where you can make changes, test ideas, or fix problems without touching the main project. Other people can keep working on the main branch while you work on yours. When your changes are ready, you merge the branch back into the main version.

The most common command to create a branch is straightforward: you type git branch branch-name in your terminal, where branch-name is whatever you want to call it. Git creates the branch but keeps you on your current branch. To actually start working in the new branch, you then type git checkout branch-name. Many people combine these two steps into one command: git checkout -b branch-name, which creates the branch and switches to it at the same time.

Key Takeaways

  • Use git checkout -b branch-name to create a new branch and switch to it in one command.
  • 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 by typing git branch, and the one you are currently on will have an asterisk next to it.
  • Switching between branches is safe — Git saves your work on each branch separately, so changes on one branch do not affect another.

Naming your branch so others understand what it contains

A good branch name tells anyone reading your project what work is happening on that branch. Names like feature/user-authentication, bugfix/cart-total-error, or docs/readme-update are clear. Names like test, work, or stuff create confusion later when you have ten branches and cannot remember which one fixes what.

Many teams use a prefix to organize branches by type. The prefix comes before a forward slash, then the description. feature/ is for new functionality, bugfix/ is for fixing problems, docs/ is for documentation changes, and refactor/ is for reorganizing code without changing what it does. Use hyphens instead of spaces — Git branch names do not accept spaces, so fix-login-button works but fix login button does not.

Creating a branch from a specific point in your project history

By default, git checkout -b branch-name creates your new branch from wherever you are right now in your project. But sometimes you want to branch from an earlier version. You might want to fix a bug that existed in an old release, or you might want to base new work on a stable version rather than work-in-progress code.

To create a branch from a specific point, type git checkout -b branch-name commit-hash, where commit-hash is the identifier of the version you want to branch from. You can find commit hashes by typing git log, which shows your project history with each version's hash at the top. You only need the first seven characters of the hash. For example, git checkout -b hotfix-old-bug a1b2c3d creates a new branch starting from that specific version.

Switching between branches and checking which one you are on

Once you have created branches, you move between them with git checkout branch-name. Type the name of the branch you want to work on, and Git switches your files to match that branch. Your files on disk will change to show the version of the code on that branch — any changes you made on a different branch will disappear from view until you switch back.

To see all the branches in your project and find out which one you are currently on, type git branch. The output shows a list of all branches, and the one you are on has an asterisk and a different color. If you have many branches, you can type git branch -a to see branches on your computer and on the remote server (if you are sharing the project with others).

Saving your work before switching branches

Git will not let you switch branches if you have unsaved changes in your current branch. This is a safety feature — Git wants to make sure you do not lose work by accident. If you try to switch and have changes, Git tells you to either commit those changes or stash them.

Committing means saving your changes permanently to the current branch. Type git add . to mark all your changes, then git commit -m "description of what you changed" to save them. Stashing means temporarily hiding your changes so you can switch branches, then bringing them back later. Type git stash to hide changes, switch branches, do your work, then type git stash pop when you come back to restore them. Stashing is useful when you are in the middle of something and need to switch to a different branch quickly.

Deleting branches you no longer need

As your project grows, you accumulate branches. Once you have merged a branch back into the main version and no longer need it, delete it to keep your project clean. Type git branch -d branch-name to delete a branch on your computer. Git will refuse to delete a branch if it has changes that have not been merged, which is another safety feature.

If you are absolutely certain you want to delete a branch even if it has unmerged changes, use git branch -D branch-name (capital D). If you pushed the branch to a remote server and want to delete it there too, type git push origin --delete branch-name. After you delete a branch, it is gone from your list, but the work you did on it is still saved in your project history if you committed it.

Understanding what happens when you switch branches

When you switch from one branch to another, Git changes the files on your computer to match the version on that branch. If you added a file on branch-A and then switch to branch-B, that file disappears from your folder — it is not deleted, it just does not exist on branch-B. When you switch back to branch-A, the file reappears. This can feel strange at first, but it is the whole point of branches: each branch is a separate version of your project.

This also means you can test different approaches in parallel. You might create one branch to try a new design and another branch to try a different design, switch between them to compare, and then keep the one you like and delete the other. Your main branch stays untouched the whole time, so if both experiments fail, your original work is still there.

Frequently Asked Questions

What is the difference between git branch and git checkout -b?

git branch branch-name creates a new branch but leaves you on your current branch. git checkout -b branch-name creates a new branch and when ready switches to it. Most people use the second one because it saves a step.

Can I rename a branch after I create it?

Yes. Type git branch -m old-name new-name to rename a branch you are not currently on, or git branch -m new-name if you are already on the branch you want to rename. If you pushed the old branch to a remote server, you will need to delete it there and push the new name.

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

Git will not let you switch if you have unsaved changes. You must either commit them to the current branch or stash them (hide them temporarily) before switching. This prevents you from losing work by accident.

Can multiple people work on the same branch at the same time?

Yes, but it requires careful coordination. Both people push their changes to the same branch on the remote server, which can create conflicts if you edit the same lines. Most teams avoid this by giving each person their own branch and merging into a shared branch when work is ready.

How do I know which branch is the main one?

The main branch is usually called main or master (older projects often use master). You can see which branch is the default by typing git branch -a and looking for the one marked with an arrow, or by checking your project settings on the remote server like GitHub or GitLab.