What a branch is and why you create one
A branch in Git is a separate line of work that exists alongside your main code. When you create a branch, you get your own copy of the project files at that moment. You can make changes, add features, or fix bugs on that branch without touching the main version that other people are using.
Think of it like making a photocopy of a document before you edit it. The original stays clean. If your edits work out, you merge the branch back into main. If they don't, you delete the branch and the main version was never affected.
You create a branch when you're about to start work on something — a new feature, a bug fix, an experiment. It keeps your changes organized and lets other people keep working on main without waiting for you to finish.
Key Takeaways
- A branch is a separate copy of your project where you can make changes without affecting the main version.
- You create a branch with the command git branch branch-name, then switch to it with git checkout branch-name, or do both at once with git checkout -b branch-name.
- Branch names should be short, lowercase, 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.
- Once you finish your work and push the branch, you merge it back into main through a pull request or merge command.
Creating a branch with one command
The fastest way to create a branch and start using it is to run one command. Open your terminal or command prompt, navigate to your project folder, and type:
git checkout -b branch-name
Replace branch-name with something short that describes your work. Good examples are fix-typo, add-search-feature, or update-styling. Use lowercase letters, numbers, and hyphens only — no spaces or special characters.
This single command creates the branch and switches you to it at the same time. You're now working on your own separate copy. Any changes you make from this point forward will be on this branch, not on main.
Creating and switching as two separate steps
If you prefer to create a branch first and switch to it later, you can do that in two commands.
First, create the branch:
git branch branch-name
Then switch to it:
git checkout branch-name
This approach is useful if you're setting up multiple branches at once, or if you want to see all your branches before deciding which one to work on. The result is the same — you end up on the new branch, ready to make changes.
Checking which branch you're on
At any time, you can see all your branches and find out which one you're currently using. Type:
git branch
Git will show you a list of all branches in your project. The branch you're currently on will have an asterisk (*) next to it and usually appears in a different color. If you have only one branch, you're probably still on main or master — the default branch that Git creates when you start a project.
If you want to see branches that exist on the remote server (where your project is stored online), type:
git branch -a
This shows both your local branches and the ones that other people have pushed up.
Naming your branch so others understand it
Branch names matter because other people on your team will see them. A good branch name tells anyone reading it what you're working on, without them having to ask.
Use patterns like these: fix-login-error, add-user-profile, update-readme, remove-old-api. Start with the type of work (fix, add, update, remove), then describe what you're changing. Keep it short — three to five words is usually enough.
Avoid names like my-branch, test, new-stuff, or work-in-progress. These don't tell anyone what you actually did. Also avoid uppercase letters and spaces — Git handles lowercase and hyphens more reliably across different systems.
What happens after you create a branch
Once you're on your new branch, you work normally. You edit files, save them, and commit your changes with git commit just like you would on main. The difference is that all those commits stay on your branch — they don't show up on main until you merge.
When you're ready to share your work, you push the branch to the remote server with:
git push origin branch-name
This uploads your branch and all your commits. Other people can now see it, review your changes, and eventually merge it back into main. Most teams use a pull request (on GitHub or similar platforms) to review the changes before merging, which gives teammates a chance to comment and catch problems.
Once the branch is merged into main, you can delete it to keep your project clean. You'll have a fresh branch ready for your next task.
Frequently Asked Questions
What's the difference between git branch and git checkout -b?
git branch only creates the branch — you stay on whatever branch you were on before. git checkout -b creates the branch and switches to it in one step. For most people, git checkout -b is faster because you don't have to run two commands.
Can I create a branch from a branch that's not main?
Yes. When you create a branch, Git copies whatever branch you're currently on. So if you're on a branch called feature-x and you run git checkout -b feature-x-part-2, the new branch will be based on feature-x, not main. This is useful when you're breaking large work into smaller pieces.
What if I create a branch but don't use it?
You can delete it anytime with git branch -d branch-name. If you've already pushed it to the remote server, you'll also need to delete it there with git push origin --delete branch-name. Unused branches don't hurt anything, but deleting them keeps your project organized.
Do I have to push a branch right after I create it?
No. You can create a branch and work on it locally for as long as you want before pushing. Push when you're ready to share your work or back it up to the server. Until then, the branch exists only on your computer.
Can two people work on the same branch?
Yes, and it happens often. Both people push their commits to the same branch, and Git merges the changes together. If you both edit the same line of code, Git will flag a conflict and ask you to decide which version to keep. This is why clear communication with your team matters when you're sharing a branch.