What a branch does and when you need one
A branch in Git is a separate line of work that lets you make changes without touching the main version of your project. When you create a branch, you get your own copy of all the files at that moment. You can edit, add, or delete things in that branch, and the main version stays exactly as it was. Once your work is done and tested, you merge the branch back into the main version.
You need a branch whenever you are working on something that is not finished yet — a new feature, a bug fix, an experiment, or changes that other people need to review before they go live. Without branches, everyone working on the same project would be editing the same files at the same time, and changes would collide constantly.
The main branch in most projects is called main or master. That is the version people actually use. Every other branch is temporary — you create it, work in it, and delete it once the work is merged back.
Key Takeaways
- Create a branch with git branch branch-name, then switch to it with git checkout branch-name, or do both in one step with git checkout -b branch-name.
- Always create a new branch from the main branch so your work starts from a known good state, not from someone else's incomplete changes.
- Give your branch a short, descriptive name that tells other people what you are working on — like fix-login-button or add-dark-mode.
- Switch between branches with git checkout branch-name, and check which branch you are on with git branch or git status.
- Once your work is done, you push the branch to the shared repository and create a pull request so others can review it before merging.
Creating your first branch from the command line
Open your terminal or command prompt and navigate to your project folder. Make sure you are on the main branch first by typing git checkout main. Then pull the latest version with git pull so you are starting from the most recent code, not an old snapshot.
Now create your branch. The simplest way is one command: git checkout -b branch-name. Replace branch-name with something short and clear. If you are fixing a login bug, call it fix-login-bug. If you are adding a dark mode feature, call it add-dark-mode. Use hyphens between words, not spaces or underscores.
Git will create the branch and switch you into it when ready. You can verify this by typing git status — the output will show you which branch you are currently on. Now any changes you make will be saved to this branch only, not to main.
Understanding branch names and when to create them
Branch names should be short enough to type but descriptive enough that someone reading the name knows what the branch is for. fix-password-reset is better than fix-stuff. update-homepage-layout is better than changes. If your project has a naming convention — like starting all bug fixes with bug/ or all features with feature/ — follow it so branches stay organized.
Create a new branch for each separate piece of work. If you are fixing two different bugs, make two branches. If you are adding a feature and then fixing a bug in that feature, do the feature in one branch, merge it, then create a second branch for the bug fix. This keeps changes organized and makes it easier to undo one piece of work without undoing everything else.
Never create a branch from another incomplete branch unless you are intentionally building on someone else's work. Always start from main, which is the stable version everyone agrees on. If you create a branch from a branch that has not been merged yet, you will inherit all of that person's changes, and if their work gets rejected or changed, yours becomes complicated to fix.
Switching between branches and checking your location
Once you have created branches, you will need to move between them. Use git checkout branch-name to switch. If you are on the fix-login-bug branch and need to switch to add-dark-mode, type git checkout add-dark-mode. Git will update all your files to match that branch.
Before you switch, make sure you have saved your changes. Type git status to see if you have any unsaved work. If you do, either commit it with git commit -m "message" or stash it with git stash (which temporarily hides changes so you can switch branches and come back to them later). If you try to switch with unsaved changes, Git will warn you and refuse to move.
To see all the branches in your local project, type git branch. The branch you are currently on will have an asterisk next to it. To see branches on the shared repository as well, type git branch -a. This is useful when working with other people — you can see what branches they have created.
Pushing your branch to the shared repository
When your work is done and you have committed your changes, you need to push the branch to the shared repository so other people can see it. Type git push origin branch-name. The first time you push a new branch, Git may ask you to set the upstream branch — just follow the instruction it gives you, or type git push -u origin branch-name to do it in one step.
Once the branch is pushed, you can create a pull request (also called a merge request in some tools). This is a formal way of saying "I have finished this work, please review it and merge it into main if it looks good." The pull request shows everyone what changes you made, lets them comment on the code, and gives them a chance to ask questions or request changes before your work becomes part of the main version.
Different teams have different rules about who can approve a pull request and when it can be merged. Some require one other person to review it. Some require tests to pass automatically. Some require a manager to sign off. Ask your team what the process is before you push.
Deleting branches after they are merged
Once your branch has been merged into main, you can delete it. This keeps the repository clean and prevents old branches from piling up. You can delete a branch locally with git branch -d branch-name. If the branch has not been fully merged, Git will refuse and ask you to use -D instead (capital D) if you really want to delete it.
You should also delete the branch from the shared repository. Type git push origin --delete branch-name. Many pull request tools (like GitHub or GitLab) offer a button to delete the branch automatically after merging, which is faster than doing it by hand.
Deleting a branch does not delete your work — the changes are still in main because you merged them. Deleting just removes the branch pointer, which is a label pointing to a set of commits. Once the commits are in main, the branch label is no longer needed.
Troubleshooting common branch problems
If you try to switch branches and Git says you have unsaved changes, you have two options. Commit the changes with git commit -m "message" if you want to keep them, or discard them with git checkout -- . (note the space and period at the end). The second option erases all changes in your current branch, so only use it if you are sure you do not want them.
If you created a branch with the wrong name, you can rename it. Type 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 on the branch you want to rename. If you have already pushed the old name to the shared repository, you will need to delete the old branch there and push the new one.
If you are on a branch and want to see what changes you have made compared to main, type git diff main. This shows you every line you added, removed, or changed. If the output is very long, press the spacebar to scroll down and q to quit.
Frequently Asked Questions
Can I create a branch if I have unsaved changes in my current branch?
No. Git will refuse to create or switch to a branch if you have changes that have not been committed. Commit your current work first with git commit -m "message", or stash it temporarily with git stash if you are not ready to commit yet.
What happens if I delete a branch by accident?
If you deleted it locally but it still exists on the shared repository, you can recreate it by checking it out: git checkout branch-name. If you deleted it from the shared repository too, you can still recover it if the commits have not been garbage-collected, but this is complicated — ask someone on your team for help or check your repository's documentation.
Do I have to merge my branch into main, or can I merge it into a different branch?
You can merge into any branch, but main is the standard target because it is the stable version. If you are working on a feature that depends on another incomplete feature, you might merge into that feature's branch instead, but this is unusual and should be discussed with your team first.
How do I see what commits are in my branch that are not in main?
Type git log main..branch-name to see all commits in your branch that are not in main. This is useful before you create a pull request so you know exactly what you are asking people to review.
Can multiple people work on the same branch?
Yes, but it requires coordination. If two people push changes to the same branch, Git will ask the second person to pull the first person's changes before pushing. This can create merge conflicts if you both edited the same lines. It is usually easier to give each person their own branch and merge them separately.