0

I have a branch named A which is the main one, and a lot of branches r based on it. Now I want to add some codes in a commit in branch A, and I want it affects all the code behind it, which is all the branches. Should I use rebase?

Cup Y
  • 1
  • 1

1 Answers1

2

There is no short-cut here, you just have to manage each branch how you see fit.

The reason for this is that git doesn't know anything about the relationship between branches, only the relationship between commits. Your history might look like this:

abc123 \
       |--- def456 \
                   |--- fff111
                   |--- ddd222

Then branches are a set of pointers to that history:

  • branch A points at commit def456
  • branch B points at commit fff111
  • branch C points at commit ddd222

If you make a new commit aaabbb on branch A, you get this history:

abc123 \
       |--- def456 \
                   |--- fff111
                   |--- ddd222
                   |--- aaabbb
  • branch A points at commit aaabbb
  • branch B points at commit fff111
  • branch C points at commit ddd222

If you want commit aaabbb to be part of the history of branch B, you can either use git rebase, creating a new commit which has the same changes as "fff111" but a new commit hash:

abc123 \
       |--- def456 \
                   |--- fff111 [orphaned]
                   |--- ddd222
                   |--- aaabbb \
                               |--- f2f2f2

Or you can use git merge, creating a new commit which has both fff111 and aaabbb as parents:

abc123 \
       |--- def456 \
                   |--- fff111 --\
                   |--- ddd222    +--- 9f9f9f
                   |--- aaabbb --/

Plenty has been said elsewhere about the pros and cons of each approach, so you'll have to decide which makes sense for you. The answer might be different for different branches - you might rebase branch B, but merge into branch C.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • 1
    `git rebase` now has [`--update-refs`](https://stackoverflow.com/a/73429249/7976758) to automatically move dependent branches. – phd Oct 11 '22 at 18:08
  • 1
    @phd Presumably the refs (tips) wouldn't be rebased, except for one branch. So I don't think that will help in this case unless they are either based off of each other, or you merge them all into another branch first and then rebase that (with `--rebase-merges`). But by that point you might as well just rebase each branch separately- it's simpler. ;) – TTT Oct 11 '22 at 18:26
  • You can (manually) add update actions to your rebase-todo. I have not yet experimented with this new feature. Git needs to learn (somehow) the concept of "child branch" so that it can do this more automatically... – torek Oct 11 '22 at 21:46
  • @torek I've played with it recently and that's exactly what it does- it changes branches to point to a newly created commit during the rebase of another branch, using `update-refs` commands in the todo. (Which is why it wouldn't work for this question, where the branches aren't said to be chained, but presumably they just have a bunch of branches off of `main` that aren't related to each other.) – TTT Oct 11 '22 at 22:01