首页 > 解决方案 > git 仅添加 master 中尚未跟踪的文件

问题描述

是否有一个命令仅用于暂存分支尚未git跟踪的文件(具有特定扩展名) ?main

我的具体用例是在 PR 上触发的 GitHub 操作,该操作使用压缩 SVG 资产,svgo但前提是这些资产尚未在main.

name: SVG Compression

on:
  pull_request:
    branches:
      - master
    paths:
      - '**.svg' # only run workflow when SVG files changed

jobs:
  tests:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repo
        uses: actions/checkout@v2

      - name: Set up node
        uses: actions/setup-node@v2

      - name: Install SVGO
        run: |
          npm -g svgo

      - name: Compress new SVG assets and commit to current PR
        run: |
          svgo assets/*
          # how to `git add` only if the file is new to the `main` branch?
          # ignore files with small but irrelevant diffs from repeated compression
          git add assets/*.svg
          git commit -m 'compress new SVG assets'

标签: gitgithubbranchgithub-actionsgit-add

解决方案


推荐阅读