What a branch is and why you create one

A branch is a separate copy of your project where you can make changes without touching the main version. When you create a branch, Git gives you your own workspace. You edit files, save them, and commit them to that branch alone. The main version — usually called main or master — stays untouched until you decide to merge your branch back into it.

You create a branch before you start work on a new feature, a bug fix, or any change that might take multiple commits. This way, if something goes wrong, you can delete the branch and start over without breaking anything in main. If the work is good, you merge it back and the changes become permanent.

The branch exists only on your computer until you push it to a shared repository like GitHub or GitLab. Other people can then see it, review your work, and help you decide whether to merge it.

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 the work: fix-login-button or add-dark-mode are clearer than changes or test.
  • You can see all your branches with git branch, and the one you are currently on will have an asterisk next to it.
  • Pushing a branch to GitHub or GitLab with git push origin branch-name makes it visible to others and backs it up off your computer.

Creating and switching to a branch in one command

The fastest way is to use git checkout -b branch-name. This creates the branch and moves you into it in a single step. Open your terminal or command prompt, navigate to your project folder, and type:

git checkout -b fix-login-button

Replace fix-login-button with a name that describes what you are doing. Git will create the branch based on whatever branch you were on before (usually main), and you will now be inside that new branch. Any files you edit and commit from this point go into fix-login-button, not main.

If you want to see which branch you are on at any time, type git branch. The branch with an asterisk next to it is your current location.

Creating a branch without switching to it

Sometimes you want to create a branch but stay where you are. Use git branch branch-name without the -b flag:

git branch fix-login-button

This creates the branch but leaves you on whatever branch you were on before. You can switch to it later with git checkout fix-login-button. This approach is less common — most people use the one-step method — but it is useful if you are setting up multiple branches at once or if you want to finish a commit on your current branch before moving.

Naming your branch so others understand it

Use a short name that says what the branch does. fix-login-button, add-dark-mode, and update-readme are clear. Names like changes, test, work, or temp tell nobody what is in the branch and make it hard to find later.

Most teams use hyphens to separate words, not underscores or spaces. Avoid capital letters — Git treats Fix-Login and fix-login as different names on some systems and the same on others, which causes confusion. Stick to lowercase letters, numbers, and hyphens.

If your team uses a ticket system like Jira, you might start the branch name with the ticket number: PROJ-123-fix-login-button. This links the branch to the ticket and makes it straightforward for others to find the related work.

Pushing your branch so others can see it

Once you have created a branch and made some commits, push it to GitHub, GitLab, or whatever service you use. Type:

git push origin branch-name

Replace branch-name with your actual branch name. The first time you push a new branch, Git might ask you to set the upstream branch. It will tell you the exact command to run — usually something like git push -u origin branch-name. The -u flag tells Git to remember this connection, so next time you just type git push.

Once pushed, your branch is backed up on the server and visible to anyone with access to the repository. They can see your commits, review your code, and discuss whether to merge it into main.

Switching between branches

As you work, you may need to switch back to main or to a different branch. Use git checkout branch-name:

git checkout main

This moves you back to the main branch. Your files will change on disk to match whatever is in main. Any uncommitted changes in your current branch will block the switch — Git will warn you and ask you to commit or discard them first. This is a safety feature: Git does not want you to lose work by accident.

If you have changes you are not ready to commit, you can use git stash to temporarily save them, switch branches, and then come back and unstash them later. But for most work, commit before you switch.

Deleting a branch you no longer need

Once you have merged a branch into main and no longer need it, delete it to keep your branch list clean. First, switch away from the branch:

git checkout main

Then delete it locally with:

git branch -d fix-login-button

If you pushed the branch to GitHub or GitLab, you should also delete it there. Most services show a button to delete the branch right after you merge it. Or use the command line:

git push origin --delete fix-login-button

Deleting a branch does not delete your commits — they stay in main if you merged them. It just removes the branch pointer, making the list easier to read.

Frequently Asked Questions

What happens if I create a branch but never merge it?

Nothing bad happens. The branch sits there until you decide what to do with it. You can merge it later, delete it, or leave it alone. If it is pushed to a shared repository, others can see it and work with it. Unmerged branches do not affect main or anyone else's work.

Can I create a branch from a branch that is not main?

Yes. When you run git checkout -b new-branch, Git creates the new branch based on whatever branch you are currently on. If you are on fix-login-button and create fix-login-button-styling, the new branch will include all the commits from fix-login-button plus any new ones you add to it.

What if two people create branches with the same name?

Git treats them as separate branches on each person's computer until they push. Once pushed to a shared repository, the second person to push will get an error saying the branch already exists. They will need to rename their branch or coordinate with the first person to avoid confusion.

Do I have to push a branch right after I create it?

No. You can create a branch, work on it for days, and push it whenever you want. Pushing early is safer because it backs up your work on the server, but it is not required. Push when you are ready to share the work or when you want a backup.