I'm looking for advice on what a good branching strategy looks like when using mostly open source code. Most of my dev experience is with writing and working with custom made code and only using open source libraries so I'm a bit out of my dept here.

The aim is to use an open source application and make mostly minor and a few major changes to the code for our companies usage. We may also want to push bug fixes upstream sometimes.

My key concern is how to handle version updates. Whats the best way to merge our custom changes into the main code once an update is available without creating multiple repos? This will happen a couple of times each year, we aim to be no more than 1 version behind the latest version.

My current plan is to create a new branch to pull the new version into, do a diff between this and the current dev branch and then merge the code in a third branch until merges are complete and stable. This will now become the new trunk and the old one tagged as an older version. Any changes that need to be pushed upstream will be added to the first branch.

Suggestion and improvements welcome.

  • Did you find a way to efficiently achieve this? I'm also looking for a similar solution. We will make significant changes to the project in our fork, but those will be possibly be overwritten when changes to the master happen, forcing us to redo our changes over and over. – user658182 Dec 28 '20 at 4:32

Git is not for versions. More precisely, git makes it easy to track project history, but does not make it easy to maintain a different variant or edition of a project.

Your suggested strategy of regularly merging your modifications with upstream changes is fine and may be unavoidable. However, you will have to merge the same differences again and again. So this is only bearable if your modifications stay close to the upstream code base, and if you only do fairly minor changes.

In general, the better approach is to not modify the upstream code, but to extend it using some plugin mechanism. Then, the upstream code is more like a library that you use. This lets you keep your modifications as separate code, and you won't have to repeatedly merge these modifications. This also makes it feasible to make more complex modifications.

To some degree, you can combine these approaches. E.g. only modify the upstream code to insert some hooks, and then use these hooks to insert your plugins. If the upstream project accepts a pull request with suitable hooks that is even better, because they can then be maintained as part of the upstream project. However, some open source projects may be hesitant to add hooks that are only used for proprietary extensions, so such a pull request also has a bit of a political dimension.

A note on repos versus branches: Git doesn't care. There isn't a huge difference between two repos and two branches inside one repo. In particular, every branch can be linked to its own remote repository/branch. However, if you are using tools such as GitHub then they do make a difference. If you want to make pull requests to the upstream project you need a public fork on GitHub, and all branches in that fork will be public. That means that you might need multiple forks: one where you push all your branches for internal collaboration in your project, and one where you only push branches that are supposed to be public.

  • I'm currently looking to do something similar (please see above comment). You mention a plugin system but the master doesn't have anything like that. If I implement my own, then am I not in the same boat that those will be overridden when new versions of the master are released? Do you have any references for implementing such a plugin system? Thanks! – user658182 Dec 28 '20 at 14:51
  • @user658182 Yes, those hooks for a plugin would be overridden as well. But it might be easier to rewrite the hooks for each new upstream version than to rewrite your entire code. Thus, the hooks would serve as an anti-corruption layer in DDD or be like a hardware abstraction layer in an operating system. – amon Dec 28 '20 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.