Git
Branching
Master Branch
Version Control
Coding Tips

Update Git branches from master

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Keeping a feature branch current with master is routine Git hygiene that reduces late merge surprises. The two main approaches are merge and rebase, and each has different history and collaboration implications. Choosing intentionally, instead of by habit, prevents avoidable conflicts and force-push incidents.

Prepare the Branch Before Updating

Start with clean local state.

bash
git status

If you have uncommitted work, commit it or stash it first.

bash
git stash push -m "wip before branch sync"

Then fetch remote refs.

bash
git fetch origin

Fetching first keeps updates explicit and avoids operating on stale branch tips.

Method 1: Merge master Into Feature Branch

Merge preserves exact branch history and creates a merge commit when needed.

bash
git checkout feature/payment-flow
git merge origin/master

If conflicts appear, resolve files, add them, and complete merge.

bash
git add .
git commit

Then push branch.

bash
git push origin feature/payment-flow

Use merge when shared branch history should remain explicit or when team policy discourages rewriting commits.

Method 2: Rebase Feature Branch Onto master

Rebase replays feature commits on top of the latest master, creating a cleaner linear history.

bash
git checkout feature/payment-flow
git rebase origin/master

During conflicts:

bash
1git status
2# edit files
3git add <resolved-files>
4git rebase --continue

After successful rebase, push with force-with-lease.

bash
git push --force-with-lease origin feature/payment-flow

--force-with-lease is safer than plain force because it checks remote branch state first.

Merge or Rebase Decision Guide

Use merge when:

  • Branch is shared by multiple developers.
  • You want to preserve exact integration points.
  • Team prefers non-rewritten history.

Use rebase when:

  • Branch is personal or tightly coordinated.
  • You want a linear review history.
  • You can safely force-with-lease after rewrite.

The right choice depends on branch ownership and collaboration model.

Conflict Resolution Practices

Conflict quality matters more than conflict speed.

Recommended workflow:

  1. Resolve one file at a time.
  2. Run tests after each logical conflict group.
  3. Re-read final diff to ensure intended behavior.

Useful commands:

bash
git diff
git log --oneline --graph --decorate -20

This helps verify you did not accidentally drop necessary changes while resolving conflicts.

Keep Branches Fresh to Minimize Pain

Large divergence from master creates harder conflict sessions. Update frequently rather than waiting until merge day.

A practical cadence for active branches is daily fetch and periodic sync from master.

bash
git fetch origin
git rebase origin/master

Frequent small syncs are easier to debug than one massive catch-up.

Special Case: master Renamed to main

Many repositories now use main instead of master. The commands are identical except branch name.

bash
git fetch origin
git rebase origin/main

Check default branch with:

bash
git remote show origin

Keep team docs and scripts aligned with actual default branch to avoid confusion.

If your repository enforces pull-request checks, update branch before requesting review. Reviewers spend less time on conflict-only diffs, and CI signals are more reliable.

This keeps integration cadence predictable.

Common Pitfalls

A common pitfall is rebasing a shared branch without informing teammates, which rewrites commits they may already use. Another issue is using plain force push after rebase, which can overwrite remote updates accidentally. Teams also often merge or rebase without fetching first, producing avoidable conflict noise. Ignoring test runs after conflict resolution is another recurring mistake. Finally, long-lived branches that rarely sync with master accumulate large integration risk and slow release work.

Summary

  • Fetch first, then update branch from latest master.
  • Use merge for history preservation and shared-branch safety.
  • Use rebase for cleaner linear history on controlled branches.
  • Resolve conflicts carefully and validate with tests before push.
  • Sync feature branches frequently to reduce integration risk.

Course illustration
Course illustration

All Rights Reserved.