What a branch is and why you make one

A branch in Git is a separate line of work inside your project. When you create a branch, you get your own copy of all your files at that moment. You can change things in that branch without touching the main version — usually called main or master — until you are ready. Think of it like making a photocopy of your project folder, editing the copy, and only merging those changes back when they are finished and tested.

You make a new branch when you want to work on a feature, fix a bug, or experiment without risking the stable version. If something goes wrong, you delete the branch and start over. The main branch stays untouched. This is how teams work on the same project without stepping on each other's changes.

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, lowercase, and describe what you are doing — like fix-login-bug or add-dark-mode.
  • You can see all your branches with git branch and delete a branch with git branch -d branch-name once you are done with it.
  • Your changes stay in the branch you created them in until you merge that branch back into main.

The two-step method: create and switch

The traditional way to make a branch uses two commands. First, you create the branch with git branch followed by the name you want:

git branch my-new-feature

This creates the branch but does not move you into it. You are still on whatever branch you were on before — probably main. To actually start working in the new branch, you switch to it with git checkout:

git checkout my-new-feature

Now any changes you make will happen inside my-new-feature, not in main. Your terminal prompt often shows which branch you are on, so you can verify you switched correctly.

The one-step shortcut: create and switch together

Most people use a shortcut that does both steps at once. The flag -b tells Git to create the branch and move into it in a single command:

git checkout -b my-new-feature

This is faster and clearer — you do not have to remember to switch after creating. You are when ready ready to start making changes. This is the method you will see in most team projects and tutorials.

Naming your branch so others understand it

Branch names should tell you and your teammates what the work is about. Use lowercase letters, hyphens to separate words, and keep it short. fix-login-button is better than FixLoginButton or fix_the_login_button_that_doesnt_work_properly. Common prefixes help organize branches by type:

  • feature/ for new functionality — feature/dark-mode
  • fix/ for bug fixes — fix-typo-homepage
  • docs/ for documentation changes — docs-api-guide
  • test/ for testing or experiments — test-new-layout

Some teams require this format, others do not. Check your project's guidelines before you start. A clear name saves time when you are looking back at old branches or trying to find which one has the work you need.

Seeing what branches you have

To list all the branches in your local copy of the project, type:

git branch

This shows every branch you have created, with an asterisk (*) next to the one you are currently on. If you want to see branches that exist on the remote server (where your team stores the code), add the -a flag:

git branch -a

This is useful when you are working with a team and want to see what branches other people have pushed up. It helps you avoid creating a branch with a name that already exists.

Deleting a branch when you are done

Once you have finished working in a branch and merged your changes back into main, you can delete it to keep things tidy. Use the -d flag:

git branch -d my-new-feature

Git will refuse to delete a branch if it has changes that have not been merged. If you are certain you want to delete it anyway — for example, if you abandoned the work — use the capital -D flag instead:

git branch -D my-new-feature

Deleting a local branch does not affect the remote version. If you pushed the branch to the server and want to remove it there too, you will need a separate command: git push origin --delete my-new-feature. This is usually done after a pull request is merged and the branch is no longer needed.

What happens to your changes when you switch branches

When you switch from one branch to another, Git updates your files to match that branch. If you have made changes in the current branch but have not committed them, Git will warn you and refuse to switch — it does not want to lose your work. You have three options: commit the changes first, save them temporarily with git stash, or create a new branch from your current state.

Once you commit your changes in a branch, they stay in that branch. Switching back to main will not show those changes — they only appear when you switch back to the branch where you made them. This is the safety net that makes branches so useful. You can experiment freely knowing the main version is protected.

Frequently Asked Questions

What is the difference between git branch and git checkout -b?

git branch creates the branch but leaves you on your current branch. git checkout -b creates the branch and moves you into it when ready. Both end with the same result, but checkout -b saves you a step and is more common in practice.

Can I rename a branch after I create it?

Yes. Use git branch -m old-name new-name to rename a branch you are not currently on, or git branch -m new-name if you are already in the branch you want to rename. If the branch has been pushed to the remote server, you will need to delete the old name there and push the new one.

What if I create a branch from the wrong starting point?

If you created a branch from main but meant to create it from a different branch, the simplest fix is to delete the branch and create it again from the right place. Use git branch -D wrong-branch, switch to the correct starting branch, then create the new branch from there.

Do I have to push my branch to the server?

No. Branches you create locally stay on your computer until you push them. You only need to push when you want teammates to see your work, when you are ready for a code review, or when you want a backup on the server. Use git push origin branch-name to send it up.

Can multiple people work in the same branch?

Yes, but it requires coordination. Both people pull the latest changes, make their own edits, and push them back. Git will warn you if there are conflicts — places where both of you changed the same lines. You will need to decide which changes to keep. For larger teams, it is usually clearer if each person works in their own branch and merges when done.