无论历史如何,将提交放在分支之上

Put a commit on top of a branch, regardless of history

我有两个分支,master 和 release,我正在尝试编写一个 github-action,它可以从 master 中提取所有内容,运行 一个构建命令,然后将更改推送到发布分支,而不用 master 的提交填充历史记录。

当我使用以下命令手动执行此操作时它有效:

git checkout release
git merge master --no-commit --squash
ionic build --prod
git commit -m "Build 1.x"

这不会引发任何合并冲突,而且可以正常工作。在操作中尝试它会导致合并冲突或不相关的历史错误。变基工作但它没有给我发布分支中的干净历史记录,以便在出现问题时轻松回滚。

我也尝试过使用孤立分支并将其重新定位到发布分支,但这会清除发布分支中的所有历史记录。我想我可能能够获取在该分支中生成的单个提交并将其放在发布之上,但我找不到执行此操作的命令。

这是我最初尝试的:

    name: Build
    branches: [ master ]

    workflow_dispatch:

    jobs:
      build:
        runs-on: ubuntu-latest

        steps:

          - uses: actions/checkout@v2
              with:
                ref: release

          - uses: actions/setup-node@v2
          - uses: coturiv/setup-ionic@v1

          - name: Install node Packages
              run : npm i

          - name: Get code changes for build
              run: |
               git fetch
               git checkout master
               git checkout release
               git rebase -s ours master release

          - name: Build
              run: ionic build --prod

          - name: Initialise Commit
             run: | 
             git config user.email "<ommitted>"
             git config user.name "<ommitted>"
             git add www/
             git commit -m "Build - $GITHUB_RUN_NUMBER"
      
         - name: Push Build
             run: git push --force

这是我目前得到的:

    name: Build

    on:
      push:
        branches: [ master ]

      workflow_dispatch:

    jobs:
      build:
        runs-on: ubuntu-latest

        steps:

          - uses: actions/checkout@v2
            with:
              ref: release

          - uses: actions/setup-node@v2
          - uses: coturiv/setup-ionic@v1

          - name: Install node Packages
            run: npm i

          - name: Initialise Git Config
            run: |
              git config user.email "simon@ineedsurgery.com"
              git config user.name "simon"

          - name: Get code changes for build
            run: |
              git fetch
              git checkout master
              git pull

          - name: Create temporary build branch
            run: |
              git checkout --orphan build-temp

          - name: Build
            run: |
              ionic build --prod
              git add --all
              git commit -m "Build - $GITHUB_RUN_ID"

          - name: Rebase build commit
            run: |
              git checkout release
              git pull
              git rebase -s ours build-temp release
              git branch build-temp -d
      
          - name: Push Build
            run: git push --force

感谢您的帮助,我希望我的问题很清楚!

“我想我可能能够获取在该分支中生成的单个提交并将其放在发布之上,但我找不到执行此操作的命令。”

你试过git cherrypick了吗?这实质上允许您从一个分支中选择一个提交并将其应用于另一个分支,而不管历史如何(正如您在标题中所希望的那样)。它被描述为 here.

来自该教程:

“为了演示如何使用 git cherry-pick 让我们假设我们有一个具有以下分支状态的存储库:

a - b - c - d   Master
     \
       e - f - g Feature

git cherry-pick 用法很简单,可以像这样执行:

git cherry-pick commitSha

在此示例中,commitSha 是一个提交引用。您可以使用 git log 找到提交参考。在此示例中,我们构建了假设我们想在 master 中使用提交 f。首先,我们确保我们正在 master 分支上工作。

git checkout master

然后我们用下面的命令执行cherry-pick

git cherry-pick f

一旦执行,我们的 Git 历史将如下所示:

a - b - c - d - f   Master
     \
       e - f - g Feature

已成功将 f 提交选中到 master 分支中。"

我找到了一个合并参数,可以让我实现我正在寻找的东西

--allow-unrelated-histories

它很好地隐藏在文档中。 cherrypick 答案看起来很适合手动编辑分支,但由于它是自动化的,所以它不起作用。

这是我的最终代码:

# This is a basic workflow to help you get started with Actions

name: Build

# Controls when the action will run. 
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  push:
    branches: [ dev ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it

      - uses: actions/checkout@v2
        with:
          ref: release

      - uses: actions/setup-node@v2
      - uses: coturiv/setup-ionic@v1

      - name: Install node Packages
        run: npm i

      - name: Initialise Git Config
        run: |
          git config user.email "blah"
          git config user.name "blah"

      - name: Get code changes for build
        run: |
          git fetch
          git checkout dev
          git pull
      
      - name: Checkout release and merge
        run: |
          git checkout release
          git pull
          git merge dev --squash --no-commit --allow-unrelated-histories -X theirs
          git reset

      - name: Build
        run: |
          ionic build --prod
          git add --all
          git commit -m "Build - $GITHUB_RUN_ID"
      
      - name: Push Build
        run: git push --force