user101289
user101289

Reputation: 10422

Git usage with multiple branches

I've been using Git for years but mostly in a single developer scenario. Now I'm working on a project with a team, and I'm not sure of the best practice for this use case:

I have a branch (call it feature1) I've created to add a feature to the master. The feature1 branch hasn't been merged into master, but it's basically done. But I am now having to work on a new feature (which will go in a branch called feature2) that depends on some of the code I built in feature1.

Would I want to create feature2 as a branch of feature1? Or should I create feature2 as a branch of master, and then pull in the items I need from feature1?

What's the best way to handle this without causing problems down the road when both branches eventually get merged into master?

Upvotes: 1

Views: 51

Answers (2)

bitoiu
bitoiu

Reputation: 7464

Would I want to create feature2 as a branch of feature1? Or should I create feature2 as a branch of master, and then pull in the items I need from feature1?

Two options:

1. If you think feature1 will ship before feature2

You want to branch from feature1 and this is the usual adopted method.

2. If you think feature2 will ship without the full feature1

You want to branch from master, and add the commits that you need. You can also just get the code from copying it, but that's not nice.

Is there a better option?

Might not be possible in some cases but I would isolate what you need from feature1, possibly even merge to master. Consider this as the new base between feature1 and feature2.

For example, image that feature1 adds a method to an sdk an at the same time uses it. In feature2 you need the sdk method available. In this case you would isolate the sdk change and then create branches from there without having to copy code or have a branch fully dependent on another.

Anyway, the smaller the change the better: - Create small (but useful) feature branches - Open pull requests as soon as possible to have on-going reviews - Ask for feedback often and merge when ready

Other people might have different views, but as anything in SCM this has a bit of flavour to it.

Upvotes: 0

Tim
Tim

Reputation: 43304

Would I want to create feature2 as a branch of feature1?

Yes. Branch off of the oldest commit possible while still having all required commits that feature2 needs. (Meaning: if feature1 has 5 commits and for feature2 you need only the first 2, branch off of the 2nd.) Then you basically have feature1 and feature2 share the same ancestor, which is perfectly fine.

Or should I create feature2 as a branch of master, and then pull in the items I need from feature1?

No, you don't want the same commit in multiple places. It sometimes happens that a commit gets changed. This would make it harder to keep track of things.

Upvotes: 2

Related Questions