Table of Contents

Diverged Branches

Introduction

General

Diverged branches in Git occur when two branches that originally shared a common commit have since progressed differently, with each branch having commits that the other does not.

How Does This Happen?

Divergence usually happens when:

  1. Two branches share a common history.
  2. Commits are made separately to both branches without merging them.
  3. The branches evolve independently, resulting in separate commit histories.

How do you identify diverged branches?

If you go in the repository on KUDU and use git statusC:\home\site\repository>git status

You will see this message:

Test Locally

 Sometimes we can't see the underlying issue so we have an option to test it out locally and see the fix in Git client.

 Example:

 As you can see here there is a diverted branch that we need to merge into the main branch so it looks like this:

 By testing and resolving on a local clone we can get a better overview of what needs to be fixed

 These are the steps to get started:

 1.  Local clone of the environment that is causing the mess (you can do a "git status" in the Repository folder in Kudu)

2.  When it is cloned down make sure you do it via a git client (Fork, GitKraken etc)

3.  Go to Kudu and download the .git folder from the Repository

4.  When downloaded, clear the content of the .git folder on your local clone and insert the content from the one you just downloaded from Kudu, into the empty .git folder and then you will see the correct git changes

 5. There can only be one master so we need to either merge a master into origin/master or vice versa

 In our example, I had to merge origin/master into master

 6. Open git bash by right-clicking anywhere in the .git folder and open gitbash from there (see image)

 When opening in Git-Bash it needs to look like this:

 The (master) indicates we are in the master branch
 We need to switch it to origin/master and we can do that by typing "git checkout + id of the diverted branch"

 How do we find the diverted branch? Go to your git client and find the id here:
 

 So the git command will be "git checkout 005ba75936eafca29dc9a62d0e537e53c40e4f96"

 Alright, head back to the correct branch you want to merge to  the diverted branch by doing a "git checkout master"

When back, do a "git merge" and you can merge the branches together. 

When done and successful do "git status"

Do a "git add ." -> "git commit -m"merge fixed"

You can now run the local solution in Visual Studio to see if that fixed the error :) Not VS Code

We aren’t quite done yet. This has only been on local so far. We need to now do this on Cloud

When successfully done on local, you need to apply this on the diverged branch on Cloud in the Repository folder

 And boom, you are done. This is the graphical version of Eric's steps. Enjoy :)

Explanation about diverge branch and global.json

Let me explain about the diverging branch and how it keeps happening:

When you deploy a temporary file called global.json will be added in your solution. It will be removed after a successful deploy and the builds succeed. If the build fails and deploy fails it will not remove the global.json file and then it will automatically create a diverged branch and keep all the changes in the diverged branch.

The fix is to remove the file and fix the issue by merging the branches together.

So to prevent this in the future please make sure your build succeeds!

Test Locally

Test Locally

 Sometimes we can't see the underlying issue so we have an option to test it out locally and see the fix in Git client.

 Example:

 As you can see here there is a diverted branch that we need to merge into the main branch so it looks like this:

 By testing and resolving on a local clone we can get a better overview of what needs to be fixed

 These are the steps to get started:

 1.  Local clone of the environment that is causing the mess (you can do a "git status" in the Repository folder in Kudu)

2.  When it is cloned down make sure you do it via a git client (Fork, GitKraken etc)

3.  Go to Kudu and download the .git folder from the Repository

4.  When downloaded, clear the content of the .git folder on your local clone and insert the content from the one you just downloaded from Kudu, into the empty .git folder and then you will see the correct git changes

 5. There can only be one master so we need to either merge a master into origin/master or vice versa

 In our example, I had to merge origin/master into master

 6. Open git bash by right-clicking anywhere in the .git folder and open gitbash from there (see image)

 When opening in Git-Bash it needs to look like this:

 The (master) indicates we are in the master branch
 We need to switch it to origin/master and we can do that by typing "git checkout + id of the diverted branch"

 How do we find the diverted branch? Go to your git client and find the id here:
 

 So the git command will be "git checkout 005ba75936eafca29dc9a62d0e537e53c40e4f96"

 Alright, head back to the correct branch you want to merge to  the diverted branch by doing a "git checkout master"

When back, do a "git merge" and you can merge the branches together. 

When done and successful do "git status"

Do a "git add ." -> "git commit -m"merge fixed"

You can now run the local solution in Visual Studio to see if that fixed the error :) Not VS Code

We aren’t quite done yet. This has only been on local so far. We need to now do this on Cloud

When successfully done on local, you need to apply this on the diverged branch on Cloud in the Repository folder

 And boom, you are done. This is the graphical version of Eric's steps. Enjoy :)

Explanation about diverge branch and global.json

Let me explain about the diverging branch and how it keeps happening:

When you deploy a temporary file called global.json will be added in your solution. It will be removed after a successful deploy and the builds succeed. If the build fails and deploy fails it will not remove the global.json file and then it will automatically create a diverged branch and keep all the changes in the diverged branch.

The fix is to remove the file and fix the issue by merging the branches together.

So to prevent this in the future please make sure your build succeeds!