Creating a branch in Git means making a copy of your code at a specific point so you can work on changes without affecting the main version
A branch is a separate line of development. When you create one, Git takes a snapshot of your current code and lets you make changes in isolation. Other people can keep working on the main branch (usually called main or master) while you experiment, fix bugs, or build features on yours. When your work is ready, you merge the branch back in.
The most common way to create a branch is with the command git branch branch-name, where you replace branch-name with whatever you want to call it. Then you switch to that branch with git checkout branch-name. Or you can do both in one step with git checkout -b branch-name.
Key Takeaways
- Create a new branch with git branch branch-name, then switch to it with git checkout branch-name, or combine both steps with git checkout -b branch-name.
- Branch names should be lowercase, use hyphens instead of spaces, and describe what you are working on — like fix-login-bug or add-dark-mode.
- Your new branch starts from wherever you were when you created it, so switch to main first if you want to branch from the latest version.
- Use git branch with no arguments to see all your branches, and git branch -d branch-name to delete one after you are done with it.
The step-by-step process for creating and switching branches
Start by checking which branch you are currently on. Type git branch and press Enter. Git will show you a list of all branches on your machine, with an asterisk next to the one you are on right now.
If you want your new branch to start from the latest code on main, switch there first: git checkout main. Then pull the latest changes with git pull. This ensures your new branch will be based on the most recent version, not an old snapshot.
Now create the branch. Type git checkout -b feature-name, replacing feature-name with something that describes your work. Git will create the branch and move you to it in one command. You will see the branch name change in your terminal prompt or status bar.
Verify you are on the right branch by typing git branch again. The asterisk should now be next to your new branch name.
Naming branches so your team can understand them
Branch names matter because other people will see them. Use lowercase letters, numbers, and hyphens. Avoid spaces — Git allows them, but they cause problems in commands and scripts. Instead of "new feature", write new-feature.
Include a prefix that describes the type of work. Common patterns are feature/, fix/, docs/, and refactor/. So you might name a branch feature/user-authentication or fix/memory-leak. This makes it obvious at a glance what the branch is for.
Keep names short but specific. add-payment-processing is better than work-in-progress or temp. If your team uses a ticket system, include the ticket number: fix/PROJ-1234-login-timeout.
Branching from a specific point in your history
By default, your new branch starts from wherever you are right now. But sometimes you need to branch from an older version — to fix a bug in a released version, for example.
First, find the commit you want to branch from. Type git log --oneline to see recent commits with their short IDs. Scroll through the list until you find the one you want. Then create a branch from that point: git checkout -b branch-name commit-id. Replace commit-id with the actual ID from the log.
You can also branch from a tag if your project uses them. Type git checkout -b branch-name tag-name. This is useful when you need to patch an old release.
Pushing your branch so others can see it
When you create a branch locally, it only exists on your machine. To share it with your team or back it up to a server, you need to push it. Type git push -u origin branch-name. The -u flag tells Git to track this branch on the remote server and remember the connection for next time.
After the first push, you can just type git push without the branch name. Git will know where to send your changes. If you push again and Git says the branch does not exist on the remote, you probably forgot the -u flag the first time — just run the full command again.
Other people can now see your branch on the server. They can check it out with git checkout branch-name if they have pulled the latest list of remote branches with git fetch.
Deleting branches when you are finished
After you merge your branch back into main, you can delete it to keep your repository clean. Type git branch -d branch-name to delete it locally. Git will refuse if the branch has unmerged changes, which is a safety feature.
To delete a branch on the remote server, type git push origin --delete branch-name. This removes it from the shared repository so your team does not see old branches cluttering the list.
If you want to delete a branch that has unmerged work and you are sure you do not need it, use git branch -D branch-name (capital D) instead. This forces the deletion without checking.
Common mistakes when creating branches
The most common mistake is creating a branch from an outdated version of main. If you have not pulled recently, your new branch will be based on old code. Always run git pull before creating a branch, or at least switch to main and pull there first.
Another mistake is forgetting to push the branch. You create it, make commits, and assume your team can see it — but it only exists locally until you push. If you forget and someone asks for your branch, you will have to push it then, which can cause confusion about timing.
Typos in branch names are also common. If you create feature/user-authentification instead of feature/user-authentication, you will have to delete it and start over. Type carefully or use tab completion if your terminal supports it.
Frequently Asked Questions
What is the difference between git branch and git checkout -b?
git branch branch-name creates the branch but leaves you on your current branch. git checkout -b branch-name creates it and switches you to it in one command. Most people use the second one because it is faster.
Can I create a branch from someone else's branch?
Yes. First fetch their branch with git fetch, then check it out with git checkout branch-name. Git will automatically create a local copy that tracks theirs. You can then create a new branch from there with git checkout -b new-branch-name.
What happens to my uncommitted changes when I create a new branch?
If you have unsaved changes in files, they stay with you when you switch branches. Git will warn you if switching would overwrite them. Commit or stash your changes first with git stash if you want to move them to a different branch.
How do I rename a branch I already created?
If you are on the branch, type git branch -m new-name. If you are on a different branch, type git branch -m old-name new-name. If you already pushed it, delete the old one on the remote with git push origin --delete old-name and push the new one with git push -u origin new-name.
Can I have multiple branches open at the same time?
You can have multiple branches on your machine, but you can only be on one at a time. You switch between them with git checkout branch-name. Each branch remembers its own set of commits and changes, so you can work on one, switch to another, and come back without losing anything.