I have the following commits A->B->C->D. If i push D's SHA, i know that it will take also A,B,C along.
My requirement is a bash script that rebases the last commit and makes it first so it won't have any dependency.
So, A->B->C->D should become D->A->B->C so that i can use D's SHA in the push.
I've tried doing it multiple times, but i can't manage to get it done without breaking anything.
Have you done this before, or do you have any thoughts on how to do this in a bash script in order to be easily re-used?
There is no fully general solution to this, because comparing
DwithCwill depend on what's inC, which in turn depends on what's inB, and so on. As a silly but simple example, supposeC-vs-Dincludes this change:But, in the version before version
A, this reads:The correct adjustment is no doubt to make
Duse the singular, i.e., the change you want is now to replacesheepwithfox(singular).How will you automate the idea that the plural of
sheepissheepbut the plural offoxisfoxes, and the verb number (jumpsvsjump) must match the noun?That aside, in general, a rebase is simply a series of cherry-pick operations. To reproduce the
A, thenB, thenC, thenDsequence on some new base, you cherry-pick commitA(making a new commit,A'), then cherry-pickBontoA'making aB', then cherry-pickContoB', and so on. To change the order, you simply change the order of cherry-picking operations.Each commit can be labeled with a branch or tag name, so if you have this:
we would want to do this—let's start by labeling the end of the
...sequence as commitoand call itstart:Now we'll cherry-pick
Donto a new branchtmp, which we create starting from commito:A successful cherry-pick creates new commit
D':Let's now cherry-pick
AthroughCbut add yet another branch label,tmp2:Now we can delete the branch-name
yourbranchentirely, making it look as though commitsAthroughDare gone (they aren't, really, since git keeps things around for 30 days by default), and once we've done that we can renametmpand/ortmp2.Each cherry-pick can require manual intervention (manually fixing singular-vs-plural or whatever), so while you could automate some of this, you must be prepared to do something about failure. It's also odd (as noted in comments) that you should have to do this repeatedly: it suggests something is weird in your work-flow.