'How to check the diff between the last 2 pushed commits

So, I have a GitHub workflow that runs on push and checks for changes in a specific directory and runs a script if there are changes. However, with the current implementation (git diff HEAD^ HEAD), it only checks the diff between the last 2 commits. It is entirely possible that more than 1 commit was pushed so I am looking for a way through which I can compare the last 2 pushed commits.

Would be awesome if anyone can help me with this!



Solution 1:[1]

You can use the following:

on: 
  push:

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2
        with:
          # checkout full tree
          fetch-depth: 0
      - run: |
          git diff ${{github.event.before}} ${{github.sha}}

As per the docs on the github context and the docs on the push webhook event data {{github.event.before}} is replaced by the commit SHA before the push. {{github.sha}} or {{github.event.after}} is replaced by the SHA of the latest commit that was pushed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 riQQ