What a branch is and why you create one
A branch in Git is a separate line of work inside your repository. When you create a branch, you get your own copy of the project files where you can make changes without affecting the main version — usually called main or master. Other people working on the same project can keep their own branches too, and everyone's changes stay separate until someone decides to merge them back together.
You create a branch when you want to work on a feature, fix a bug, or test an idea without risking the stable version that other people depend on. Once your work is done and reviewed, you merge the branch back into main. This workflow is how teams prevent half-finished code from breaking things for everyone else.
Key Takeaways
- Create a branch with 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 and describe what you are working on — fix-login-bug or add-dark-mode are clearer than my-stuff.
- You can see all your branches with git branch and see which one you are currently on — it will have an asterisk next to it.
- Your new branch starts as a copy of whatever branch you were on when you created it, usually main.
- Pushing a branch to a shared repository with git push origin branch-name lets other people see your work and review it before merging.
The two-step method: create, then switch
The most straightforward way is to create the branch first, then move into it. Open your terminal or command prompt, navigate to your project folder, and type:
git branch new-feature
This creates a new branch called new-feature. You are still on your old branch — Git does not automatically move you. To switch to the new branch, type:
git checkout new-feature
Now any changes you make will be saved to this branch only. You can verify you are on the right branch by typing git branch — it will list all your branches and put an asterisk next to the one you are currently using.
The one-step method: create and switch at once
Most people use a faster shortcut that creates the branch and switches to it in one command:
git checkout -b new-feature
The -b flag means "create a new branch". This does exactly what the two-step method does, but in one line. Either way works — use whichever feels more natural to you.
If you are using a newer version of Git (2.23 or later), you can also use git switch -c new-feature, which does the same thing. The switch command is newer and slightly clearer about what it does, but checkout works everywhere.
Naming your branch so others understand it
Branch names should be short and tell someone what the work is about. fix-login-bug, add-dark-mode, and update-docs are all clear. Names like my-stuff, test, or branch1 make it hard for teammates to know what you are doing.
Many teams use a naming pattern. Some start with the type of work: feature/, bugfix/, docs/. Others include a ticket number: PROJ-123-fix-search. Check if your team has a standard — if they do, follow it so the branch list stays organized.
Use hyphens instead of spaces or underscores. Git accepts most characters, but hyphens are the easiest to read and type. Avoid special characters like @, #, or : — they can cause problems in some tools.
Seeing what branches you have and which one you are on
To list all branches on your computer, type:
git branch
You will see something like this:
main * new-feature old-work
The asterisk shows which branch you are currently on. In this example, you are on new-feature. The other branches still exist — you just are not using them right now.
If you want to see branches that exist on the shared repository (the one on GitHub, GitLab, or wherever your team stores the code), type:
git branch -r
This shows remote branches — the ones other people have pushed up. You can switch to a remote branch the same way you switch to a local one: git checkout branch-name.
Pushing your branch so others can see it
When you create a branch on your own computer, only you can see it. To share it with teammates, push it to the shared repository:
git push origin new-feature
The word origin is the standard name for the main shared repository. new-feature is the name of your branch. After you push, other people can see your branch and pull it down to their own computers if they need to review your work or build on it.
The first time you push a new branch, Git may tell you to set an upstream branch. You can copy the command it suggests, or type:
git push -u origin new-feature
The -u flag sets up tracking, which means future pushes and pulls on this branch will automatically know to use origin. After that, you can just type git push without naming the branch every time.
What happens when you switch branches
When you switch from one branch to another, Git changes the files in your folder to match that branch. If you were working on new-feature and switch back to main, the files will revert to the state they were in on main. Your changes on new-feature are still saved — they just are not visible until you switch back.
If you have unsaved changes when you try to switch branches, Git will stop you and ask you to commit or discard them first. This prevents you from accidentally losing work. If you are not ready to commit, you can use git stash to temporarily save your changes, switch branches, and come back to them later.
Frequently Asked Questions
What branch should I start from when I create a new one?
You will start from whatever branch you are currently on. Most of the time, you should be on main before you create a new branch. If you need to base your work on a different branch, switch to that branch first, then create the new one.
Can I delete a branch I do not need anymore?
Yes. Type git branch -d branch-name to delete a local branch on your computer. Use git push origin --delete branch-name to remove it from the shared repository. Git will warn you if you try to delete a branch that has not been merged yet.
What if two people create branches with the same name?
Git will not let you push a branch with a name that already exists on the shared repository. You will get an error and have to rename your branch or pull the other person's version first. This is rare if your team uses clear naming patterns.
Do I have to merge a branch right away?
No. A branch can exist for days or weeks while you work on it. Push it regularly so your work is backed up and teammates can see what you are doing. Merge it when the work is done and reviewed, not before.
Can I create a branch from a branch that is not main?
Yes. You can create a branch from any other branch. Switch to the branch you want to start from, then create the new one. The new branch will be a copy of whatever branch you were on.