19

I have a situation like this:

            (master)
A - B - E - F
      \       
        C - D
            (feature-x)

Should I merge master into feature-x if I need critical fixes E and F into the feature-x branch to continue development and I intend to merge back into master?

Is there any disadvantage to repeated merging master into a feature branch and then the feature back into master when the feature branch might or might not be shared with other developers?

chepner
  • 497,756
  • 71
  • 530
  • 681
Chaudhry Junaid
  • 372
  • 5
  • 15

2 Answers2

15

The most common workflow for handling this situation would probably be to rebase your feature branch on the master branch:

$ git checkout feature-x
$ git rebase master

This gives you:

            (master)
A - B - E - F
             \       
               C - D
                   (feature-x)
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 7
    To address the case where `feature-x` is shared with other developers, though, merging in `master` would be better. – chepner Aug 12 '15 at 20:48
  • 3
    The choice of whether to merge `master` into `feature-x` or to rebase `feature-x` onto `master` is subjective. There are advantages and disadvantages to each. – ChrisGPT was on strike Aug 12 '15 at 20:58
  • Thank you, this is simple & easy to understand – wO_o Mar 03 '23 at 08:04
  • 1
    Just to point out that the rebase would yield commits `C'` and `D'` rather than _just_ `C` and `D` — rebasing would create new hashes for these commits while keeping the exact same changes. – rmoralesdelgado Mar 15 '23 at 17:59
12

So far as I understand, merging master into your feature branch isn't considered bad practice. @larsks answer gives some good info about how to use rebasing which is an option. But be sure to follow the golden rule "Do not rebase commits that exist outside your repository"(see Perils of rebasing).

To clarify: 'commits that exist outside your repository' would be public (pushed) commits.

If you wonder whether rebasing, is better than merging or vise versa, I would suggest you look over: 'Rebase vs. Merge'. The article points out that the answer to this is dependent on what you and your team think is best for your project.

For larger projects, I like the history to show exactly what happened. So where I work, we usually merge master into our feature branches to get them up to date with the latest code. Though, I wouldn't necessarily consider this a global best practice for everyone. However, it's not considered bad practice either.

Some others like to have very clean history, so for them rebasing may be considered the better option.