How to Use Git Feature Branches (and Merge Back into Main)
Shipping changes directly to main/master works… until it doesn’t. Feature branches give you a clean, low-risk workflow: you develop in isolation, test safely, and merge only when the work is ready.
This post walks through a practical, repeatable process for:
- creating a feature branch
- making commits
- keeping it updated
- merging back into main (or master)
- cleaning up afterward
Prerequisites
You’ll need:
- Git installed
- a local clone of your repository
- a remote named origin (common default)
Check your current branch and status:
git status
git branch
Step 1: Start from an up-to-date main branch
Before branching, make sure your base branch is current.
If your repo uses main
git checkout main
git pull origin main
If your repo uses master
git checkout master
git pull origin master
Tip: If you’re not sure which your repo uses, run:
git branch --show-current
git remote show origin
Step 2: Create your feature branch
Pick a branch name that describes the work. A common pattern is:
feature/<short-description>bugfix/<short-description>hotfix/<short-description>
Example:
git checkout -b feature/add-login-page
This does two things:
- creates the branch
- switches you to it
Push it to the remote (so it’s backed up and visible to others):
git push -u origin feature/add-login-page
The -u sets the upstream, so future git push and git pull are simpler.
Step 3: Make changes and commit as you go
Edit files, then check what changed:
git status
git diff
Stage changes:
git add .
Commit with a meaningful message:
git commit -m "Add login page layout"
Repeat this cycle as you build the feature:
- change code
git addgit commit
Step 4: Keep your feature branch up to date
If main is moving while you work, you have two common options:
- merge
maininto your feature branch (simpler) - rebase your feature branch onto
main(cleaner history)
Option A: Merge main into your feature branch (simple + safe)
First update local main:
git checkout main
git pull origin main
Then merge into your feature branch:
git checkout feature/add-login-page
git merge main
If there are conflicts, Git will tell you which files need attention. After fixing conflicts:
git add .
git commit
Push your updated branch:
git push
Option B: Rebase onto main (cleaner history)
Update main first:
git checkout main
git pull origin main
Rebase your feature branch:
git checkout feature/add-login-page
git rebase main
If conflicts occur:
- fix the files
- then continue:
git add .
git rebase --continue
After a rebase, you usually need to force-push (because history changed):
git push --force-with-lease
Use
--force-with-leaseinstead of--force. It’s safer because it refuses to overwrite remote work you don’t have locally.
Step 5: Merge the feature branch back into main
Once the feature is complete and reviewed/tested, merge it into main.
Recommended: fast-forward disabled (creates a merge commit)
First make sure main is current:
git checkout main
git pull origin main
Merge your feature branch:
git merge --no-ff feature/add-login-page
Then push:
git push origin main
If your team prefers a fast-forward merge (no merge commit)
This only works if main hasn’t diverged and Git can fast-forward:
git checkout main
git pull origin main
git merge feature/add-login-page
git push origin main
Step 6: Delete the feature branch (cleanup)
After merging, it’s good hygiene to delete the branch.
Delete locally:
git branch -d feature/add-login-page
If Git warns the branch isn’t fully merged (rare if you merged correctly), you can force delete:
git branch -D feature/add-login-page
Delete the remote branch:
git push origin --delete feature/add-login-page
Handling Merge Conflicts (Quick Guide)
Conflicts happen when Git can’t automatically combine changes.
When you see a conflict:
- Open the file(s) Git lists
- Look for conflict markers:
<<<<<<< HEAD
your current changes
=======
incoming changes
>>>>>>> main
- Edit the file to the final desired content
- Stage and finish:
git add <file>
If you were merging:
git commit
If you were rebasing:
git rebase --continue
Suggested Branch Naming Conventions
Good names make your repo easier to navigate:
feature/add-login-pagefeature/api-rate-limitsbugfix/fix-null-pointerhotfix/patch-prod-crashchore/update-dependencies
If you use issue tracking, include the ticket ID:
feature/ABC-123-add-login-page
A Simple End-to-End Example
Here’s the full workflow in one place:
# Start clean and up to date
git checkout main
git pull origin main
# Create a feature branch
git checkout -b feature/add-login-page
git push -u origin feature/add-login-page
# Work + commit
git add .
git commit -m "Add login page layout"
git push
# Merge back into main
git checkout main
git pull origin main
git merge --no-ff feature/add-login-page
git push origin main
# Cleanup branches
git branch -d feature/add-login-page
git push origin --delete feature/add-login-page
Final Thoughts
Feature branching is one of the easiest ways to reduce risk and improve collaboration:
- you keep
mainstable - you can test and review changes in isolation
- merges are predictable and auditable
If you want to level this up further, pair feature branches with:
- pull requests / merge requests
- CI checks (tests + linting)
- protected
mainbranch rules
Happy branching.