Git branches ideally should last for short time, probably for 1-2 days. Then it gets merged into some mainline.
But in some cases, where we work on very big features, we maintain branches. And when 2 or 3 people each work on these very big features in exclusive areas of code, it becomes kinda tough to maintain them.
Amidst hot fixes that go to stable branch, we need to keep these 2-3 big branches in sync with the stable branch. So we end up doing this, quite often.
(in feature-branch1) $ git merge stable (in feature-branch2) $ git merge stable (in feature-branch3) $ git merge stable
Is there a right way to maintain these long running branches in git? By doing the above, the git history is kind of messy. These feature branches mostly get pushed to a remote which means that we cannot see rebase
as an option. What else can I do?
Long-Running Branches This means you can have several branches that are always open and that you use for different stages of your development cycle; you can merge regularly from some of them into others.
Build your strategy from these three concepts: Use feature branches for all new features and bug fixes. Merge feature branches into the main branch using pull requests. Keep a high quality, up-to-date main branch.
You should branch whenever you cannot pursue and record two development efforts in one branch. (without having an horribly complicated history to maintain). A branch can be useful even if you are the only one working on the source code, or if you are many.
A branching strategy, therefore, is the strategy that software development teams adopt when writing, merging and deploying code when using a version control system. It is essentially a set of rules that developers can follow to stipulate how they interact with a shared codebase.
Merging in the stable branch every now and then is actually the easiest and best thing you can do to keep your feature branch up to date. I wouldn't recommend rebasing because there might be more people working on the feature at the same time and they would have to reset their local branches a lot every time there was a forced push. Even if only one person works on the feature branch, merging is better because it clearly shows the point in history where you brought in fixes from the stable branch.
Yes, your history can look kind of messy once you merge the feature branch back into stable. But as long as you're avoiding the mess of daily micro-merges from your git pulls (use git pull --rebase
instead), you will be able to appreciate actual meaningful merges.
If you really want to avoid merging in new features from stable into the feature branch, but just want bugfixes, you can cherry-pick bugfixes into the feature branch. However this can be a lot of work because it requires that you're always on top of everything that's happening in stable. Use git cherry
to help you out:
# commits from "stable" that are not present in # the current branch will be prefixed with "+" git cherry HEAD stable
Rebasing is possible if the team is small enough, and consists of reasonably experienced git users.
What you can do is, for instance, agree that the feature-branch will be rebased on stable every night. Or you could send an email when you rebase the feature-branch to stable.
Everything should be fine, but in case you forget to sync with the rebased feature branch, say you committed C
on top of the old feature branch oldFB
, which has been rebased into oldFB'
:
S1 - oldFB - C <-- feature-branch \ S2 - oldFB' - D <-- origin/feature-branch
It is still possible to rebase your commit(s) C
by running:
git checkout feature-branch git rebase --onto origin/feature-branch oldFB C
Note that you'll have to find manually the commit called oldFB
in the diagram above.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With