Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rebase git submodule and parent repo

My situation: You have a feature branch based off master, and someone commits to master. Now your history is like this:

A - B - C - D (master)
         \
           E - F - G  (feature)

So you'd like to rebase feature onto master for a clean history, as one does. But consider this: that repo is a submodule of another, and the parent repo references the submodule's commits like so:

A - B - C - D (submodule:master)
         \
           E - F - G  (submodule:feature)
           *    *
           *     *
           X - Y - Z             (parent:feature)
              (asterisks represent references to submodule)

If I rebase the submodule naively, the parent's references to submodule's commits will be invalid. Let's assume that some of the commits in the feature branches are meaningful enough to separate, so squashing them into one commit is out.

Any way to do it and maintain those references? (both 'feature' branches can be freely rewritten).

like image 780
iPherian Avatar asked Sep 14 '25 20:09

iPherian


1 Answers

the parent's references to submodule's commits will be invalid.

Those references only becomes invalid if they are lost by the rebase.

All you need to do is add a old_feature branch where feature is, before rebasing feature.
Don't forget to push the old_feature branch.

Then, once feature is rebased and pushed, you go to the parent repo, make sure its submodule follows the feature branch, and do an update remote:

git submodule update --remote

The commits X and Z can keep their old references to old_feature branch, while a new commit will keep a reference to the rebased feature branch of the submodule.


As jthill adds in the comments, reflog is still there locally, for 90 days, if you already rebased without referencing the old state of feature first:

you've got a month to fix the oops in that repo, git branch old-feature feature@{last.monday}

Then push that old-feature branch, in order to make sure E and F keep being referenced in the remote repository whose commits are referenced by X and Z.

like image 178
VonC Avatar answered Sep 17 '25 10:09

VonC