stefanzweifel / git-auto-commit-action

Automatically commit and push changed files back to GitHub with this GitHub Action for the 80% use case.
MIT License
2.06k stars 230 forks source link

--amend option removes all git history #294

Closed jawoznia closed 1 year ago

jawoznia commented 1 year ago

git-auto-commit Version

v4

Machine Type

Ubuntu (eg. ubuntu-latest)

Bug description

I try to create a workflow that will update PRs on change. I don't want to create a new commit and would prefer to amend the automatic change to head commit in PR.

What I found is that for some reason the whole history is replaced with single new commit.

Steps to reproduce

It should be enough to run the example workflow.

Tried solutions

It is fine if I remove the amend command but it is not desired solution.

Example Workflow

my config 

name: Release plz 

on:
  pull_request:

jobs:
  print_commit_name:
    runs-on: ubuntu-latest

    permissions:
      # Give the default GITHUB_TOKEN write permission to commit and push the changed files back to the repository.
      contents: write

    if: startsWith(github.head_ref, 'release_')
    steps:
    - uses: actions/checkout@v3
      with:
        ref: ${{ github.head_ref }}
    - name: Create new file 
      run: echo '\n// New commit' > src/main.rs 
    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_options: '--amend'
        push_options: '--force'

Relevant log output

No response

stefanzweifel commented 1 year ago

Howdy

Using --amend can be dangerous. I thought I had a section about this in the README, but there's only this section about using it in combination with --no-edit.

The problem lies in how actions/checkout checks out a repository. By default, only the last commit (1 commit) is checked out / fetched and not the entire repo history.

Another user ran into a similar issue in https://github.com/stefanzweifel/git-auto-commit-action/issues/159#issuecomment-845347950.

To solve this, add fetch-depth: 2 and skip_fetch: true to your job.

    - uses: actions/checkout@v3
      with:
        ref: ${{ github.head_ref }}
+       fetch-depth: 2

    - uses: stefanzweifel/git-auto-commit-action@v4
      with:
        commit_options: '--amend'
        push_options: '--force'
+       skip_fetch: true

fetch-depth: 2 to fetch more than just 1 single commit and not destroy the local commit history.

skip_fetch: true to disable an internal feature of git-auto-commit, where we run git fetch just before we try to run git checkout -B.

Let me know if this works or if you still run into the issue.

jawoznia commented 1 year ago

Thanks for a quick response and great explanation. It works now.