How to create a new branch in Git
A new branch in Git is created with the command git branch followed by the name you want to give it. The simplest way is to type git branch feature-login (replacing "feature-login" with whatever you're working on), then switch to that branch with git checkout feature-login. Alternatively, you can do both steps at once with git checkout -b feature-login, which creates the branch and moves you into it immediately.
When you create a branch, you're making a copy of the current state of your code — usually the main branch — so you can work on changes without affecting the version other people are using. The branch stays on your computer until you push it to a shared repository like GitHub or GitLab, where teammates can see it and work with it too.
Key Takeaways
- Use git checkout -b branch-name to create a new branch and switch to it in one command.
- Branch names should be descriptive and lowercase with hyphens, like fix-login-bug or add-user-profile.
- Your new branch starts as a copy of whichever branch you were on when you created it, usually main or master.
- Push your branch to the shared repository with git push origin branch-name so others can see and work with it.
- You can switch between branches anytime with git checkout branch-name, as long as you've committed or stashed your changes first.
Creating a branch from your current location
When you run git checkout -b feature-login, Git creates a new branch that points to the exact commit you're currently on. If you're on the main branch at commit ABC123, your new feature-login branch also starts at ABC123. This means you're working from the latest stable code, which is usually what you want.
The -b flag tells Git to create the branch before checking it out. Without it, git checkout feature-login would fail if the branch doesn't exist yet. Some people prefer to separate the steps — running git branch feature-login first, then git checkout feature-login — but the combined command is faster and more common.
Creating a branch from a different commit or branch
If you need to base your new branch on something other than your current location, add the source at the end of the command. For example, git checkout -b hotfix-payment origin/main creates a new branch called hotfix-payment based on the latest version of main from the shared repository, even if you're currently on a different branch locally.
This is useful when you're in the middle of work on one feature and need to quickly fix a bug in production. You can branch from the stable main branch without having to switch away from your current work, commit it, or stash it. The new branch will contain all the commits from main up to that point, and your current branch stays untouched.
Naming your branch so others understand what you're doing
Branch names should be lowercase, use hyphens instead of spaces, and describe what the branch is for. Names like feature-user-authentication, fix-cart-total-bug, or docs-api-readme tell anyone reading the repository what work is happening. Avoid names like test, temp, or my-stuff — they don't convey meaning and make it hard to clean up old branches later.
Many teams use a prefix to categorize branches: feature/ for new functionality, fix/ for bug fixes, docs/ for documentation, and refactor/ for code cleanup. So you'd type git checkout -b feature/user-authentication or git checkout -b fix/cart-total-bug. Check your team's conventions before you start — they may have a standard format they follow.
Pushing your branch to the shared repository
Creating a branch on your computer doesn't make it visible to teammates. To share it, you push it to the remote repository with git push origin branch-name. The word "origin" refers to the default remote location — usually GitHub or GitLab — and "branch-name" is whatever you called your branch.
The first time you push a new branch, Git may tell you to set the upstream branch. You can either follow the suggestion it gives you, or use git push -u origin branch-name, which pushes the branch and sets up tracking in one step. After that, git push alone will push to the correct place. Once your branch is on the remote, other people can see it, check it out, and add commits to it.
Switching between branches and keeping your work safe
You move between branches with git checkout branch-name. But Git won't let you switch if you have uncommitted changes — it protects you from losing work by accident. If you're in the middle of something and need to switch branches, you have two options: commit your changes with git commit -m "message", or temporarily save them with git stash.
Stashing is useful when you're not ready to commit yet. git stash saves your changes in a temporary holding area, cleans up your working directory, and lets you switch branches. Later, you can come back to the same branch and run git stash pop to restore your changes. This keeps your commit history clean while letting you pause work and jump to something urgent.
Viewing all your branches and deleting ones you're done with
To see all branches on your computer, type git branch. The current branch will have an asterisk next to it. To see branches on the remote repository too, use git branch -a. This shows you what you're working with and helps you avoid creating duplicate branches by accident.
Once you've merged a branch back into main and no longer need it, delete it with git branch -d branch-name. If Git warns you that the branch hasn't been merged, it's protecting you from accidentally losing work — you can force deletion with git branch -D branch-name if you're certain. To delete a branch from the remote repository, use git push origin --delete branch-name. Cleaning up old branches keeps the repository organized and makes it easier to see what's actually being worked on.
Frequently Asked Questions
What's the difference between git branch and git checkout -b?
git branch creates a new branch but leaves you on your current branch. git checkout -b creates the branch and switches you into it immediately. Most people use git checkout -b because it's one step instead of two.
Can I create a branch from an old commit instead of the current one?
Yes. Use git checkout -b branch-name commit-hash, where commit-hash is the identifier of the commit you want to branch from. You can find commit hashes by running git log and looking at the long string next to each commit.
What happens to my branch if I don't push it?
Your branch stays on your computer only. No one else can see it or work on it. If your computer crashes or you delete the branch, the work is lost. Push to the remote repository to make it permanent and shareable.
Do I have to be on main to create a new branch?
No. Your new branch will be based on whatever branch you're currently on. If you're on feature-login and create feature-login-validation, the new branch includes all the commits from feature-login. This is useful for building on existing work, but usually you want to branch from main so you start from a clean state.
Can two people work on the same branch at the same time?
Yes, but you both need to pull and push carefully to avoid conflicts. Each person makes commits, pushes them, and the other person pulls them. If you both change the same lines in the same file, Git will flag a conflict that you'll need to resolve together before merging.