A branch in Git is a separate line of work. It lets you try new things without touching the main, working version of your project. This is the feature that makes teamwork actually possible.
The idea
Imagine your project is a book. The main branch is the published copy that everyone trusts. When you want to add a new chapter, you do not scribble in the published book. You take a fresh copy, write your chapter there, and only merge it back once it is good.
That fresh copy is a branch.
Doing it
Make a branch and switch to it:
git checkout -b add-dark-mode
Now work normally — add, commit, repeat. None of this affects main. When your feature is ready, you bring it back:
git checkout main
git merge add-dark-mode
Why this is great
- You can experiment freely. If the idea fails, just delete the branch. No harm done.
- Many people can each work on their own branch at the same time.
- The
mainbranch stays clean and working while everyone builds around it.
A word on merge conflicts
Sometimes two people change the same line, and Git cannot decide who is right. This is a merge conflict. It looks frightening the first time, but it is just Git asking you to pick which version to keep. You edit the file, choose the correct lines, and commit. That is all.
Simple habit
Make a new branch for each new feature or fix. Keep main always working. This single habit will save you from a surprising amount of pain. For the wider picture, see What is Git?.