Creating a branch in Git means making a separate copy of your code where you can work without changing the main version
A branch is a parallel line of development. When you create one, Git copies your current code into a new workspace. You can edit, test, and experiment there while the main branch (usually called main or master) stays untouched. Once your work is solid, you merge the branch back into main. This is how teams work on features at the same time without stepping on each other's changes.
The most common way to create a branch is with the command git branch followed by the name you want. If you want to create a branch and switch to it in one step, use git checkout -b instead. Both work; the second one is faster if you plan to start coding right away.
Key Takeaways
- Create a new 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, like fix-login-bug or add-dark-mode.
- You can see all your branches by typing git branch with no other words, and the one you are currently on will have an asterisk next to it.
- Always make sure you are on the right branch before you start editing files, because your changes will go into whatever branch you are currently on.
The fastest way: create and switch in one command
Open your terminal or command prompt in your project folder. Type this command and press Enter:
git checkout -b my-new-feature
Replace my-new-feature with whatever you want to call your branch. Git will create the branch and move you into it immediately. You are now ready to edit files and make commits. Any changes you save will stay on this branch and will not touch your main code.
This is the method most people use because it saves a step. You create the branch and land on it in one action, so you do not have to remember to switch afterward.
Creating a branch without switching to it
Sometimes you want to create a branch but keep working on your current one for now. Use this command instead:
git branch my-new-feature
This creates the branch but leaves you where you are. You stay on your current branch and can switch to the new one later with git checkout my-new-feature. This is useful if you are in the middle of something and want to set up a branch for later work, or if you are creating a branch for someone else to use.
Naming your branch so others understand what it is
Branch names should be short and tell anyone reading them what the code inside is supposed to do. Good names are fix-password-reset, add-search-feature, or update-docs. Avoid spaces — use hyphens instead. Do not use uppercase letters; stick to lowercase and numbers.
Some teams follow a pattern like feature/dark-mode or bugfix/login-error to sort branches by type. Check with your team to see if they have a naming rule. If you are working alone, any clear name works fine.
A name like work or test or stuff makes it hard to remember what you were doing when you come back to it weeks later. Spend ten seconds on a good name now and save yourself confusion later.
Seeing which branches you have and which one you are on
Type this command to list all your branches:
git branch
Git will show you every branch in your project. The one you are currently on will have an asterisk (*) next to it. If you have many branches, press q to exit the list.
If you want to see branches on the remote server as well as your local ones, type git branch -a. Remote branches show up with origin/ in front of the name, like origin/main. This is useful when you are working with other people and want to see what branches they have pushed up.
Switching between branches you already have
Once you have created a branch, move to it with this command:
git checkout my-new-feature
Replace my-new-feature with the name of the branch you want to work on. Git will switch your workspace to that branch. Any files you open will show the version from that branch, and any changes you make will be saved there.
A common mistake is forgetting which branch you are on and making changes in the wrong place. Before you start editing, run git branch to confirm you are on the right one. The asterisk will show you exactly where you are.
What happens to your changes when you switch branches
If you have made changes but not committed them yet, Git will warn you before letting you switch. You have three choices: commit the changes first, save them temporarily with git stash, or cancel the switch. Git does this to protect your work — it will not let you lose changes by accident.
If you commit your changes before switching, they stay on the branch you were on. When you switch to a different branch, those changes disappear from your screen because they are not part of that branch's history. Switch back to the original branch and they reappear. This is the whole point of branches — each one is its own separate timeline.
Frequently Asked Questions
Can I create a branch from a branch that is not main?
Yes. Whatever branch you are currently on when you run git checkout -b new-branch becomes the starting point for the new branch. If you are on feature-one and create feature-one-part-two, the new branch will have all the code from feature-one. This is useful when one feature depends on another.
What if I create a branch with the wrong name?
Rename it with git branch -m old-name new-name. If you are currently on the branch you want to rename, you can skip the old name and just type git branch -m new-name. The rename only affects your local copy; if you have already pushed it to a server, you will need to delete the old one there and push the new name.
Do I have to delete a branch after I merge it?
No, but most people do to keep the list clean. Once a branch is merged into main and you are done with it, delete it with git branch -d branch-name. This removes it from your local list. If it is also on a remote server, you can remove it there too with git push origin --delete branch-name.
Can I see what code is different between two branches?
Yes. Use git diff branch-one branch-two to see all the changes between them. This shows you what code is in one branch but not the other, which is helpful before you merge to make sure you are not losing anything important.