How to Use Git Feature Branches

How to Use Git Feature Branches

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:

bash
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

bash
git checkout main
git pull origin main

If your repo uses master

bash
git checkout master
git pull origin master

Tip: If you’re not sure which your repo uses, run:

bash
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:

bash
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):

bash
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:

bash
git status
git diff

Stage changes:

bash
git add .

Commit with a meaningful message:

bash
git commit -m "Add login page layout"

Repeat this cycle as you build the feature:

  1. change code
  2. git add
  3. git commit

Step 4: Keep your feature branch up to date

If main is moving while you work, you have two common options:

  • merge main into 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:

bash
git checkout main
git pull origin main

Then merge into your feature branch:

bash
git checkout feature/add-login-page
git merge main

If there are conflicts, Git will tell you which files need attention. After fixing conflicts:

bash
git add .
git commit

Push your updated branch:

bash
git push

Option B: Rebase onto main (cleaner history)

Update main first:

bash
git checkout main
git pull origin main

Rebase your feature branch:

bash
git checkout feature/add-login-page
git rebase main

If conflicts occur:

  • fix the files
  • then continue:
bash
git add .
git rebase --continue

After a rebase, you usually need to force-push (because history changed):

bash
git push --force-with-lease

Use --force-with-lease instead 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:

bash
git checkout main
git pull origin main

Merge your feature branch:

bash
git merge --no-ff feature/add-login-page

Then push:

bash
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:

bash
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:

bash
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:

bash
git branch -D feature/add-login-page

Delete the remote branch:

bash
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:

  1. Open the file(s) Git lists
  2. Look for conflict markers:
text
<<<<<<< HEAD
your current changes
=======
incoming changes
>>>>>>> main
  1. Edit the file to the final desired content
  2. Stage and finish:
bash
git add <file>

If you were merging:

bash
git commit

If you were rebasing:

bash
git rebase --continue

Suggested Branch Naming Conventions

Good names make your repo easier to navigate:

  • feature/add-login-page
  • feature/api-rate-limits
  • bugfix/fix-null-pointer
  • hotfix/patch-prod-crash
  • chore/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:

bash
# 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 main stable
  • 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 main branch rules

Happy branching.