前言
使用 Github Action 來執行 git merge 指令時,出現如下錯誤:
fatal: refusing to merge unrelated histories
Solution
原因出在 actions/checkout@v2 的 fetch-depth
v2 的 fetch-depth 預設為 1
需要設為 0 或將 --unshallow 新增至 fetch 指令
git fetch --unshallow
My Github Action
name: Merge branch
on:
  issues:
    types:
      - closed
jobs:
  merge-branch:      
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set git config
        run: |
          git config --local user.name XXX
          git config --local user.email <>          
      - name: Checkout
        run: |
          git fetch --unshallow
          git checkout [BRANCH_NAME]          
      - name: Merge
        run: |
          git merge main          
      - name: Push
        run: |
          git push --set-upstream origin [BRANCH_NAME]          
Reference
fatal: refusing to merge unrelated histories Git - git-fetch Document