How to Merge Master into any Branch in Git |Video upload date:  · Duration: PT4M5S  · Language: EN

Quick practical guide to merging master into another branch with commands conflict handling and push best practices

Why merge master into your branch

If you are working on a feature branch and master keeps getting updates you will want to bring those changes into your branch. Doing this often avoids giant surprise conflicts on merge day and keeps CI from yelling at you. This guide shows the sane git commands to merge master into a branch while keeping your working tree tidy and your stress levels acceptable.

Prerequisites

  • Git installed and configured
  • A clean working tree without uncommitted experiments
  • Permission to push the branch if you plan to update remote

Step by step workflow

Short version first for the impatient humans. Then we will explain the drama in each step.

1 Switch to your target branch

Change context to the branch that needs master updates. Replace feature-branch with your branch name.

git checkout feature-branch
# or the newer form
git switch feature-branch

2 Update local master

Fetch remote changes and make sure your local master matches origin master. This ensures the merge brings the latest commits.

git fetch origin
git checkout master
git pull origin master

3 Merge master into your branch

Go back to your branch and merge. If master can be fast forwarded there will be no merge commit. If not a merge commit will record the moment master landed in your branch.

git checkout feature-branch
git merge master

If you need a linear history consider rebasing instead of merging. Rebase rewrites commits and will cause trouble if the branch is shared with others. Use rebasing only when you know what you are doing.

4 Resolve conflicts when they occur

If git reports conflicts run git status to see the files. Open each file, choose the correct code, then stage and finish the merge.

git status
# edit conflicted files
git add path/to/file
git commit

Pro tip If conflict resolution starts to look like therapy for code create a quick backup branch before you do anything drastic

git branch backup-feature-branch

Quick checklist before pushing

  • Run tests or a build to make sure nothing exploded
  • Verify the changes in a diff if you like to be paranoid
  • Push the updated branch to the remote
git push origin feature-branch

When to rebase instead of merge

Rebase moves your branch commits on top of master and gives a tidy linear history. Do not rebase commits that other people rely on. If your branch is private or you coordinate with reviewers rebase can be a nice option.

git checkout feature-branch
git rebase master
# resolve any rebase conflicts then
git rebase --continue

Troubleshooting and recovery

  • If the merge looks wrong you can abort with git merge --abort while the merge is in progress
  • If you have a backup branch you can reset to it and start over
  • For rebase problems use git rebase --abort to return to the pre rebase state

Final thoughts

Merging master into a feature branch is a simple habit that prevents nasty surprises later. Keep your working tree clean backup if you are nervous and pick merge or rebase based on your team workflow. Do this often and your future self will thank you or at least not cry in the pull request comments.

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.