Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git master and development out of sync

I have a master and development branch. The main (server) repos are hosted in Azure Devops (on premise). I did two Pull Requests (from feature branches into development) for changes in my development branch.

PR1: feature-branch-1 -> development

PR2: feature-branch-2 -> development

Then I thought, I need to sync my development branch with my master branch, so I did a Pull Request from my development branch to my master branch, accepted, committed.

PR3: development -> master

Now it is saying my development branch is "1 behind and 2 ahead of master"

Comparing development to master shows my two feature Pull Requests. Comparing master to development shows my merge PR to sync the branches.

What in my process did I do wrong, and how do I fix this - get them in sync?

like image 561
user210757 Avatar asked Jun 06 '19 15:06

user210757


2 Answers

The process you did is correct, but depending on what you chose one of the following happened.

  • When you performed the PR merge, you chose "Merge", which will create a merge commit on the target (master) branch. Thus Master is 1 ahead of development.
  • When you performed the PR merge, you chose "Squash", in which case it will create a new commit with the changes to target (master) branch. This Master is 1 commit ahead of development and 2 commits behind.
  • When you performed a Fast Forward, if no merge were required, the target and source branch would now be the same. And no merge commit would be added.
  • When you had rebased develop onto master, all changes would be replayed on the master branch and then committed. You'd have to force push develop in that case.

To make sure develop is back in sync with master, you have a few options:

  • Merge master back to develop. Develop will be 1 merge commit ahead of master afterwards.
  • Rebase develop onto master and force push. Develop will be the same as master + any outstanding changes will be replayed on top of develop.
  • Reset develop to master and force push. Develop will be the same as master + any outstanding changes will be lost.
  • Delete/rename the current develop branch and create a new one from master.
like image 180
jessehouwing Avatar answered Oct 02 '22 21:10

jessehouwing


Now it is saying my development branch is "1 behind and 2 ahead of master"

Comparing development to master shows my two feature Pull Requests. Comparing master to development shows my merge PR to sync the branches.

No error about your operation, and the answer(1 behind and 2 ahead of master) is also correct.

Before all, you need to know what does "Squash Merge" mean.

Let's name these commits: merge commit from feature-branch-1 to Development is A. The commit from feature-branch-2 to Development is B. The merge commit created from Development to Master is C.

While performing merge feature-branch-1 to Development with Squash Merge, it will create new commit A'. In fact, compare with their content, there's no defference between A and A'. But the feature branch itself does not have a commit merging it into the default branch. enter image description here

So, follow this logic. While you performing merge Development to Master, it will create one new commit C'. After merge, in Master branch, it just add one new commit C', not C. That's why you get the behind|ahead as 1 behind and 2 ahead of master.

Change your compare branch to Development, you will see that the behind|ahead of your feature-branch-1 and feature-branch-2 is 2|1. That's all because of Squash Merge.

The advantage of Squash Merge is keeping your default branch histories clean and easy to follow without demanding any workflow changes on your team.

But if you want get them in sync with history, please change your merge type as Basic Merge.

like image 34
Mengdi Liang Avatar answered Oct 02 '22 20:10

Mengdi Liang