Lets say I'm developing feature A on branch feat-a
. I am done with the feature, I commit my changes in commit X
and push and create a pull request and ask for it to be reviewed. In the meantime I start work on feature B git checkout feat-b
which builds on top of feature A so I rebase my feat-b
branch on feat-A
with git rebase feat-a
and start to build some features and commit them in commit (Y)
Now the reviewer finds a small problem in the code - lets say some documentation missing. I fix the issue and change commit (X) with git commit --ammend
which now creates the new commit Z
which replaces X
.
- main branch now has the history
common-ancestor-commit *** commit-Z
- feat-b branch now has the history
common-ancestor-commit *** commit-X *** commit-Y
How can I fix things such that feat-b is: common-ancestor-commit *** commit-Z *** commit-Y
?
CodePudding user response:
Depending on what the change was between X
and Z
, this might simply work:
git fetch
git rebase Z feat-b
# or in the likely case that feat-b is already checked out:
git rebase Z
In words that says: "Take all of the commits reachable by feat-b
that are not reachable by Z
and replay them, one by one, on top of Z
." So this will reset feat-b
to Z
, and then cherry-pick X
and then Y
. The reason it might work is if the changes in X
are a subset of the new changes in Z
, then during that rebase X
will fall out. Of course it's also possible that it won't fall out, and in that case it could leave you with stuff you no longer want, or perhaps you'll have conflicts, so the safer way to go is to use the --onto
option:
git fetch
git rebase X feat-b --onto Z
In words that says: "Take all of the commits reachable by feat-b
that are not reachable by X
and replay them, one by one, on top of Z
." This will reset feat-b
to Z
and then cherry-pick just Y
, which is exactly what you want. Although unlikely, note it's still possible you'll have a conflict here too, and if so simply resolve it.
Side Note: in all of the above commands you can replace Z
with main
if desired, since the Pull Request was already completed.