Git rebase to master example |Video upload date:  · Duration: PT12M0S  · Language: EN

Clear example showing how to rebase a feature branch onto master with commands conflict handling and safe push tips

Quick overview

Rebase your feature branch onto master to keep a linear commit history and to apply the latest master branch changes to your work. This is a git workflow trick that looks neat in a log and causes mild panic when done accidentally on a shared branch. Follow these steps and you will be fine most of the time.

Before you start

Make sure your working tree is clean. Run git status and either stash or commit uncommitted changes. This avoids surprises and also spares your future self from cryptic error messages.

Step by step

  1. Update local master

    Switch to master and pull the latest updates from origin so your base is current.

    git checkout master
    git pull --ff-only origin master
  2. Switch back to your feature branch

    Either use checkout or switch. Take your pick.

    git checkout feature-branch
    # or
    git switch feature-branch
  3. Rebase onto master

    Replay your feature commits on top of the updated master history. History appears linear and tidy. Clean, like someone who actually writes tests.

    git rebase master
  4. Resolve conflicts if they happen

    If git stops with merge conflicts open the files, fix the problems, stage the fixes then continue the rebase. If the situation gets bleak use the abort option to go back.

    # after fixing files
    git add path/to/file
    git rebase --continue
    
    # if things are too wild
    git rebase --abort
  5. Push the rewritten branch

    Because rebase rewrites commits you will need to force push. Use the safer option to avoid trampling other peoples work.

    git push --force-with-lease origin feature-branch

Common pitfalls and how to avoid them

  • Rewriting shared history Rebase rewrites commits. If others are working on the same branch coordinate first or you will make enemies on the team.
  • Force push with care Use --force-with-lease to reduce the risk of overwriting remote work. It is not magic but it helps.
  • Conflicts Merge conflicts are not a crime. They are a conversation between code changes. Read both sides and decide which one is right.

Pro tip

If you want to squash or edit commits while rebasing try an interactive rebase. It lets you clean up commit history before pushing.

git rebase --interactive master

When to avoid rebase

If the feature branch is shared and people are basing work on it prefer merge. Rebase is best for local cleanup and making history pleasant to look at during code review. If you like surprises then go ahead. If you like coworkers then coordinate first.

Summary

Rebasing a feature branch onto master keeps history linear and brings in the latest master branch changes. The basic flow is prepare local repo, update master, checkout the feature branch, run git rebase master, resolve conflicts then push with --force-with-lease. Use interactive rebase for squashing and always communicate when rewriting shared history. Now go forth and rebase responsibly or at least convincingly.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.