Home > OS >  Is it possible to retroactively extract some code into a separate branch in git?
Is it possible to retroactively extract some code into a separate branch in git?

Time:06-15

I have the task to completely rewrite a web app using the latest versions of VueJS and CoreUI and what not. So I created a new branch for that, but now I realized that putting the entire rewrite into a single branch wasn't the best idea.

Because my lead-developer would like to review my code and now the branch I have is humongous which makes it very hard for him to review it properly.

What I should've done, of course, is to create sub-branches for every feature that I was re-implementing.

Now I'm no expert at git so I'm wondering if it may be possible to do this retroactively, at least to some degree.

One problem of course is that every file, every feature is in some way dependent on another file / feature. So if I would now just create a new branch and cut-and-paste a bunch of files from the huge branch to the smaller branch, then the huge branch will stop working until the smaller branch has been merged again.

Anyone has any idea what I could do in this situation to make my lead-developer's life a little easier without breaking stuff?

CodePudding user response:

There are several approaches you could take: You could try cherry-picking specific commits from this said humongous branch while into a different branch. For this you would need to know a range of commits that does some work and you would need to continue with newer branches created from the last one, a waterfall effect.

So now you have Master <--- HumongousBranch where you would likely need Master <--- SmallerBranch_1 <--- SmallerBranch_2 <--- SmallerBranch_3

The other approach, more drastic, less commit-rescuing: You could try and isolate the files you have the changes into, patch them into a patch file, then have a new branch from master or something and then apply this patch and commit changes for specific files, keeping different files into different branches.

Both approaches are possible but both of them take a while to be done and should most probably be done only by the person who created the changes in the first place.

CodePudding user response:

I would say it depends on how you commited your stuff.

If the history is pretty clean, you could use git rebase --onto to break your linear history into branches

---Z---X---Y <= Master
            \---A---B---C---D---E---F---G---H---I---J---K <= Humongous

Becomes

---Z---X---Y <= Master
                \---A---B <= VueJSLastVersionBranch
                         \---C---D---E <= Feat1
                         \---F---G---H <= Feat2
                         \---I---J---K <= Feat3    

If the history is not clean and the project is hudge, i guess it's pretty impossible (or it will be super-time consuming), you better have to re-write all you code (copying parts of your humongous branch) from scratch to have clean history/branches.

  •  Tags:  
  • git
  • Related