Why Branches Are Needed
What happens if something goes wrong while developing a new feature? If you worked directly on main, the original is broken.
Branches solve this. Keep the original (main) stable at all times and do new work on a separate copy (branch). If you are happy with the result, merge it. If not, just discard it.
main ─────────────────────────────── (always stable)
\ /
feature ──── work ──── done (experimental)
Creating a Branch
Here is how to create a branch on the GitHub web interface.
- On the repository main page, click the branch selector above the file list (default:
main) - Type a new branch name in the text box:
add-profile - Click Create branch: add-profile from 'main'
Branch naming conventions:
- Use lowercase and hyphens (-)
- Make the name describe what the branch does:
fix-typo,add-dark-mode,update-readme
Editing Files on a Branch
After creating a branch, any changes you make are saved only to that branch.
- Confirm you are on the
add-profilebranch (the branch selector showsadd-profile) - Click
README.md→ click the pencil icon - Add content:
## About Me
- Name: John Doe
- Interests: coding, reading, cooking
- Goal: collaborate using GitHub
- Commit changes → message:
docs: add profile info - Select Commit directly to the
add-profilebranch
Comparing Branches
Check how main and add-profile differ.
- Switch the branch selector back to
main - Look at
README.md— your added content is not there - Switch back to
add-profile— the content is there
Branches are independent of each other.
Merging a Branch
Once your work is done, merge it into main. The next lesson covers this in detail with Pull Requests. For now, here is the quick approach.
- Click the Pull requests tab at the top of the repository
- Click New pull request
- Confirm the settings: base:
main← compare:add-profile - Preview the diff
- Create pull request → enter a message → Merge pull request
After merging, your changes are reflected in main.
"Why?" — Why Branches Are Essential for Teams
Working alone, you might not need branches. But on a team, they are essential.
While teammate A develops the login feature, teammate B works on the payment feature. If both commit directly to main, their code will conflict.
With branches:
- A works on the
feature/loginbranch - B works on the
feature/paymentbranch - Each finishes, gets reviewed, and merges into
main
Parallel development without conflicts.
Deep Dive
Deleting a Branch
Clean up branches after they are merged. Accumulated stale branches make management harder.
- After a Pull Request is merged, a Delete branch button appears
- Or go to the Code tab → N branches link → click the trash icon next to the branch
Deleting a branch does not remove its commit history. You can restore it at any time.
Default Branch Name — main vs master
The default branch used to be named master. Since 2020, GitHub changed the default for new repositories to main.
You may see master in older repositories or tutorials. The functionality is identical — only the name differs.
You can change the name in Settings → Default branch.
- Create an
add-profilebranch inmy-first-repo. - Edit
README.mdon that branch and commit. - Switch back to the
mainbranch and confirm the changes are not there.
Q1. What is the main reason for using branches?
- A) To share a repository across multiple accounts
- B) To work on new changes safely without touching the original (main)
- C) To upload files faster
- D) To auto-generate commit messages