Search code examples
gitgithub-actionsgithub-pagesgit-submodules

"refusing to merge unrelated histories" When updating submodule refs in a Github Action


I have a Github action that I'm using to try and update project dependencies. I'm using a repository_dispatch trigger to attempt to pull and update a child repo is updated. When the child repo is updated it sends a repository_dispatch event to the parent service, which has the child repo as a git submodule.

I'm able to successfully pull the repo, and pull the submodules using the actions below, however when I try to update the submodule tag either with git submodule update --remote --merge or by CDing into the directory and doing a git pull I get an unrelated histories error message.

The relevant part of my action is below. The trigger repo and the submodule repo are both private. I've commented how each of the steps are returning.

name: Handle_New_Data
on:
  repository_dispatch:
    types: [new_data]

jobs:
  build:
    runs-on: windows-latest
    env: 
      GH_ACCESS_TOKEN: ${{secrets.GH_AT}}
    steps:
      - name: Setup github
        uses: actions/checkout@v4
        with:
          token: ${{ secrets.GH_CI_TOK }}
          submodules: 'recursive'
          ref: 'main'

      # this is just here for testing - main is the default branch for this repository
      - name: Setup for git private repos
        run: |
          git config --global url.https://[email protected]/.insteadOf https://github.com/
          git branch -M main

      # In the github action this correctly shows the contents of the submodule's repository
      - name: Check submodule dir
        run: ls DataSubMod
        
      # This step returns success:
      # Fetching submodule DataSubMod
      # Already up to date.
      - name: Pull submodules
        run: |
          git pull --recurse-submodules

      # Either of these two options return the same error - 
      # "refusing to merge unrelated histories"
      - name: Update submodules
        run: |
          cd ./DataSubMod
          git pull https://github.com/myrepos/DataSubMod.git
          cd ..

      - name: Update submodules alt
        run: |
          git submodule update --remote --merge

I've tried a few other combinations (with and without ref in the checkout, submodules set to true instead of recursive, etc.) but no luck. If I follow these steps locally, the histories merge without issue.


Solution

  • Add fetch-depth: 0 to the checkout step:

    jobs:
      build:
        runs-on: windows-latest
        env: 
          GH_ACCESS_TOKEN: ${{secrets.GH_AT}}
        steps:
          - name: Setup github
            uses: actions/checkout@v4
            with:
              token: ${{ secrets.GH_CI_TOK }}
              submodules: 'recursive'
              ref: 'main'
              fetch-depth: 0
    

    Otherwise Actions will perform a shallow checkout.