Branching and Merging
Branches let developers work in parallel without mixing changes together too early. This is one of the most important Git concepts.
Why branches exist
A branch is a separate line of work. Developers can create a branch for:
- a feature
- a bug fix
- a hotfix for production
- a proof-of-concept change
This keeps the main branch cleaner and safer.
Basic branch flow
git switch main
git pull origin main
git switch -c feature/login
Then do the work, commit, and push:
git add .
git commit -m "Implement login screen"
git push -u origin feature/login
Merge strategy
Once the work is ready, merge it into main:
git switch main
git pull origin main
git merge feature/login
If there are conflicts, Git will stop and ask for manual resolution.
Merge conflicts
A merge conflict happens when both branches modified the same lines.
Example conflict message:
CONFLICT (content): Merge conflict in app.py
To resolve:
- open the conflicted file
- decide which version is correct
- remove conflict markers
- stage the resolved file
- complete the merge
git add app.py
git commit -m "Resolve merge conflict"
Safe merge practices
- keep branches small and focused
- sync from main often
- review code before merging
- prefer clean, readable commit messages
Fast-forward merge
If no one changed main while the feature branch was in progress, Git may do a fast-forward merge.
This is the simplest kind of merge.
Merge commit
When both sides changed, Git creates a merge commit. This keeps the history visible and helps with rollback decisions.
Branch naming conventions
A simple convention is:
- feature/login-page
- bugfix/null-check
- hotfix/production-ssl
Good names are short, clear, and descriptive.
Production example
A release team may branch from main to create a release branch. QA tests it, then it is merged to production after approval. This gives a controlled release path.
Common pitfalls
- merging without checking branch status
- leaving feature branches open for too long
- resolving conflicts without understanding both sides
- rebasing without a clear reason
Key takeaways
- Branches give isolation and safety.
- Merging should be deliberate, not automatic.
- Clear branch names help the whole team.
- Merge conflicts are normal; they are a sign that the team needs to communicate about overlapping changes.