0

My remote master branch: A D

My remote dev branch: A B

My local dev branch: A B C

Current Merge request in Github: remote dev -> remote master

How can I make the remote dev branch as A D B' C', so that I can make the remote master branch as A D B' C' by just approving current Merge request?

What I did is

  1. making the local dev branch A D B' C'
  2. tried to push local dev branch which is now A D B' C' to the remote dev branch which is A B

and as you can guess, it said

"Updates were rejected because the tip of your current branch is behind"

Could you let me know what is the best practice here?

Maradox
  • 620
  • 2
  • 13
  • Does this answer your question? [Updates were rejected because the tip of your current branch is behind its remote counterpart](https://stackoverflow.com/questions/39399804/updates-were-rejected-because-the-tip-of-your-current-branch-is-behind-its-remot) - all that’s required is to force-push – AD7six Jul 16 '21 at 11:13

1 Answers1

0

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.

schilli
  • 1,700
  • 1
  • 9
  • 17
  • I think it is not that strange. I started from the master when it does not have a commit D yet, and while I am working on dev branch by committing B, C, another team mate made a update in the remote master branch by committing D. (which is really common) – Maradox Jul 15 '21 at 01:28
  • Ok. I see. Did my advice help? Could you solve it with a force push? – schilli Jul 15 '21 at 19:45
  • @AD7six How would you undo a force push? I would really like to know how if I’m mistaken and it’s possible. – schilli Jul 16 '21 at 18:31
  • @AD7six thanks. I wasn't aware of that, but still I would categorize it as more advanced git knowledge. I'll edit the answer. – schilli Jul 20 '21 at 12:36
  • Yea I know, but I guess it may help someone else who does not know how to achieve this step from question: "making the local dev branch A D B' C'". I'll also add a TL;DR section for the force push. I'm also fine with the down vote if you still find the answer too long. Trying to improve it :) – schilli Jul 20 '21 at 12:44
  • It’s always better to (just) answer the asked question, that’ll do for now though :) – AD7six Jul 22 '21 at 15:57