git-auto-commit Action
The GitHub Action for committing files for the 80% use case.
A GitHub Action to detect changed files during a Workflow run and to commit and push them back to the GitHub repository.
By default, the commit is made in the name of "GitHub Actions" and co-authored by the user that made the last commit.
If you want to learn more how this Action works under the hood, check out this article by Michael Heap.
If your use case is not covered by git-auto-commit, you might want to check out the following alternative Actions:
Usage
Adding git-auto-commit to your Workflow only takes a couple lines of code.
- Set the
contents-permission of the default GITHUB_TOKEN to write. (Required to push new commits to the repository)
- Add the following step at the end of your job, after other steps that might add or change files.
- uses: stefanzweifel/git-auto-commit-action@v7
Your Workflow should look similar to this example.
name: Format
on: push
jobs:
format-code:
runs-on: ubuntu-latest
permissions:
# Give the default GITHUB_TOKEN write permission to commit and push the
# added or changed files to the repository.
contents: write
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.head_ref }}
# Value already defaults to true, but `persist-credentials` is required to push new commits to the repository.
persist-credentials: true
# Other steps that change files in the repository go here
# …
# Commit all changed files back to the repository
- uses: stefanzweifel/git-auto-commit-action@v7
[!NOTE]
The Action has to be used in a Job that runs on a UNIX-like system (e.g. ubuntu-latest).
The following is an extended example with all available options.
- uses: stefanzweifel/git-auto-commit-action@v7
with:
# Optional. Commit message for the created commit.
# Defaults to "Apply automatic changes"
commit_message: Automated Change
# Optional. Remote branch name where commit is going to be pushed to.
# Defaults to the current branch.
branch: feature-123
# Optional. Options used by `git-commit`.
# See https://git-scm.com/docs/git-commit#_options
commit_options: '--no-verify --signoff'
# Optional glob pattern of files which should be added to the commit
# Defaults to all (.)
# See the `pathspec`-documentation for git
# - https://git-scm.com/docs/git-add#Documentation/git-add.txt-ltpathspecgt82308203
# - https://git-scm.com/docs/gitglossary#Documentation/gitglossary.txt-aiddefpathspecapathspec
file_pattern: '*.php src/*.js tests/*.js'
# Optional. Relative file path under $GITHUB_WORKSPACE to the repository.
# Defaults to the root of the repository.
repository: .
# Optional commit user and author settings
commit_user_name: My GitHub Actions Bot # defaults to "github-actions[bot]"
commit_user_email: my-github-actions-bot@example.org # defaults to "41898282+github-actions[bot]@users.noreply.github.com"
commit_author: Author <actions@github.com> # defaults to "username <numeric_id+username@users.noreply.github.com>", where "numeric_id" and "username" belong to the author of the commit that triggered the run
# Optional. Tag name to be created in the local repository and
# pushed to the remote repository on the defined branch.
# If only one of `tag_name` or `tagging_message` is provided, the value of the provided field will be used for both tag name and message.
tag_name: 'v1.0.0'
# Optional. Message to annotate the created tag with.
# If only one of `tag_name` or `tagging_message` is provided, the value of the provided field will be used for both tag name and message.
tagging_message: 'Codename "Sunshine"'
# Optional. Option used by `git-status` to determine if the repository is
# dirty. See https://git-scm.com/docs/git-status#_options
status_options: '--untracked-files=no'
# Optional. Options used by `git-add`.
# See https://git-scm.com/docs/git-add#_options
add_options: '-u'
# Optional. Options used by `git-push`.
# See https://git-scm.com/docs/git-push#_options
push_options: '--force'
# Optional. Disable dirty check and always try to create a commit and push
skip_dirty_check: true
# Optional. Skip internal call to `git fetch`
skip_fetch: true
# Optional. Skip internal call to `git checkout`
skip_checkout: true
# Optional. Skip internal call to `git push`
skip_push: true
# Optional. Prevents the shell from expanding filenames.
# Details: https://www.gnu.org/software/bash/manual/html_node/Filename-Expansion.html
disable_globbing: true
# Optional. Create given branch name in local and remote repository.
create_branch: true
# Optional. Creates a new tag and pushes it to remote without creating a commit.
# Skips dirty check and changed files. Must be used in combination with `tag` and `tagging_message`.
create_git_tag_only: false
# Optional. Suppress the security warning emitted when the action runs on a
# `pull_request_target` event. See the "Workflow should run in **base** repository"
# section below for context before disabling this warning.
disable_pull_request_target_trigger_warning: false
# Optional. Shell snippets to run around each git operation. Each hook
# is evaluated in the same bash process as the action — `set -eu` is
# in effect, the working directory is your repository, and all
# `INPUT_*` env vars are visible. A non-zero exit aborts the action.
before_add_hook: ''
after_add_hook: ''
before_commit_hook: ''
after_commit_hook: ''
before_tag_hook: ''
after_tag_hook: ''
before_push_hook: ''
after_push_hook: ''
Please note that the Action depends on bash. If you're using the Action in a job in combination with a custom Docker container, make sure that bash is installed.
Example Workflow
In this example, we're running php-cs-fixer in a PHP project to fix the codestyle automatically, then commit possible changed files back to the repository.
Note that we explicitly specify ${{ github.head_ref }} in the checkout Action.
This is required in order to work with the pull_request event (or any other non-push event).
name: php-cs-fixer
on:
pull_request:
push:
branches:
- main
jobs:
php-cs-fixer:
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
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.head_ref }}
- name: Run php-cs-fixer
uses: docker://oskarstark/php-cs-fixer-ga
- uses: stefanzweifel/git-auto-commit-action@v7
with:
commit_message: Apply php-cs-fixer changes
See EXAMPLES.md for more scenarios, including auto-formatting, dependency updates, generated docs, release tagging, drift checks, and GPG-signed commits.
Inputs
Checkout action.yml for a full list of supported inputs.
Outputs
You can use these outputs to trigger other Actions in your Workflow run based on the result of git-auto-commit-action.
changes_detected: Returns either "true" or "false" if the repository was dirty and files have changed.
commit_hash: Returns the full hash of the commit if one was created.
create_git_tag_only: Returns either "true" or "false" if a tag was created, when create_git_tag_only was used.
⚠️ When using outputs, the step needs to be given an id. See example below.
Example
- uses: stefanzweifel/git-auto-commit-action@v7
id: auto-commit-action #mandatory for the output to show up in ${{ steps }}
with:
commit_message: Apply php-cs-fixer changes
- name: "Run if changes have been detected"
if: steps.auto-commit-action.outputs.changes_detected == 'true'
run: echo "Changes!"
- name: "Run if no changes have been detected"
if: steps.auto-commit-action.outputs.changes_detected == 'false'
run: echo "No Changes!"
Hooks
git-auto-commit can run custom shell snippets around each git operation
it performs. This is useful when you need to prepare or clean up the
repository as part of the same step — for example, unshallowing a
shallow clone right before the commit is staged.
Eight optional hooks are available:
| Hook |
Runs |
before_add_hook / after_add_hook |
around git add |
before_commit_hook / after_commit_hook |
around git commit |
before_tag_hook / after_tag_hook |
around git tag (only when a tag is being created) |
before_push_hook / after_push_hook |
around git push (skipped when skip_push: true) |
Each hook is an inline shell snippet that runs in the same bash process
as the action. The working directory is your repository, and all
INPUT_* environment variables and standard GitHub Actions env vars are
visible to the snippet.
Example
- uses: stefanzweifel/git-auto-commit-action@v7
with:
before_add_hook: |
git fetch --unshallow
Multi-line snippets work via YAML's | block scalar:
- uses: stefanzweifel/git-auto-commit-action@v7
with:
before_commit_hook: |
echo "About to commit at $(date)"
./scripts/prepare-commit.sh
Notes
- A hook only runs when its underlying step actually runs. For example,
before_add_hook/after_add_hook are skipped when the working tree is clean,
and before_push_hook/after_push_hook are skipped when skip_push: true.
- If a hook exits with a non-zero status, the action fails. Append
|| true to a snippet to ignore its failure.
- Hooks share environment with the action, so they can read action
inputs (e.g.
$INPUT_COMMIT_MESSAGE) and write to $GITHUB_OUTPUT.
- Snippets run under
set -eu. Referencing an unset variable aborts the
action; use ${VAR:-} to default an optional variable to empty.
Security
Hook snippets are evaluated as shell code in the same process as the
action. Treat them as you would any run: step.
[!CAUTION]
Do not combine hooks with the pull_request_target event when the
snippet references attacker-controlled GitHub context. Fields like
${{ github.event.pull_request.title }}, ${{ github.event.pull_request.body }},
${{ github.head_ref }}, and commit messages from a fork are
interpolated into the snippet before bash sees it. A malicious PR
can inject shell commands that run on your runner with access to your
repository secrets. See the pull_request_target section
for the broader risk and GitHub's script-injection guidance.
If you need values from PR-controlled context inside a hook, pass them
via an intermediate env var rather than interpolating them directly into
the snippet:
- uses: stefanzweifel/git-auto-commit-action@v7
env:
PR_TITLE: ${{ github.event.pull_request.title }}
with:
before_commit_hook: |
# $PR_TITLE is read as data, not evaluated as code
echo "PR: $PR_TITLE"
Limitations & Gotchas
The goal of this Action is to be "the Action for committing files for the 80% use case". Therefore, you might run into issues if your Workflow falls into the not supported 20% portion.
The following is a list of edge cases the Action knowingly does not support:
No git pull when the repository is out of date with remote. The Action will not do a git pull before doing the git push. You are responsible for keeping the repository up to date in your Workflow runs.
No support for running the Action in build matrices. If your Workflow is using build matrices, and you want that each job commits and pushes files to the remote, you will run into the issue, that the repository in the workflow will become out of date. As the Action will not do a git pull for you, you have to do that yourself.
No support for git rebase or git merge. There are many strategies on how to integrate remote upstream changes to a local repository. git-auto-commit does not want to be responsible for doing that.
No support for detecting line break changes between CR (Carriage Return) and LF (Line Feed). This is a low level issue, you have to resolve differently in your project. Sorry.
If this Action doesn't work for your workflow, check out EndBug/add-and-commit.
Checkout the correct branch
You must use actions/checkout@v2 or later versions to check out the repository.
In non-push events, such as pull_request, make sure to specify the ref to check out:
- uses: actions/checkout@v7
with:
ref: ${{ github.head_ref }}
Do this to avoid checking out the repository in a detached state.
Commits made by this Action do not trigger new Workflow runs
The resulting commit will not trigger another GitHub Actions Workflow run.
This is due to limitations set by GitHub.