You create branches to work on different features without breaking your main code. Think of a branch like making a copy of your project where you can experiment safely.
Why branches matter
Every Git repository starts with one branch called main (or sometimes master on older repositories). This is your stable, working version. When you want to add a new feature or fix something, you create a new branch. If your experiment works, you merge it back. If it doesn't, you delete the branch and nothing is lost.
This lets you try risky changes without fear. Your main code stays untouched until you decide the new work is ready.
Creating a branch
Make a new branch with:
git branch experiment
This creates a branch called experiment but keeps you on your current branch. To switch to the new branch:
git switch experiment
Or do both steps at once:
git switch -c experiment
The -c flag creates the branch and switches to it immediately. Use this version most of the time.
Now make some changes and commit them normally:
git add .
git commit -m "Try new approach to user login"
These commits only exist on the experiment branch. Your main branch is unchanged.
Seeing all branches
List your branches with:
git branch
The branch with the asterisk is your current branch. Switch between branches anytime with git switch [branch-name].
When you switch branches, Git changes all the files in your project to match that branch's state. Files appear and disappear as you switch. This is normal and expected.
Merging branches back
When your experiment works and you want to keep the changes, merge the branch back into main:
git switch main
git merge experiment
First you switch to the branch you want to merge into (usually main). Then you merge the other branch into it.
If Git can combine the changes automatically, it creates a merge commit. Sometimes it will open a text editor for you to write a merge message. The default message is usually fine.
Handling conflicts
Sometimes Git cannot merge automatically. This happens when you changed the same lines in both branches. Git marks the conflicts in your files like this:
<<<<<<< HEAD
Original line from main branch
=======
New line from experiment branch
>>>>>>> experiment
Edit the file to keep what you want, remove the conflict markers, then commit:
git add [conflicted-file]
git commit -m "Resolve merge conflict"
Most conflicts are straightforward to fix. Read both versions and decide which to keep.
Cleaning up branches
After merging, delete the branch you no longer need:
git branch -d experiment
Git prevents you from deleting branches with unmerged work. If you really want to delete a branch with unmerged changes, use -D instead of -d. Be careful with this.
Working with branches safely
Create branches for every new feature or fix. Name them clearly: fix-login-bug or add-search-function. This keeps your work organized and your main branch stable.
Small, focused branches are easier to review and merge than large ones with many changes. Make branches for single features, not collections of different work.