Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VSTS and Git: Why does squashing my DEV branch while merging to master says that DEV is both behind and ahead of master?

I'm hoping someone can help me with this as I'm scratching my head to understand what is going on, and if it can be rectified.

I'm currently working on a project in VSTS and using GIT as the code repository. I have the usual MASTER branch, with a DEVELOPMENT branch coming off that. I then create feature branches off the DEVELOPMENT branch.

When we have finished the changes in the feature branch, I create a Pull Request and can successfully merge the changes into the DEV branch. The DEV branch then shows '0' behind and 'x' ahead of MASTER ... which is correct.

The issue comes when we are ready to merge the changes into MASTER. We create a PULL REQUEST to do this and the changes successfully merge into MASTER ... however ... the DEV branch now says that is 1 behind MASTER and still x ahead of MASTER!! Why is DEV 1 behind MASTER? And why is DEV still x ahead of MASTER? After the PULL REQUEST, shouldn't MASTER and DEV be in sync? That is, DEV should be 0 behind, and 0 ahead of MASTER?

There is a very good chance I'm not understanding GIT properly, but could I have some settings in VSTS wrong ... like a branching policy incorrectly set? The only branch policy I have set on MASTER (at this stage) is "Enforce a merge strategy - Squash merge".

Thanks in advance.

like image 340
Dazfl Avatar asked Feb 19 '18 08:02

Dazfl


People also ask

Should I squash commits when merging to master?

As a general rule, when merging a pull request from a feature branch with a messy commit history, you should squash your commits. There are exceptions, but in most cases, squashing results in a cleaner Git history that's easier for the team to read.

What does git merge -- squash do?

Squash merging is a merge option that allows you to condense the Git history of topic branches when you complete a pull request. Instead of each commit on the topic branch being added to the history of the default branch, a squash merge adds all the file changes to a single new commit on the default branch.

What happens when merging branches?

When you perform a merge, you effectively merge one branch into another—typically a feature branch or bug fix branch into a main branch such as master or develop. Not only will the code changes get merged in, but also all the commits that went into the feature branch.


2 Answers

Squash merge is the cause for your misunderstanding.

When you squash merge, all the commits of your development branch are squashed into one single commit. This is the reason why DEV is 1 behind master, since it doesn't has the squashed commit. Also DEV is x ahead of master because DEV has x commits which aren't in master.

Ideally you should only squash merge your feature/topic branches, that will give you one commit per feature. You should not squash your dev branch when you merge into master. So you can change the branch policy on master and put that policy in DEV if you want that. My advice is to let your developers decide when to squash or not. When you complete a PR in VSTS, it gives you an option to squash.

like image 118
Harshil Lodhi Avatar answered Sep 20 '22 20:09

Harshil Lodhi


Just to add some more visibility to @harshil-lodhi 's (absolutely correct) answer.

Before you create a pull request, the state of the repository looks like this:

enter image description here

If you look at this repository inside the SourceTree tool, it proves the numbers are correct (1 behind, 3 ahead):

enter image description here

When you merge the pull request enforcing squashing of the commits, the state changes to the following (VSTS):

enter image description here

And SourceTree:

enter image description here

The dev branch still has its 3 separate commits, which is why it is 3 commits ahead of master. On the other hand, master has one more commit, squashed changes that arrived from the dev branch during the merging/squashing. As you can see. the squashed commit is not a merge commit - it doesn't have 2 parents. Although the contents of the dev and master branches are the same, they are still different branches for Git.

like image 37
Yan Sklyarenko Avatar answered Sep 21 '22 20:09

Yan Sklyarenko