-1

I'm curious how others handle back supporting previous versions. I really hope the following helps explain what I mean.

IE: lets say I have the following git structure:

A -> B -> C -> D -> E -> F
     \
      G

if B is 1.0 and F is 2.0, but I had to go back and put some updates into B so I make a branch from B and add in my updates in G which is now 1.1, but the code changed so much from C on, that it really doesn't merge in and is only applicable to version 1.0 on B. Do you just keep that Branch and never merge it or how do you merge it in such a way that it ends up like the following:

A -> B -> G -> C -> D -> E -> F

Just curious how others handle back supporting previous versions or what you should do in this scenario with your repo?

Hope all that makes sense.

Thanks

FYI: Found how to insert in the middle at: How to inject a commit between some two arbitrary commits in the past?

Also think it depends how actively you are maintaining the previous version whether you keep the branch as 1.X and go forward with it, or you just back put in a fix and rebase based on the above link.

Hugo y
  • 1,421
  • 10
  • 20
zep
  • 1
  • 1
  • 4
  • Please type `repository` where you intend *repository*. And scrutinise tag description when deciding about use. – greybeard Dec 04 '20 at 09:27

2 Answers2

0

You have a couple options, but I want to address a matter of "how git works" first, as it may help you decide which way to go...

I have the following git structure:

A -> B -> C -> D -> E -> F
     \
      G

...

how do you merge it in such a way that it ends up like the following:

A -> B -> G -> C -> D -> E -> F

As written, this isn't possible. It may seem like I'm splitting hairs, because you could rewrite history to get something very similar, e.g.

A -> B -> G -> C' -> D' -> E' -> F'

But that's actually different in a very important way: It means that every commit from C onward was removed from history and replaced with a new commit, and assuming this is a shared repo, you'll end up with force a push which will cause problems for all other clones. See the section "recovering from upstream rebase" in the git rebase docs for an explanation of the issue: https://git-scm.com/docs/git-rebase

(The reason those commits are replaced is that a commit's history - i.e. what commits led up to it - is an integral part of the commit.)

Option 1 - reparent

So, with an understanding of that issue if you still want to make that change, what you're talking about is reparenting C. This is similar to a rebase, except that you're ignoring the changes that G would apply - effectively reverting them. (This might make sense if your normal workflow relies on rebasing branches to form a linear history, as this is basically the rebase equivalent to merge --ours.)

But how to do it? Up until recently I'd have suggested git filter-branch, but it seems that's fallen out of favor and the new shiny thing is git filter-repo; you can read about how to use it here: https://github.com/newren/git-filter-repo Warning: it's a very general-purpose tool, so that doc isn't exactly focused on just the operation you want to perform; but specifically because it's a very general and potentially dangerous tool, if you're going to use it then you should learn more about it than one recipe I would type into an answer.

Option 2 - merge

On the other hand, you refer to what you're wanting as a merge, which suggests that you use a merge-based workflow (rather than a rebase-and-fast-forward workflow). That is, maybe your diagram was just a misinterpretation and the above was all for naught.

If you normally would merge branches back together, then here you can say

git merge --ours branch

and git will create a merge commit but will ignore any changes from the branch being merged in

 A -> B -> C -> D -> E -> F -> M(!G)
      \                       /
       G ---------------------

This is only really important if you don't like having the diverging branch. That is, this is exactly what --ours merges are for, but then again --ours merges are only important if your team decides they're important.

Option 3 - Do nothing

Generally it won't hurt anything. It does mean that reports of unmerged branches would forever include the patch reelase branch.

Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
-1

If you can, use git cherry-pick which will allow you to backport the fix to G. This only works if you have commits that are granular enough so that your bugfix is container within specific commits, and not bunched together with other work.

Otherwise, you replicate the fix and branch off G to create H or v1.1.1.

A -> B -> C -> D -> E -> F -> I (2.0.1)
     \
      G -> H (1.1.1)
steadweb
  • 15,364
  • 3
  • 33
  • 47
  • Sorry, I'm not sure I follow. G would already contain the updates needed. IE I created a branch from B and added in my changes. So now I have a this branch just floating out there with changes. I'm under the assumption branches are suppose to be merged back into the master. Basically I would want to merge branch G between B and C commits if that is possible... otherwise I'm stuck with this branch floating out there permanently to keep track of v1.1 with these bug fixes – zep Dec 03 '20 at 15:34
  • How would you merge back in to master if you're maninting multiple versions of the software, in the same codebase? You need to have 2 branches where master would be your most up to date version (i.e. 2.x) and another branch that you support (i.e. release/1.x) – steadweb Dec 03 '20 at 15:56
  • Yeah, currently that is what I was looking to do, was just branch from version 1.0 and maintain that and my master branch 2.0. Really depends how many back fixes into 1.0 I would have to make I assume. I did find a way to inject a commit in the middle finally at the following link: https://stackoverflow.com/questions/32315156/how-to-inject-a-commit-between-some-two-arbitrary-commits-in-the-past – zep Dec 03 '20 at 16:02
  • Yup, rebase will work, but it rewrites history, as that answer suggests. Just be aware of anyone else who fetches your repo as they'll need to also rebase their work ontop of the new commits. – steadweb Dec 03 '20 at 16:07
  • I assume this would only affect people who already had the repo and did a pull? If you cloned the repo again they would be fine correct? – zep Dec 03 '20 at 16:17
  • Yeah that's correct. Any new clones will have the new commits you push. – steadweb Dec 03 '20 at 16:18