Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run git diff in github actions

I am getting this:

Command failed: git diff --name-only HEAD^..HEAD
fatal: ambiguous argument 'HEAD^..HEAD': unknown revision or path not in the working tree.

I want to run git diff --name-only HEAD^..HEAD in my branch to get a list of the files that were changed. It's working locally but not on GitHub actions. What must I do?

My code is this:

name: build
on:
  push:
    branches:
      - main
jobs:
  run:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
      - name: Configure Node.js
        uses: actions/setup-node@v2
        with:
          node-version: 14.x
      - name: Install dependencies
        run: yarn install
      - name: Publish file changes to Slack
        # HERE I run `git diff` in node.js process
        run: "SLACK_TOKEN=${{ secrets.GITHUB_TOKEN }} npx ts-node scripts/publishSlackUpdate"
      - name: Build TOC
        run: make toc
      - name: Commit build changes
        uses: EndBug/add-and-commit@v7
        with:
          author_name: Docs Builder
          author_email: [email protected]
          message: 'Updated build'
          add: '*.md'
like image 899
alien Avatar asked Jan 28 '21 20:01

alien


People also ask

Can you do a diff on GitHub?

On the Github, go to the Source view of your project. You will see a link named 'Branch List'. Once the page opens you can see a list of all the remote branches. Hit on the Compare button in front of any of the available branches to see the difference between two branches.

How do I git diff?

Use ONLY q+enter to exit. It's possible to break out by repeatedly typing q+enter+q+enter+q+enter until the end of time no matter what the console shows.

Can I use git diff to compare two files?

You can compare files between two Git commits by specifying the name of the ref that refers to the commits you want to compare. A ref may be a commit ID or HEAD, which refers to the current branch. Let's compare two commits in our Git repository. The above command will perform a diff operation across our two commits.

How do I run an action in GitHub?

On GitHub.com, navigate to the main page of the repository. Under your repository name, click Actions. In the left sidebar, click the workflow you want to run. Above the list of workflow runs, select Run workflow.


Video Answer


2 Answers

If you take a look at the documentation for the actions/checkout@v2 action, you'll see it performs a shallow clone with a single revision by default:

    # Number of commits to fetch. 0 indicates all history for all branches and tags.
    # Default: 1
    fetch-depth: ''

Because it only fetches a single revision, there is no HEAD^.

You can fix this by setting the fetch-depth option on the checkout action. Setting it to 0 will fetch the entire history; alternatively, for what you're doing you could probably just set it to 2:

    steps:
      - name: Checkout repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 2
like image 50
larsks Avatar answered Oct 05 '22 14:10

larsks


Putting origin/ before branch name worked for me

git diff --name-only origin/main origin/${GITHUB_HEAD_REF}
like image 31
vineet Avatar answered Oct 05 '22 14:10

vineet