TL;DR
After you did make the local dev branch to match A D B C
, all you need to do is a force push instead of just a normal push:
git push -f
Comprehensive answer
(including to make the local dev branch match A D B C
)
Your scenario sounds really strange, because you have a commit (D
) on master, but not on develop. However, it should be possible to achieve what you want (getting A D B' C'
in dev branch).
First the key tools, which should help achieving the desired goal (I find the linked tutorials quite nice to read, so that should explain the commands well):
Now, I try to explain step by step how it should work. I have not tried it myself since it's a bit of a special setup. I will perform all operations on local develop branch with a final force push to overwrite the history and remote branch. Also I'll provide an expected output after each step.
Cherry pick the commit you like to have on develop branch:
git cherry-pick D
Now, your develop branch should look like this: A B C D
.
In the next step you will re-arrange the order of the commits using rebase:
git rebase -i HEAD~4
You should get following editor view:
pick A
pick B
pick C
pick D
# Rebase ...
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
In order to change the commit order just cut the line of commit D
and paste it after A
(then save and close the editor):
pick A
pick D
pick B
pick C
# Rebase ...
#
# Commands:
# p, pick = use commit
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
#
# If you remove a line here THAT COMMIT WILL BE LOST.
# However, if you remove everything, the rebase will be aborted.
#
I hope you don't get conflicts, but if you do you have to solve them (I'll skip on this).
Eventually, your local develop branch should look like the desired output: A D B C
. Therefore, you can now force push (DISCLAIMER: only force push if the local branch is correct! It will overwrite your remote branch and it's not as simple to recover it; see this answer):
git push -f
This will update the remote develop branch to the desired output: A D B C
.
I hope everything works as expected and it's somehow understandable how I explained it.