5 ms·
Regarding "rebase --onto" > Imagine that for some reason I just want to move commits F and G to be rebased on top of main. I think there’s probably some git wo
by kazinator 3y ago
Regarding "rebase --onto"
> Imagine that for some reason I just want to move commits F and G to be rebased on top of main. I think there’s probably some git workflow where this comes up a lot.
This comes up for me in a branch where I have some necessarly local changes which are permanently there and have to be maintained, and that branch experiences non-fast-forward changes from upstream.
Say we are up-to-date in this branch, plus our two local commits that are not in upstream.
We do a fetch. Upstream has rewritten 17 commits. So now we have 19 diverging local commits. We only care about two of them. We just want to accept the diverged upstream commits, and then rebase the two on top of that.
We do not want to rebase all 19 commits!
We can do:
git rebase HEAD^^ --onto origin/master
So HEAD^^ is the "upstream" here that we have explicitly specified. So following the documentation:
- All changes made by commits in the current branch but that are not in <upstream> are saved to a temporary area.
[That's precisely our two local commits; those are the ones not in HEAD^^]
- The current branch is reset to <upstream>, or <newbase> if the --onto option was supplied.
[We did specify --onto, so the current branch goes weeee... to origin/master, the abruptly updated, non-fast-forward upstream that we want to catch up with.]
- The commits that were previously saved into the temporary area are then reapplied to the current branch, one by one, in order.
[That's the cherry-picking that we want: our two local commits.]
So end result is that our branch is now the same as origin/master (up-to-date) plus has the two needed local commits.
Further, I have a situation in which there are two git repos on the same machine which have a different version of a local commit. One of the repos (A) is an upstream for the other (B).
When B does a fetch, it gets the master branch from A with A's local commit, which is unwanted in B. B has its own flavor of that commit:
git rebase HEAD^ --onto origin/master^
takes care of it. We catch up with origin/master, but ignoring one commit, and on top of that we cherry pick ours.