<p>After searching the internet, I couldn't find any details on how to complete the PR, so I settled on the second option of just doing a merge.</p>
<p>To fix the error of &quot;fatal: refusing to merge unrelated histories&quot;, I found an article <a href="https://hull.au/blog/git-merging-within-devops-pipelines/" rel="nofollow noreferrer">here</a>: that specified to add a &quot;fetchDepth: 0&quot;:</p>
<pre><code>steps:
- checkout: self
  persistCredentials: true
  clean: true
  fetchDepth: 0
</code></pre>
<p>And that fixed everything. Here is the final yaml:</p>
<pre><code>name: merge develop to main
trigger: none

pool:
  vmImage: 'windows-latest'

steps:
- checkout: self
  persistCredentials: true
  clean: true
  fetchDepth: 0

- script: |
   git config --local user.email &quot;REDACTED&quot;
   git config --local user.name &quot;REDACTED&quot;
   git fetch --all
   git checkout develop
   git checkout main
   git merge develop -m &quot;Merging develop into main via manual Azure Pipeline&quot;
   git push origin
   git status
</code></pre>

CC BY-SA 4.0
2024-12-17T17:40:05.410Z
2024-12-17T17:40:05.410Z
Wade Baird
<p>I want to create a (manually triggered) Azure pipeline that merges the develop branch to main. It either be merged via an auto completed PR or just a merge. I have scoured the internet and the two options I have found are the 1) InvokeRESTAPI@1, or 2) Git commands via scripts. I have tried both, and can't get either to work.</p>
<p><strong>1) InvokeRESTAPI@1</strong></p>
<p>Using the rest api, I can get it to create a PR, but can't get that PR to autocomplete or just complete. I have found some articles that said you need to create the PR, then update it. However, the serverless agent doesn't allow for me to get the created PR id and then update it with completed, so I think I'd have to call the REST Api from a script to make that work. Here is my yaml for that attempt:</p>
<pre><code>name: merge dev to main
trigger: none

pool: server

steps:
- task: InvokeRESTAPI@1
  inputs:
    connectionType: 'connectedServiceName'
    serviceConnection: 'REDACTEDNAME'
    method: 'POST'
    headers: |
      {
        &quot;Content-Type&quot;: &quot;application/json&quot;,
        &quot;Authorization&quot;: &quot;Bearer $(System.AccessToken)&quot;
      }
    body: |
      {
        &quot;sourceRefName&quot;: &quot;refs/heads/develop&quot;,
        &quot;targetRefName&quot;: &quot;refs/heads/main&quot;,
        &quot;title&quot;: &quot;Merging develop into main via manual pipeline&quot;,
        &quot;description&quot;: &quot;Manual merge via Azure Pipeline&quot;,
        &quot;mergeOptions&quot; : &quot;noFastForward&quot;,
        &quot;status&quot; : &quot;completed&quot;,
        &quot;autoCompleteSetBy&quot;: &quot;$(Build.QueuedById)&quot;,
        &quot;vote&quot;: 10,
        &quot;closedBy&quot;: &quot;$(Build.QueuedById)&quot;
      }
    urlSuffix: '/_apis/git/repositories/$(Build.Repository.ID)/pullRequests?api-version=7.1'
    waitForCompletion: 'false'
</code></pre>
<p><strong>2) Scripts</strong></p>
<p>I next attempted to just do a merge via the script option. However I can't get the merge to work here either. I keep getting the error of &quot;fatal: refusing to merge unrelated histories&quot;. Which everything I've found on the internet says that is when histories are truly unrelated, which isn't the case here.</p>
<p>Here is my yaml for this attempt:</p>
<pre><code>name: merge develop to main
trigger: none

pool:
  vmImage: 'windows-latest'

steps:
- checkout: self
  persistCredentials: true
  clean: true

- script: |
   git fetch --all
   git checkout develop
   git checkout main
   git merge develop -m &quot;Merging develop into main via manual Azure Pipeline&quot;
   git push origin
   git status
</code></pre>
<p>and here is the output from that:</p>
<pre><code>branch 'develop' set up to track 'origin/develop'.
Previous HEAD position was b49ec9b update yaml test
Switched to a new branch 'develop'
branch 'main' set up to track 'origin/main'.
Switched to a new branch 'main'
fatal: refusing to merge unrelated histories
Everything up-to-date
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Finishing: CmdLine
</code></pre>
<p>Any assistance would be greatly appreciated. I would prefer the scripted route and just do a merge instead of a PR.</p>

<azure-devops><azure><azure-pipelines>
Azure Pipeline to merge branches
Wade Baird
# Azure Pipeline to merge branches

(Asked by: username_0 on 2024-12-17)

I want to create a (manually triggered) Azure pipeline that merges the develop branch to main. It either be merged via an auto completed PR or just a merge. I have scoured the internet and the two options I have found are the 1) InvokeRESTAPI@1, or 2) Git commands via scripts. I have tried both, and can't get either to work.
1) InvokeRESTAPI@1
Using the rest api, I can get it to create a PR, but can't get that PR to autocomplete or just complete. I have found some articles that said you need to create the PR, then update it. However, the serverless agent doesn't allow for me to get the created PR id and then update it with completed, so I think I'd have to call the REST Api from a script to make that work. Here is my yaml for that attempt:
name: merge dev to main
trigger: none

pool: server

steps:
- task: InvokeRESTAPI@1
  inputs:
    connectionType: 'connectedServiceName'
    serviceConnection: 'REDACTEDNAME'
    method: 'POST'
    headers: |
      {
        "Content-Type": "application/json",
        "Authorization": "Bearer $(System.AccessToken)"
      }
    body: |
      {
        "sourceRefName": "refs/heads/develop",
        "targetRefName": "refs/heads/main",
        "title": "Merging develop into main via manual pipeline",
        "description": "Manual merge via Azure Pipeline",
        "mergeOptions" : "noFastForward",
        "status" : "completed",
        "autoCompleteSetBy": "$(Build.QueuedById)",
        "vote": 10,
        "closedBy": "$(Build.QueuedById)"
      }
    urlSuffix: '/_apis/git/repositories/$(Build.Repository.ID)/pullRequests?api-version=7.1'
    waitForCompletion: 'false'

2) Scripts
I next attempted to just do a merge via the script option. However I can't get the merge to work here either. I keep getting the error of "fatal: refusing to merge unrelated histories". Which everything I've found on the internet says that is when histories are truly unrelated, which isn't the case here.
Here is my yaml for this attempt:
name: merge develop to main
trigger: none

pool:
  vmImage: 'windows-latest'

steps:
- checkout: self
  persistCredentials: true
  clean: true

- script: |
   git fetch --all
   git checkout develop
   git checkout main
   git merge develop -m "Merging develop into main via manual Azure Pipeline"
   git push origin
   git status

and here is the output from that:
branch 'develop' set up to track 'origin/develop'.
Previous HEAD position was b49ec9b update yaml test
Switched to a new branch 'develop'
branch 'main' set up to track 'origin/main'.
Switched to a new branch 'main'
fatal: refusing to merge unrelated histories
Everything up-to-date
On branch main
Your branch is up to date with 'origin/main'.

nothing to commit, working tree clean
Finishing: CmdLine

Any assistance would be greatly appreciated. I would prefer the scripted route and just do a merge instead of a PR.

**Tags:** azure-devops, azure, azure-pipelines


---

## 1 answers

### Answer by username_0 on 2024-12-17. Score: 0 | Accepted answer

After searching the internet, I couldn't find any details on how to complete the PR, so I settled on the second option of just doing a merge.
To fix the error of "fatal: refusing to merge unrelated histories", I found an article here: that specified to add a "fetchDepth: 0":
steps:
- checkout: self
  persistCredentials: true
  clean: true
  fetchDepth: 0

And that fixed everything. Here is the final yaml:
name: merge develop to main
trigger: none

pool:
  vmImage: 'windows-latest'

steps:
- checkout: self
  persistCredentials: true
  clean: true
  fetchDepth: 0

- script: |
   git config --local user.email "REDACTED"
   git config --local user.name "REDACTED"
   git fetch --all
   git checkout develop
   git checkout main
   git merge develop -m "Merging develop into main via manual Azure Pipeline"
   git push origin
   git status


---

