Creating a branch in Git means making a separate line of work that doesn't affect your main code
A branch in Git is a copy of your project at a specific point in time. When you create a branch, you get your own workspace where you can make changes, add features, or fix bugs without touching the main version of your code. Other people working on the same project can do the same thing on their own branches at the same time. Once your work is done and tested, you merge your branch back into the main code.
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 your branch. After you create it, you switch to that branch with git checkout branch-name. Many people combine both steps into one command: git checkout -b branch-name, which creates the branch and moves you into it at the same time.
Key Takeaways
- Use git checkout -b branch-name to create a new branch and switch to it in one step.
- Branch names should be short and describe what you're working on, like fix-login-bug or add-dark-mode.
- You can see all your branches with git branch, and the one you're currently on will have an asterisk next to it.
- Switching between branches with git checkout branch-name only works if you've committed or saved your current changes first.
The difference between creating a branch and switching to it
When you run git branch branch-name, Git creates the branch but leaves you on whatever branch you were already using. You then have to switch to it separately with git checkout branch-name. This two-step process is useful if you want to create a branch but keep working on your current one for a moment.
The shortcut git checkout -b branch-name does both things at once: it creates the branch and immediately moves you into it. This is what most people use because it's faster. After you run it, any changes you make will be saved to the new branch, not to the one you came from.
Naming your branch so others understand what you're doing
Branch names should be short and tell someone else (or you, later) what the branch is for. Good names include fix-password-reset, add-search-feature, or update-documentation. Avoid spaces in branch names — use hyphens instead. Git allows spaces, but they cause problems when you're typing commands or sharing branch names with teammates.
Some teams use a prefix to organize branches by type. For example, feature/dark-mode, bugfix/login-error, or docs/api-guide. This makes it easier to scan a list of branches and understand what each one is for. Ask your team if they have a naming convention before you start creating branches.
Checking which branch you're on and seeing all your branches
To see a list of all branches in your project, type git branch. The branch you're currently on will have an asterisk (*) next to its name. If you want to see branches on the remote server as well as your local ones, use git branch -a.
If you're ever unsure which branch you're on, you can also check with git status. The first line of the output will say "On branch branch-name". This is useful to run before you start making changes, so you don't accidentally edit the wrong branch.
Switching between branches you've already created
Once you've created a branch, you switch to it with git checkout branch-name. This moves you into that branch, and any new changes you make will be saved there. If you have uncommitted changes in your current branch, Git will warn you and refuse to switch until you commit or discard those changes.
If you want to keep your changes but move them to a different branch, you can use git stash to temporarily save them, switch branches, and then use git stash pop to bring them back. This is useful if you started work on the wrong branch by mistake.
Deleting a branch you no longer need
After you've merged your branch back into the main code, you can delete it with git branch -d branch-name. Git will refuse to delete a branch if it has changes that haven't been merged yet, which is a safety feature to prevent you from losing work by accident.
If you're absolutely sure you want to delete a branch even though it has unmerged changes, use git branch -D branch-name (capital D). This is permanent and cannot be undone, so only use it if you're certain you don't need that branch anymore.
Pushing your branch to the remote server so teammates can see it
When you create a branch on your computer, it only exists locally until you push it to the remote server (usually GitHub, GitLab, or Bitbucket). To push your branch, use git push origin branch-name. After you do this, your teammates can see your branch and pull it down to their own computers if they need to.
The first time you push a new branch, Git might tell you to use git push --set-upstream origin branch-name instead. This tells Git to remember that your local branch is connected to the remote one, so future pushes are simpler. After you set the upstream once, git push by itself will work.
Frequently Asked Questions
What happens to my changes if I switch branches without committing?
Git will stop you and show an error message. You must commit your changes, discard them, or stash them before you can switch. This prevents you from losing work by accident. If you stash, your changes are saved temporarily and you can bring them back later with git stash pop.
Can I create a branch from a branch that isn't the main one?
Yes. When you create a new branch, it's based on whatever branch you're currently on. If you're on a branch called feature/login and you create feature/login-validation, the new branch will include all the changes from feature/login. This is useful when you're building on top of work that hasn't been merged yet.
What's the difference between a local branch and a remote branch?
A local branch exists only on your computer. A remote branch exists on the server where your team stores the code. When you push a branch, you're sending your local branch to the remote server. Your teammates can then pull it down and work on it too.
Do I have to name my branch something specific?
No, but your team might have a naming standard. Check with them first. If there's no standard, just pick a name that describes what you're working on. Avoid names like test or temp because they don't tell anyone what the branch is for.